【问题标题】:No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]未找到带有 URI [/HelloWeb/WEB-INF/jsp/hello.jsp] 的 HTTP 请求的映射
【发布时间】:2013-08-14 18:23:14
【问题描述】:

我尝试使用简单的 jsp 加载 spring mvc 的简单示例,但失败并出现以下错误:No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello .jsp] 我正在使用 Spring 3.2.2。 我真的很感谢你在这里的帮助。 我尝试了几件事,但似乎我无法在控制器中映射我的 jsp 页面。

web.xml 看起来像这样:

<web-app id="WebApp_ID" version="2.4"
               xmlns="http://java.sun.com/xml/ns/j2ee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>

  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>
</web-app>

HelloWeb-servlet.xml 看起来像这样:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation=" http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.tutorial.spring" />

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

HelloControll.class 看起来像这样:

package com.tutorial.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HelloController {
  @RequestMapping(method= RequestMethod.GET)
  public String printHello(ModelMap modelMap) {
    modelMap.addAttribute("message", "Hello Spring MVC Framework!");

    return "hello";
  }
}

【问题讨论】:

  • 您是如何使用 web.xml 中的 contextConfigLocation 来加载 Spring XML 文件的?

标签: spring spring-mvc


【解决方案1】:

点击printHello 处理程序方法的正确 URL 是

/HelloWeb/hello.jsp 

【讨论】:

    【解决方案2】:

    我想通了。愚蠢的错误。 我将我的 jsp 命名为“Hello.jsp”,大写字母为“H”,并在控制器中返回小写字母“hello”。

    刚刚将我的 jsp 页面更改为“hello.jsp”。

    感谢您的帮助。

    【讨论】:

    • 请接受您的回答。它将帮助像我这样的人理解:)
    【解决方案3】:

    像这样修改web.xml

      <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    尝试访问 /YourAppName/hello。然后 Spring-MVC 将触发方法 printHello() 。然后返回/WEB-INF/jsp/中的jsp视图。

    【讨论】:

    • 谢谢,我试过了,在浏览器中出现“HTTP Status 404 - /HelloWeb/hello.jsp”错误,在 catalina 日志中没有特殊错误。
    • 浏览器也报错说:“说明请求的资源不可用。”即使它可用。我在这里错过了什么吗?
    • 您访问的 URL 是什么?请张贴。
    • 使用这个URL localhost:8080/HelloWeb/hello,spring-mvc 自动返回jsp视图“hello.jsp”
    【解决方案4】:

    调度器 servlet 必须映射正确的主路由:

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    【讨论】:

    • 谢谢,我试过了,在浏览器中出现“HTTP Status 404 - /HelloWeb/hello.jsp”错误,并且在 catalina 日志中没有特殊错误。有没有办法调试它?
    • 你知道我该如何调试它吗?
    • 您映射的是 /HelloWeb/hello 而不是 /HelloWeb/hello.jsp。尝试在您可以执行此操作的方法中添加 requestMapping 值:@Controller public class HelloController { @RequestMapping(value="/hello", method= RequestMethod.GET) public String printHello(ModelMap modelMap) { ,所以现在您正在映射 /HelloWeb/hello,或者您可以这样做:@Controller @RequestMapping("/hello") public class HelloController { @RequestMapping(value="/", method= RequestMethod.GET) public String printHello(ModelMap modelMap) { ,您现在正在映射 / HelloWeb/hello/
    • 嗨,Emanuele,我尝试了这两种方法,但在 catalina 日志中发现以下异常:在 DispatcherServlet 中找不到带有 URI [/HelloWeb/Hello.jsp] 的 HTTP 请求的映射,名称为“HelloWeb”。尽管如此,我还是收到了 HelloWeb 正在部署的消息。
    • 您是否尝试过请求uri /HelloWeb/hello(第一种情况)或/HelloWeb/hello/(第二种情况)?
    【解决方案5】:

    将 jsp 文件移动到 servlet 中定义的正确位置

    /WEB-INF/jsp/

    【讨论】:

      【解决方案6】:

      正确的 URL 是http://localhost:8080/hello,没有“/HelloWeb”

      【讨论】:

        猜你喜欢
        • 2016-07-02
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 2017-07-16
        • 1970-01-01
        相关资源
        最近更新 更多