URL mapping with glassfish and spring
I'm creating a hello world spring project on glassfish 2.1. I'm trying to use the SimpleUrlHandlerMapping to map everything that ends in /spring/*.htm to my spring controller. Here is what I have:
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Test MVC Project</display-name>
<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>/spring/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
in my dispatcher-servlet.开发者_开发技巧xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="defaultController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="WEB-INF/jsp/springbean-view.jsp" />
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.htm">defaultController</prop>
</props>
</property>
</bean>
</beans>
My sun-web.xml file:
<sun-web-app>
<context-root>/foo</context-root>
<class-loader delegate="false" />
</sun-web-app>
When I request http://localhost:9680/foo, I see the welcome page. When I request http://localhost:9680/foo/spring/test.htm, I get a 404.
I have tried many variations of patterns in the urlMapping in the servlet.xml file. The only thing I have gotten to work is if I do this:
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*">defaultController</prop>
</props>
</property>
</bean>
When I request http://localhost:9680/foo/spring, it works. To me, this means that my web.xml is correct because it is properly passing the request to the dispatch servlet. When I request http://localhost:9680/foo/spring/test.htm (or anything that ends in /spring/*, I get a 500 with this exception:
javax.servlet.ServletException: PWC1232: Exceeded maximum depth for nested request dispatches: 20
I feel like I'm doing something very stupid, but after several hours of googling and playing around with the mappings, I'm stumped.
This is almost correct:
<prop key="/*">defaultController</prop>
But that can be simpler still:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler" ref="defaultController"/>
</bean>
Also, the reason you're getting the endless forwarding loop is this:
<bean id="defaultController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="WEB-INF/jsp/springbean-view.jsp" />
</bean>
The problem here is there's no leading /
on the viewName
, so it's interpreted as relative. So, when you go to the URL ending /foo/spring/test.htm
, the relative forward URI is /foo/spring/WEB-INF/jsp/springbean-view.jsp
, which in turn is caught by the url-pattern
in web.xml
, so it goers back into the DispatcherServlet
, which forwards it on, etc etc etc. Endless loop.
Easy fix:
<property name="viewName" value="/WEB-INF/jsp/springbean-view.jsp" />
Note the leading /
精彩评论