有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在DispatcherServlet中找不到URI为的HTTP请求的java Spring MVC映射

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

INFO: Server startup in 4792 ms

WARNING: No mapping found for HTTP request with URI [/TestSpringMVC/] in DispatcherServlet with name 'HelloWeb'

WARNING: No mapping found for HTTP request with URI [/TestSpringMVC/index.html] in DispatcherServlet with name 'HelloWeb'

这是我的欢迎html页面:

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

这是我的家。html页面:

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

这是我的网站。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>

hellowebservlet。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


共 (3) 个答案

  1. # 1 楼答案

    你需要做几件事。 1.您的HelloWeb-servlet.xml缺少<mvc:annotation-driven />。这是注册处理程序映射和处理程序适配器所必需的

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

    二,。需要在控制器中更正视图分辨率。因为您已经在视图解析器中定义了/WEB-INF/page/html/,所以在控制器中不需要它

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

    假设您的tomcat侦听端口8080,web上下文为TestSpringMVC,下面是您可以尝试的URL

    http://localhost:8080/TestSpringMVC/home
    
  2. # 2 楼答案

     <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
    
  3. # 3 楼答案

    您的servlet。xml有以下内容:<property name="prefix" value="/WEB-INF/page/html/"/>

    您将在控制器中返回"page/html/home",因此本质上与page/html/page/html/home相同

    你只是想return "home"

    此外,您还需要webroot的映射,例如

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