2016-02-28 338 views
1

這裏是我的代碼:在DispatcherServlet中找不到具有URI的HTTP請求的映射?

控制器類:

@RequestMapping(value="/", method = RequestMethod.GET) 
public String searchMovie(ModelMap model){ 

    return "static/html/search.html"; 
} 

的servlet-context.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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.xsd http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 

    <mvc:annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <!-- <resources mapping="/resources/**" location="/resources/" />--> 

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


    <mvc:resources mapping="/static/**" location="/WEB-INF/static/" /> 
    <context:component-scan base-package="com.hellomvc.myapp" /> 



</beans:beans> 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

我的文件結構的HTML文件:

File Structure

控制檯輸出,當我嘗試運行.html文件:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp/WEB-INF/views/static/html/search.html.jsp] in DispatcherServlet with name 'appServlet' 

有人能幫助我如何讓我的* .html文件的工作?

回答

0

我看見你註釋掉:

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

所以,你還沒有定義任何視圖解析器!

+0

不,我沒有。因爲靜態HTML頁面不需要這種類型的任何東西。 – Vamsi

0

將帶有前綴和後綴(html)的視圖解析器定義到servlet-context.xml中。之後返回沒有.html的String。

相關問題