• No mapping found for HTTP request with URI [/spring/WEB-INF/page/index.jsp] in DispatcherServlet with name 'spring'


    今天试了下spring mvc遇到个很奇怪的事情,按照书上配置访问时总是出现如下警告:

    org.springframework.web.servlet.DispatcherServlet noHandlerFound
    警告: No mapping found for HTTP request with URI [/myspring/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'spring'

    web.xml

    Xml代码

    1. <servlet>  
    2.   <servlet-name>spring</servlet-name>  
    3.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    4.   <init-param>  
    5.      <param-name>contextConfigLocation</param-name>  
    6.      <param-value>/WEB-INF/springContext.xml</param-value>  
    7.   </init-param>  
    8. </servlet>  
    9.   
    10. <servlet-mapping>  
    11.   <servlet-name>spring</servlet-name>  
    12.   <url-pattern>/*</url-pattern>  
    13. </servlet-mapping>  

    <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

     springContext.xml

    Xml代码

    1. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    2.     <property name="prefix" value="/WEB-INF/jsp/" />  
    3.     <property name="suffix" value=".jsp" />  
    4.  </bean>  
    5.        
    6.  <bean name="/hc"  class="com.hj.controllers.HelloController"/>  

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

       HelloController.java

    Java代码

    1. @Override  
    2. protected ModelAndView handleRequestInternal(HttpServletRequest request,  
    3.         HttpServletResponse response) throws Exception {  
    4.   
    5.     return new ModelAndView("hello");  
    6. }  

    @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("hello"); }

       在项目/WEB-INF/jsp目录下防止hello.jsp文件。按照上述配置访问http://localhost:8081/myspring /hc,应该是出现hello.jsp内容才对,但是却出现了一开始提到的警告。我搜索了许多地方,结果在一老外的提供中找到了同样的问题(http: //forum.springsource.org/archive/index.php/t-71263.html),解决方法老外也说明了,只是不知 道为什么。

      就是将web.xml中

    Xml代码

    1. <servlet-mapping>  
    2.     <servlet-name>spring</servlet-name>  
    3.     <url-pattern>/*</url-pattern>  
    4.   </servlet-mapping>  

    <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

      改成

    Java代码

    1. <servlet-mapping>  
    2.     <servlet-name>spring</servlet-name>  
    3.     <url-pattern>/</url-pattern>  
    4.   </servlet-mapping>  

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

      

    spring MVC的困惑--url-pattern的/和/*有区别

    Spring MVC 遇到的一点点问题

  • 相关阅读:
    PKUSC2021游记
    P3349 [ZJOI2016]小星星
    序二
    1.3 解析库的安装
    1.2 请求库的安装
    1.5 存储库的安装
    1.6 Web 库的安装
    1.7 App 爬取相关库的安装
    2.1 HTTP 基本原理
    1.9 部署相关库的安装
  • 原文地址:https://www.cnblogs.com/passer1991/p/3228375.html
Copyright © 2020-2023  润新知