Basic Spring MVC配置:使用InternalResourceViewResolver的PageNotFound
我正在尝试运行第一个Spring 3 MVC设置。
我的应用程序在tomcat上运行,在“葡萄藤”的服务器环境中运行
出于测试目的,我试图从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中:
grapevine org.springframework.web.servlet.DispatcherServlet 1 grapevine /*
从我的背景来看:
控制器:
@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
这是因为web.xml
的
太“宽”。 值/*
表示servlet配置为接收所有请求,包括从servlet到JSP的请求。 您看到的错误消息来自DispatcherServlet
,它正在接收自己的转发请求。
你应该选择一个更具体的
,例如
,这样你的URL就变成了http://localhost:8080/grapevine/xyz/test
,那它应该工作正常。
只需将/*
替换为/
作为您的url-pattern
。 它会工作……
解决方案1 :您可以在* .html和* .json(或xml,gif,png …)中注册您的servlet:
RestServlet org.springframework.web.servlet.DispatcherServlet RestServlet / *.html *.json
解决方案2 :如果要将servlet映射到/*
,请将以下内容添加到spring.xml文件中:
这到你的web.xml文件:
jsp /WEB-INF/jsp/* jsp org.apache.jasper.servlet.JspServlet
这里解释了基本原理: spring , web.xml 。 这将为JSP页面注册一个显式处理程序,其优先级高于/*
。
请注意:这可能是一个误导性错误消息。 它发生在我身上。
即使错误消息意外地在路径的开头包含/ ContextName / …,它仍然可能是InternalResourceViewResolver前缀中的拼写错误:
或文件路径本身。
现在我修复了拼写错误,它运行正常。 我不知道为什么上下文显示在错误信息中,它真的让我忽略了我的愚蠢错字并试图尝试这个问题的精彩其他贡献。 不要让它发生在你身上!
顺便说一下,我正在使用Spring 4.0.0版本。
对我来说,我使用.jsp模板而不仅仅是.html解决了这个问题。