1
votes

I am new to Spring MVC and want to know how actual flow works.

I found few example of Spring MVC and generally every example has extra redirection ie in web.xml, welcome-file tag will send the control to some jsp file which basically does redirection to login form(for example) which is mapped with some domain.

I am following Example in this link, http://www.dzone.com/tutorials/java/spring/spring-simple-form-controller-1.html

web.xml

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>

redirect.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("userRegistration.htm"); %>

So, to avoid this redirection, I changed my mappings, Case 1

web.xml

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

mvc-dispatcher-servlet.xml

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

<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
    <props>
        <prop key="/">show</prop>
    </props>
</property>
</bean>

<bean id="show" class="com.jft.common.controller.HelloWorldController">

HelloWorldController.java

public HelloWorldController(){
    setCommandClass(Contact.class);
    setCommandName("customerForm");
    setFormView("index");
}

In this case it is not working, and in logs it displays as

No mapping found for HTTP request with URI [/HelloWorldMVC/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'mvc-dispatcher'

Now, if I add that extra redirection and make modification to my files like

Case 2

web.xml

<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

redirect.jsp

<% response.sendRedirect("register.html"); %> in redirect

and make my dispatcher servlet xml file as

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

<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
    <props>
        <prop key="/register.html">show</prop>
    </props>
</property>
</bean>

<bean id="show" class="com.jft.common.controller.HelloWorldController">

show

Everything works in this case.

what is happening in previous case, that after finding formView name again it is going inside dispatcher servlet to finding mapping for /HelloWorldMVC/WEB-INF/jsp/index.jsp.

In Case 1, What I am getting is,

I kept url pattern as /* for dispatcher servlet, so first request which will come as "localhost:8080/HelloWorldMVC/"; will be intercepted and I have mapping for that show , which goes to controller and get the page ie "index" and after applying prefix and suffix it becames "/HelloWorldMVC/WEB-INF/jsp/index.jsp", now again it is searching mapping for this request, my question is why it is searching again? which is not happening when I go through redirection way.

3

3 Answers

0
votes

Add mapping for 'index.jsp'

<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
    <props>
        <prop key="/index.jsp">show</prop>
    </props>
</property>
</bean>

In web.xml you have defined dispatcher servlet

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

Which means send every request to spring dispatcher servlet
while in simpleUrlMapping (it helps dispatcher servlet to find out mappings) you should provide specific mappings instead of mapping / i.e. mapping all requests.

0
votes

In your first case in redirect.jsp you are redirecting your request to "userRegistration.htm" so Spring will try to find mapping from key "userRegistration.htm" as you defined SimpleUrlMapping but it cant find any mapping with name "userRegistration.htm" as you specified key"/". If you changed this key value to "userRegistration.htm" than it will work

In your second case you changed redirect page to "register.html" and Spring will able to find key mapping in file as you mentioned this key="register.html". So it will map key to controller and run successfully.

Hope this is clear.

0
votes

**

call page with following extension

**

<a href="finalPage.htm">Demo</a>

as mapped in web.xml

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

Controller

@RequestMapping(value = "finalPage.htm", method = RequestMethod.GET)
public String finalPage() 
{
   return "final";
}