简体   繁体   中英

Spring MVC configuration considering web.xml

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.

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 / ie mapping all requests.

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.

**

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM