3

I have the standard Spring 4.x files in Netbeans 8.0.2. I added a controller, RegisterController.java in the controllers-package. I also added one model, a User, with some basic information about the user. Next I made 2 .jsp files, Registration.jsp and RegistrationSuccess.jsp.

Here is my RegisterController:

@Controller
@RequestMapping(value="/register")
public class RegisterController {
    @RequestMapping(method=RequestMethod.GET)
    public String viewRegistration(Model model){
        User user = new User();
        model.addAttribute("userForm", user);

        List<String> professionList = new ArrayList<>();
        professionList.add("Developer");
        professionList.add("Designer");
        professionList.add("IT Manager");
        model.addAttribute("professionList", professionList);

        return "Registration";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> Model){
        System.out.println("username: " + user.getUsername());
        System.out.println("password: " + user.getPassword());
        System.out.println("email: " + user.getEmail());
        System.out.println("birth date: " + user.getBirthDate());
        System.out.println("profession: " + user.getProfession());

        return "RegistrationSuccess";
    }
}

Now, going to myProject/register results in a 404. I am confused how Spring manages the routing though. There is a web.xml looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.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>*.htm</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>

What I think this means is every url with *.htm goes to the dispatcher-servlet:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

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

    <!--
    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>

But where do I need to insert some entries to let my RegisterController do work?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
jdepypere
  • 3,453
  • 6
  • 54
  • 84

3 Answers3

3

Since you gave *.htm in url pattern, the dispatcher-servlet will only recognize *.htm requests. Change your web.xml

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

And above your controller, there's no need to give RequestMapping. Your methods will do the work for you

@Controller
public class RegisterController {

And above your method, you should give value so that your method can find it

@RequestMapping(value="/register.htm" method=RequestMethod.GET)
public String viewRegistration(Model model)
Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Arnab Dhar
  • 239
  • 2
  • 15
  • Don't I also need to edit my DispatcherServlet? I get a warning `No mapping found for HTTP request with URI [/SpringTest/register.htm] in DispatcherServlet with name 'dispatcher'` – jdepypere Jun 02 '15 at 19:48
  • Delete that 'urlMapping' bean too.... It will work fine without that too. You dont even need that 'indexcontroller' Add this line. and the InternalViewResolver. Thats it. Now run it – Arnab Dhar Jun 02 '15 at 20:00
  • I have updated my question with my new `dispatcher-servlet.xml`, but I'm afraid I'm still doing something wrong: `The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'.` – jdepypere Jun 02 '15 at 20:24
  • 1
    add this too...in dispatcher-servlet.xml – Arnab Dhar Jun 02 '15 at 20:42
0

According to Dispatcher Servlet mapping, your RequestMapping will go to the controller with HttpRequest with .htm extension.. In your case, you have mentioned *.htm

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

So in the controller, for the appropriate request, add like this..

@RequestMapping(value="/register.htm" method=RequestMethod.GET)
    public String viewRegistration(Model model){

and In the top of the controller, add like this,

@Controller
@RequestMapping("/")
public class RegisterController {

add this in spring.xml,

<context:component-scan base-package="RegisterController" />

including package name of Controller should be there in component-scan base-package...

Vignesh Shiv
  • 1,129
  • 7
  • 22
  • What do you mean with the `spring.xml`-file? I don't seem to have it - I have a `web.xml` and `dispatcher-servlet.xml` – jdepypere Jun 02 '15 at 19:26
  • In your case it means dispatcher-servlet.xml – Vignesh Shiv Jun 02 '15 at 19:27
  • Then I get an error that the context initialisation failed, `The prefix "context" for element "context:component-scan" is not bound.` – jdepypere Jun 02 '15 at 19:32
  • add this code snippet in your dispatcher-servlet.xml. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" – Vignesh Shiv Jun 02 '15 at 19:35
  • The next error is `The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'` unfortunately :/ – jdepypere Jun 02 '15 at 19:58
  • add like this,, – Vignesh Shiv Jun 02 '15 at 20:02
0

Edit servlet mapping in you web.xml as:

 <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
 </servlet-mapping>

And also add below piece of code in your spring configuration file:

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="your.packagename.RegisterController"></context:component-scan>
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • Adding those to my `web.xml` (which I presume is my Spring config file?) results in this error: `The prefix "mvc" for element "mvc:annotation-driven" is not bound.` – jdepypere Jun 02 '15 at 20:00
  • Not in web.xml, in your Spring config file and yes for "mvc", you have to add mvc namespace. You can refer from here : https://github.com/arpitaggarwal/Spring-Social/blob/master/src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml – Arpit Aggarwal Jun 02 '15 at 20:04