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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3878957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:48:51  来源:igfitidea点击:

Basic Spring MVC config: PageNotFound using InternalResourceViewResolver

javaspringspring-mvcurl-pattern

提问by Marty Pitt

I'm trying to get a first Spring 3 MVC setup running.

我正在尝试运行第一个 Spring 3 MVC 设置。

My app is running on tomcat, with in the server context of "grapevine"

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

For the purposes of testing, I'm trying to get requests from http://localhost:8080/grapevine/testto render the contents of WEB-INF/jsp/noSuchInvitation.jsp

出于测试的目的,我正在尝试获取请求http://localhost:8080/grapevine/test以呈现WEB-INF/jsp/noSuchInvitation.jsp

When I try this, I'm getting a 404, and the logs suggest that my jsp isn't present:

当我尝试这个时,我得到一个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'

I must have mis-configured this somewhere, but I can't see what I've done wrong.

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

Here's all the relevant snippets.

这是所有相关的片段。

Web.xml:

网页.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>

From my context:

从我的上下文来看:

<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:

控制器:

@Controller
public class ParticipantInvitationController {

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

Log:

日志:

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

回答by skaffman

This is because the <url-pattern>in your web.xmlis too "wide". A value of /*means that the servlet is configured to receive all requests, and that includes the request from the servlet to the JSP. The error message you're seeing is from DispatcherServlet, which is receiving its own forwarded request.

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

You should pick a more specific <url-pattern>, e.g. <url-pattern>/xyz/*</url-pattern>, so that your URL then becomes http://localhost:8080/grapevine/xyz/test, and then it should work fine.

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

回答by Jomon

Just replace /*to /as your url-pattern. It will work...

只需将/*to替换/为您的url-pattern. 它会工作...

回答by Nacho Coloma

Solution 1: You can register your servlet at *.html and *.json (or xml, gif, png...):

解决方案 1:您可以在 *.html 和 *.json(或 xml、gif、png...)处注册您的 servlet:

<servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/</url-pattern>
  <url-pattern>*.html</url-pattern>
  <url-pattern>*.json</url-pattern>
 </servlet-mapping>

Solution 2: If you want to keep your servlet mapped at /*, add the following to your spring.xml file:

解决方案 2:如果要将 servlet 映射到/*,请将以下内容添加到 spring.xml 文件中:

<mvc:default-servlet-handler/> 

And this to your web.xml file:

这到您的 web.xml 文件:

<servlet-mapping>
  <servlet-name>jsp</servlet-name>
  <url-pattern>/WEB-INF/jsp/*</url-pattern>
 </servlet-mapping>

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>

The rationale is explained here: spring, web.xml. This will register an explicit handler for JSP pages with greater precedence than /*.

这里解释了基本原理:springweb.xml。这将为 JSP 页面注册一个显式处理程序,其优先级高于/*.

回答by David Lotts

BEWARE: This could be a misleading error message. It just happened to me.

当心:这可能是一个误导性的错误消息。它只是发生在我身上。

Even thought the error message unexpectedly contains the /ContextName/... at the beginning of the path, it could still be a misspelling in either the InternalResourceViewResolver prefix:

即使认为错误消息在路径的开头意外包含 /ContextName/... ,它仍然可能是 InternalResourceViewResolver 前缀中的拼写错误:

<property name="prefix" value="/WEB-INF-typo-Here/jsp/"/>

or the file path itself.

或文件路径本身。

Now that I fixed my misspelling, it works fine. I don't know why the context shows in the error message, and it really caused me to ignore my silly typo and attempting to try the wonderful other contributions to this question. Don't let it happen to you!

现在我修正了我的拼写错误,它工作正常。我不知道为什么错误消息中会显示上下文,这确实让我忽略了我愚蠢的错字并尝试尝试对该问题的其他精彩贡献。不要让它发生在你身上!

BTW, I am using Spring 4.0.0 release.

顺便说一句,我使用的是 Spring 4.0.0 版本。

回答by appsthatmatter

for me I solved the problem by using .jsp templates and not just .html.

对我来说,我通过使用 .jsp 模板而不仅仅是 .html 解决了这个问题。