Spring MVC配置url-pattern [英] Spring MVC configure url-pattern
问题描述
我尝试配置简单的控制器。
我有: web中的
.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
mvc-dispatcher-servlet.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/index.jsp">
<ref bean="mainPage"/>
</entry>
</map>
</property>
</bean>
<bean name="mainPage" class="ru.mypack.TBController" />
这是我的控制器:
public class TBController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
System.out.println("It is here");
ModelAndView model = new ModelAndView("index");
return model;
}}
我在Tomcat 6上运行并在此配置中(/ index。 jsp)工作得很完美!
但如果我改变这样的url-pattern
I run in on Tomcat 6 and in this configuration it (/index.jsp) works perfect!
But if I change url-pattern like this
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
它返回404访问/index.jsp
我在控制台中看到它在这里,这意味着url-pattern工作正常但是ModelAndView没有初始化
奇怪的是它看起来像是试图访问空资源
(Chrome让我觉得HTTP状态404 - )
it returns 404 accessing /index.jsp
I see "It is here" in console, it means url-pattern works fine but ModelAndView doesn't get initialized
Strange thing is that it looks like he tries to access empty resource
(Chrome dysplays me "HTTP Status 404 - ")
请帮我理解发生了什么..可能是我错过了url-pattern规范中的内容?
Please, help me to understand what is going on.. May be I missed something in url-pattern specification?
UPD:
感谢Pavel Horal,找到了解决方案。
我刚用web.xml替换了我的url-pattern
UPD:
Thanks to Pavel Horal, the solution has been found.
I just replaced my url-pattern in web.xml with
<url-pattern>/test/*</url-pattern>
现在它通过/test/index.jsp回复
And now it responds by /test/index.jsp
推荐答案
Spring正在处理如何定义servlet映射的信息。如果您使用后缀映射( *。something
),则Spring仅使用第一部分(不带后缀)。这意味着你在你的url-pattern中没有地图 / index
(没有后缀)。
Spring is working with the information how the servlet mapping is defined. If you are using suffix mapping (*.something
), then Spring uses just the first part (without the suffix). This means you shuold map just /index
in your url-pattern (without the suffix).
Spring中的JavaDoc UrlPathHelper#getPathWithinServletMapping
更好地描述了映射过程中使用的内容:
JavaDoc in Spring's UrlPathHelper#getPathWithinServletMapping
gives a better description what is being used in the mapping process:
返回给定请求的servlet映射中的路径,即
请求URL的一部分超出调用servlet的部分,
或如果整个URL已用于标识servlet 。
Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.
如果在RequestDispatcher
include中调用,则检测包含请求URL。
Detects include request URL if called within a RequestDispatcher include.
例如:servlet映射=/ test / *;请求URI =/ test / a - >/ a。
E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".
例如:servlet mapping =/ test;请求URI =/ test - >。
E.g.: servlet mapping = "/test"; request URI = "/test" -> "".
例如:servlet mapping =/ *。test;请求URI =/ a.test - >。
E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".
这篇关于Spring MVC配置url-pattern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!