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

发布于 2024-09-26 10:29:52 字数 2566 浏览 3 评论 0原文

我正在尝试运行第一个 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

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

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

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

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

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:

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦境 2024-10-03 10:29:52

这是因为 web.xml 中的 太“宽”。 /* 值表示 servlet 配置为接收所有请求,其中包括从 servlet 到 JSP 的请求。您看到的错误消息来自 DispatcherServlet,它正在接收自己转发的请求。

您应该选择更具体的 ,例如 /xyz/*,以便您的 URL然后变成 http://localhost:8080/grapevine/xyz/test ,然后它应该可以正常工作。

This is because the <url-pattern> in your web.xml is 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.

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.

顾铮苏瑾 2024-10-03 10:29:52

只需将 /* 替换为 / 作为您的 url-pattern 即可。它会起作用...

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

落墨 2024-10-03 10:29:52

解决方案 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>

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

<mvc:default-servlet-handler/> 

将其添加到您的 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>

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

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

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

<mvc:default-servlet-handler/> 

And this to your web.xml file:

<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 /*.

欢烬 2024-10-03 10:29:52

注意:这可能是误导性的错误消息。这只是发生在我身上。

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

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

或文件路径本身的拼写错误。

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

顺便说一句,我正在使用 Spring 4.0.0 版本。

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:

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

满栀 2024-10-03 10:29:52

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文