Spring MVC基本配置:使用InternalResourceViewResolver的PageNotFound


问题内容

我正在尝试运行第一个Spring 3 MVC安装程序。

我的应用程序在tomcat上运行,服务器上下文为“ grapevine”

为了进行测试,我试图从中获取请求http://localhost:8080/grapevine/test以呈现WEB- INF/jsp/noSuchInvitation.jsp

当我尝试此操作时,出现404,并且日志表明我的jsp不存在:

WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp] in DispatcherServlet with name 'grapevine'

我一定在某处配置错误,但是看不到我做错了什么。

这是所有相关的摘要。

Web.xml:

<servlet>
    <servlet-name>grapevine</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>grapevine</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

从我的角度来看:

<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

控制器:

@Controller
public class ParticipantInvitationController {

@RequestMapping("/test")
public ModelAndView test()
{
    return new ModelAndView("noSuchInvitation");
}

日志:

DEBUG org.springframework.web.servlet.DispatcherServlet  - Rendering view [org.springframework.web.servlet.view.JstlView: name 'noSuchInvitation'; URL [/WEB-INF/jsp/noSuchInvitation.jsp]] in DispatcherServlet with name 'grapevine'
DEBUG org.springframework.web.servlet.view.JstlView  - Forwarding to resource [/WEB-INF/jsp/noSuchInvitation.jsp] in InternalResourceView 'noSuchInvitation'
DEBUG org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'grapevine' processing GET request for [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp]
WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp] in DispatcherServlet with name 'grapevine'
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository  - SecurityContext contents are anonymous - context will not be stored in HttpSession. 
DEBUG org.springframework.web.servlet.DispatcherServlet  - Successfully completed request

问题答案:

这是因为<url- pattern>web.xml的太“宽”了。值的/*意思是servlet被配置为接收所有请求,并且包括从servlet到JSP的请求。您看到的错误消息来自DispatcherServlet,它正在接收自己的转发请求。

您应该选择一个更具体的<url-pattern>,例如<url-pattern>/xyz/*</url- pattern>,这样您的URL就会变成http://localhost:8080/grapevine/xyz/test,然后就可以正常工作了。