【问题标题】:Spring MVC No mapping found for HTTP request with URI in DispatcherServlet [duplicate]Spring MVC在DispatcherServlet中找不到带有URI的HTTP请求的映射[重复]
【发布时间】:2015-06-03 20:22:54
【问题描述】:

我写网站,我必须使用 html 页面。当我使用 jsp 页面编写时,我得到了很好的结果,但是我不使用 HTML 页面,因为我得到了错误:

信息:服务器在 4792 毫秒内启动

警告:在名称为“HelloWeb”的 DispatcherServlet 中找不到带有 URI [/TestSpringMVC/] 的 HTTP 请求的映射

警告:在名称为“HelloWeb”的 DispatcherServlet 中找不到带有 URI [/TestSpringMVC/index.html] 的 HTTP 请求的映射

这是我的欢迎 html 页面:

<!DOCTYPE html>
<html>
  <head>
   <meta charset="UTF-8">
   <title>Insert title here</title>
  </head>
  <body>
   <a href="home">click</a>
  </body>
</html>

这是我的 home.html 页面:

<body>
  <h1>this is home page</h1>
</body>

这是我的 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>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
  </context-param>

  <listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>

   <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>/</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"   
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans     
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.1.xsd
 http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="main.test.spring" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/page/html/"/>
    <property name="suffix" value=".html"/>
</bean>

我的控制器:

    @Controller
    public class HelloController{
       @RequestMapping(value = "/home", method = RequestMethod.GET)
       public String printHellow() {
          return "page/html/home";
       }
    }

附言我使用Tomcat 7

【问题讨论】:

  • 首先从浏览器http://localhost:8080/TestSpringMVC/home尝试,看看它是否命中了控制器。
  • 当我去http://localhost:8080/TestSpringMVC/home我有HTTP Status 404 description The requested resource is not available.
  • 你的tomcat服务器运行在8080端口只有ri8?接下来将return "page/html/home"; 更改为return "home";

标签: java html spring spring-mvc


【解决方案1】:

您需要做几件事情。 1.您的HelloWeb-servlet.xml 丢失&lt;mvc:annotation-driven /&gt;。这是注册处理程序映射和处理程序适配器所必需的。

    <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"   
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans     
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
     http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    <context:component-scan base-package="main.test.spring" />
<mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/html/"/>
        <property name="suffix" value=".html"/>
    </bean>`

2。您的视图分辨率需要在控制器中更正。由于您在视图解析器中定义了/WEB-INF/page/html/,因此您的控制器中不需要它。

@Controller
    public class HelloController{
       @RequestMapping(value = "/home", method = RequestMethod.GET)
       public String printHellow() {
          return "home";
       }
    }

假设你的 tomcat 监听端口 8080 并且 web context 是 TestSpringMVC,这里是你可以尝试的 URL。

http://localhost:8080/TestSpringMVC/home

【讨论】:

    【解决方案2】:

    你的 servlet.xml 有这个:&lt;property name="prefix" value="/WEB-INF/page/html/"/&gt;

    并且您在控制器中返回"page/html/home",因此基本上与page/html/page/html/home 相同。

    你只是想return "home"

    您还需要为您的 webroot 进行映射,例如

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
    
        return "home";
    }
    

    【讨论】:

    • 我更改了代码并收到了结果WARNING: No mapping found for HTTP request with URI [/TestSpringMVC/] in DispatcherServlet with name 'HelloWeb'
    • 为什么要使用 TestSpringMVC?当您尝试访问 /HelloWeb/ 时会发生什么?
    • 我的页面出现错误 404
    【解决方案3】:
     <url-pattern>/*.html</url-pattern>
    
     to add different type you need to add the type such as above code, and if you  
     were to add json then /*json should be added as well .Hope that solves your 
     problem
    

    【讨论】:

    • 如果我添加 /*.html 我有错误SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TestSpringMVC]]
    • 在您的 WEB-INF 文件夹中将您的视图文件保留为 .html 或 ,jsp ?
    • 这个错误很笼统,不是吗?您是否尝试过 InternalResourceViewResolver 而不是 UrlBaseViewResolver?
    • 在我的 WEB-INF 我有 .html 文件。不,我没有尝试过 InternalResourceViewResolver
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2017-09-17
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多