【问题标题】:org.springframework.web.servlet.DispatcherServlet noHandlerFound (No mapping found)org.springframework.web.servlet.DispatcherServlet noHandlerFound(未找到映射)
【发布时间】:2012-06-28 09:03:22
【问题描述】:

我的 jsps 在 WEB-INF/jsp/ 下,下面是我的 web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Checkout</display-name>

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

  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>


</web-app>

这是我试图访问的页面 product.jsp 的映射:

@Controller
@RequestMapping("/product.action")
public class ProductController {

    /**
     * Show the product selection form
     * 
     * @return
     */
    @RequestMapping(method=RequestMethod.GET)
    public String get() {
        return "products.jsp";
    }

}

当尝试从以下链接访问页面时:

http://localhost:8080/myapp/product.action

我在浏览器中收到404,我在控制台中收到以下警告:

Jun 28, 2012 10:55:23 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/product.action] in DispatcherServlet with name 'myservlet'

我在配置中遗漏了什么吗? 请指教,谢谢。

编辑:我尝试将 viewResolver bean 添加到 applicationContext,但没有成功:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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



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


</beans>

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    遵守Sunil 提到的任何规则。我在您的 spring 配置 xml 中看到一个问题,即您没有

    <mvc:annotation-driven />  
    

    你需要这个来注册Controller

    &lt;context:component-scan base-package="com.myapp"/&gt;

    【讨论】:

      【解决方案2】:

      当您指定 RequestMapping 时,URI 不应有扩展名。 Dispatcher servlet 将在搜索 URI 映射时从请求 URI 中省略它。

      使用@RequestMapping("/product") 应该可以工作。

      当您使用视图解析器时,第二件事只是返回 JSP 文件的名称。不要附加 .jsp 扩展名,InternalViewResolver 会为您完成。

      【讨论】:

      • 我试过了,它也不起作用,我是否需要更改配置或 web.xml 映射中的任何内容?
      • 它给了我404: description The requested resource (/subscriptions/product) is not available.
      • 何时收到此错误?看起来您的控制器处理程序方法已被调用,但无法找到返回的视图。如果是这样,当您返回视图名称时,它必须是相对路径。
      • 我在浏览器中输入链接并按回车时收到此错误,您建议怎么做?
      • 您提到资源'/subscriptions/product'的404错误,我想了解为什么路径包含'/subscriptions'?它在代码中的任何地方都没有提到。底线检查两件事 - 1. 您的控制器方法是否使用 URL localhost:8080/myapp/product.action 调用?您可以通过在控制台上显示一些消息来检查。 2. 处理方法返回的视图名称应该是WEB-INF的相对路径。例如在上面的 404 错误中,product.jsp 应该在 WEB-INF 下的 'subscriptions' 文件夹中,并且处理程序方法应该返回 /subscriptions/product
      【解决方案3】:

      问题是未检测到控制器。 我将基础包从com.myapp 更改为com.myapp.controller,现在可以正常使用了。

      【讨论】:

        【解决方案4】:

        使用这个 class="org.springframework.web.servlet.view.UrlBasedViewResolver" 而不是 class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        在您的应用程序上下文 bean 中。

        【讨论】:

          【解决方案5】:

          如果您在上下文 xml 中使用 viewResolver,您应该将 get 方法返回声明更改为“产品”并确保文件夹层次结构正确

          【讨论】:

            【解决方案6】:
            1. 将此&lt;url-pattern&gt;*.action&lt;/url-pattern&gt; 更改为&lt;url-pattern&gt;/*.action&lt;/url-pattern&gt;

            2. 学习其他教程时检查.xml文件中的基础包名称

            希望它能正常工作

            【讨论】:

              【解决方案7】:

              此错误的解决方法: org.springframework.web.servlet.DispatcherServlet noHandlerFound(未找到映射)

              只需按照以下步骤操作:

              右键单击项目>构建路径>配置构建路径>部署程序集>添加(右侧)>文件夹>添加您的视图文件夹>应用并关闭

              【讨论】:

                【解决方案8】:

                检查包名 spring-servlet.xml 是否拼写正确。这可能是问题所在。我在启动 Spring MVC 时遇到了

                【讨论】:

                  猜你喜欢
                  • 2020-05-11
                  • 1970-01-01
                  • 2014-01-31
                  • 2018-02-18
                  • 1970-01-01
                  • 2019-02-26
                  • 2013-10-09
                  • 2020-02-04
                  • 1970-01-01
                  相关资源
                  最近更新 更多