与glassfish和春天的URL映射 [英] URL mapping with glassfish and spring
问题描述
我正在为glassfish 2.1创建一个hello world spring项目。我正在尝试使用SimpleUrlHandlerMapping将以/spring/*.htm结尾的所有内容映射到我的弹簧控制器。这里是我的:
web.xml:
< ;?xml version =1.0encoding =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>测试MVC项目< /显示名称>
< servlet>
< servlet-name>调度程序< / servlet-name>
< servlet-class>
org.springframework.web.servlet.DispatcherServlet
< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name>调度程序< / servlet-name>
< url-pattern> / spring / *< / url-pattern>
< / servlet-mapping>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< / welcome-file-list>
< / web-app>
在我的dispatcher-servlet.xml文件中:
< 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 =viewNamevalue =WEB-INF / jsp / springbean-view.jsp/>
< / bean>
< bean id =urlMapping
class =org.springframework.web.servlet.handler.SimpleUrlHandlerMapping>
< property name =mappings>
<道具>
< prop key =*。htm> defaultController< / prop>
< /道具>
< / property>
< / bean>
< / beans>
我的sun-web.xml文件:
<太阳web应用程序>
< context-root> / foo< / context-root>
< class-loader delegate =false/>
< / sun-web-app>
当我请求 http:// localhost:9680 / foo ,我看到欢迎页面。当我请求 http:// localhost:9680 / foo / spring / test.htm 时,我得到了一个404。
我在servlet.xml文件的urlMapping中尝试了许多模式变体。我得到的唯一工作是如果我这样做:
< bean id =urlMapping
类= org.springframework.web.servlet.handler.SimpleUrlHandlerMapping >
< property name =mappings>
<道具>
< prop key =/ *> defaultController< / prop>
< /道具>
< / property>
< / bean>
当我请求 http:// localhost:9680 / foo / spring ,它的工作原理。对我来说,这意味着我的web.xml是正确的,因为它正确地将请求传递给调度servlet。当我请求 http:// localhost:9680 / foo / spring / test.htm (或任何在/春/ *结束时,我得到一个500与此异常:
javax.servlet.ServletException:PWC1232:超过最大深度嵌套的请求调度:20
我觉得我在做一些非常愚蠢的事情,但经过几个小时的谷歌搜索和玩耍与映射,我很难过。
< prop key =/ *> defaultController< / prop>
但是,这可以更简单:
< bean id =urlMappingclass = org.springframework.web.servlet.handler.SimpleUrlHandlerMapping >
将属性名= 的DefaultHandler REF = defaultController/>
将/豆腐>
另外,原因哟你会得到无尽的转发循环:
< bean id =defaultControllerclass =org.springframework。 web.servlet.mvc.ParameterizableViewController>
< property name =viewNamevalue =WEB-INF / jsp / springbean-view.jsp/>
< / bean>
这里的问题是没有前置 /
在 viewName
上,所以它被解释为相对的。因此,当您转到以 /foo/spring/test.htm
结尾的URL时,相对的转发URI是 / foo / spring / WEB-INF /jsp/springbean-view.jsp
,后者又被 web.xml中的
url-pattern
/ code>,所以它会回到 DispatcherServlet
,它会转发它等等等等。无尽的循环。
$ p
$ b
< property name =viewNamevalue =/ WEB-INF / jsp / springbean-view.jsp/>
请注意前面的 /
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 /
这篇关于与glassfish和春天的URL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!