2016-08-19 6 views
0

Spring-MVCを通して簡単なHelloメッセージを表示しようとしています。単純なHTMLページがSpring-MVC経由でロードされない理由

のWeb.xml

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

     <welcome-file-list> 
     <welcome-file>html/homepage.html</welcome-file> 

    </welcome-file-list> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 

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

</web-app> 

春servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


    <context:component-scan base-package="org.itc" /> 
    <mvc:resources mapping="/html/**" location="/html/" /> 
    <mvc:resources mapping="/js/**" location="/js/" /> 
    <mvc:annotation-driven /> 

</beans> 

ここhomepage.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <h1>Hello world</h1> 
</body> 
</html> 

homepage.htmlをP WEB-INF/htmlフォルダに再送します。私はエラー/例外を取得していません。 私はSpring 4.0.6バージョンを使用しています。 これに関する助力?

+0

は、ビューの春-servlet.xmlに線の下に追加Spring-servlet.xmlViewResolverを言及する必要がありますリゾルバ \t \t <豆:プロパティ名= "接頭辞" 値= "/ WEB-INF/JSP /" /> \t \t <豆:プロパティ\t – Vaibs

+0

@Vaibs jspファイルを使用していないので、htmlファイルを使用したいと思います。まだviewresolverを宣言する必要がありますか? – Protagonist

答えて

1

あなたは

春servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


    <context:component-scan base-package="org.itc" /> 
    <mvc:resources mapping="/html/**" location="/html/" /> 
    <mvc:resources mapping="/js/**" location="/js/" /> 
    <mvc:annotation-driven /> 
    //Add this.. 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/html/" /> 
     <property name="suffix" value=".html" /> 
    </bean> 

</beans> 

コントローラ

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String loadHtml(Map<String, Object> model, HttpServletRequest request) { 
    return "homePage"; 
} 
+0

私は試しましたが、それは同じです。 – Protagonist

+0

web.xmlのウェルカムファイルを削除する –

関連する問題