0

I'm new to Spring Framework and trying to build a test project to understand how it works. Based on this tutorial Netbeans tutorial, please see my code bellow.

CODE:

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>
            /
        </url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>
        *.html
    </url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

dispatcher-servelt.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean class="controller.CarController" 
          p:carService-ref="carService"
    />

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

CarService.java

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

/**
 *
 * @author PTOSH
 */
public class CarService {
    public String sayModel(String name) {
        return "Hello" + name + "!";
    }
}

CarController.java

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.CarService;

/**
 *
 * @author PTOSH
 */
@RequestMapping("/")
public class CarController extends SimpleFormController{
    private CarService carService;    
        public CarController() {
        //Initialize controller properties here or
        //in the Web Application Context

        setCommandClass(Name.class);
        setCommandName("name");
        setSuccessView("helloView");
        setFormView("nameView");
    }

    public void setCarService(CarService carService)
    {
        this.carService = carService;
    }
    public CarService getCarService()
    {
        return this.carService;
    }
    //Use onSubmit instead of doSubmitAction
    //when you need access to the Request, Response, or BindException objects
    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        Name name = (Name) command;
        ModelAndView mv = new ModelAndView(getSuccessView());
        mv.addObject("helloMessage", carService.sayModel(name.getValue()));
        return mv;
    }
}

applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <bean name="carService" class="service.CarService" />
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

GlassFish Server log:

Infos: Mapped URL path [/car*] onto handler 'controller.CarController#0' Infos: Mapped URL path [/index.htm] onto handler 'indexController' Infos: FrameworkServlet 'dispatcher': initialization completed in 637 ms Infos: Loading application [HelloSpring] at [/HelloSpring] Infos: HelloSpring was successfully deployed in 3 649 milliseconds. Avertissement: No mapping found for HTTP request with URI [/HelloSpring/hello.htm] in DispatcherServlet with name 'dispatcher'

Thank you for any help you can provide.

Greg86
  • 67
  • 1
  • 2
  • 15

1 Answers1

0

As per the mapping and configuration you should have the jsp files inside the WEB-INF/jsp folder.

setSuccessView("helloView");

In above code helloView is converted to WEB-INF/jsp/helloView.jsp as per prefix and suffix rule that is defined in dispatcher-servelt.xml at viewResolver.

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

Make sure you have helloView.jsp under WEB-INF/jsp folder. Same thing applies for hello as well.

As per configuration you have defined controller for index.htm. Apply same thing for hello.htm if you don't want spring InternalResourceViewResolver to come in picture.

<prop key="index.htm">indexController</prop>
Braj
  • 46,415
  • 5
  • 60
  • 76
  • helloView.jsp is under WEB-INF/jsp/helloView.jsp – Greg86 Jul 19 '14 at 18:32
  • where is `hello.htm`? – Braj Jul 19 '14 at 18:34
  • In the tutorial there is no hello.htm, and the project is running without... [project files: ](http://imagizer.imageshack.us/v2/600x480q90/673/6a5c12.png) – Greg86 Jul 19 '14 at 18:53
  • project structures are looking exactly same it's some where in code. when are you getting this problem? – Braj Jul 19 '14 at 18:55
  • Directly, when the navigator is loading the first page – Greg86 Jul 19 '14 at 19:05
  • It means you are accessing it `/index.htm` or `/`? As per welcome file in `web.xml` it will redirect to `redirect.jsp`. So many jsps that confusing me. – Braj Jul 19 '14 at 19:06
  • He is my redirect.jsp :`<%-- Views should be stored under the WEB-INF folder so that they are not accessible except through controller process. This JSP is here to provide a redirect to the dispatcher servlet but should be the only JSP outside of WEB-INF. --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% response.sendRedirect("index.htm"); %>` – Greg86 Jul 19 '14 at 19:14
  • why `index.htm` is not directly placed under welcome file list in `web.xml`? – Braj Jul 19 '14 at 19:29
  • To show how to redirect a page...? It has no impact on running the project, I tried both... – Greg86 Jul 20 '14 at 08:11