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.