java - Spring MVC 配置 url-pattern

标签 java jsp spring-mvc http-status-code-404


我尝试配置简单的 Controller 。

我有:
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" />

这是我的 Controller :

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

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

它返回 404 访问/index.jsp

我在控制台中看到“It is here”,这意味着 url-pattern 工作正常但 ModelAndView 未初始化

奇怪的是,看起来他试图访问空资源 (Chrome 向我显示“HTTP 状态 404 -”)

请帮助我了解发生了什么。可能是我在 url-pattern 规范中遗漏了什么?

更新:
感谢 Pavel Horal,找到了解决方案。
我只是用

替换了 web.xml 中的 url-pattern
<url-pattern>/test/*</url-pattern>

现在它通过/test/index.jsp 响应

最佳答案

Spring 正在处理如何定义 servlet 映射的信息。如果您使用后缀映射 (*.something),则 Spring 仅使用第一部分(不带后缀)。这意味着您应该在您的 url 模式中仅映射 /index(没有后缀)。

Spring 的 UrlPathHelper#getPathWithinServletMapping 中的 JavaDoc 更好地描述了映射过程中使用的内容:

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.

Detects include request URL if called within a RequestDispatcher include.

E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".

E.g.: servlet mapping = "/test"; request URI = "/test" -> "".

E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".

关于java - Spring MVC 配置 url-pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988689/

相关文章:

java - Java 中的 while 循环后代码未运行

java - Tomcat java.lang.ClassNotFoundException 异常

java - Spring数据绑定(bind)(@modelattribute)优雅地处理解析异常

java.security.AccessControlException : access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.reflect.annotation") Spring

java - 如何加速 Tomcat SSL 初始化

android - 在ubuntu中使用eclipse做android项目必须使用Sun Java吗?我已经安装了 OpenJDK

java - Okhttp java 11 异常

java - 如何在关闭浏览器时从数据库中删除数据

java - 单击提交按钮时,我试图在文本框中生成随机数

java - @RequestBody 返回的 Marshall 对象