Spring MVC 4 Thymeleaf 3 HTTP状态404 - 找不到资源

时间:2017-11-30 06:42:29

标签: spring spring-mvc thymeleaf

enter image description here

我在stackoverflow中找到了具有相似标题的帖子,但是其中一种方式或其他方式缺少某些配置或文件中的一些pitflow。我发布这个是因为我做了所有可能的更正,仍然无法使这项工作。我的代码如下:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>home.html</welcome-file>
        <welcome-file>default.html</welcome-file>

    </welcome-file-list>
</web-app>

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <import resource="applicationContext.xml"/>
    <context:component-scan base-package="com.rahul.*"/>

</beans>

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
    <mvc:annotation-driven/>

    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

    <!-- Thymeleaf Template Engine (Spring4-specific version) -->
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolvers">
            <set>
                <ref bean="templateResolver" />
            </set>
        </property>
    </bean>

    <!-- Thymeleaf Template Resolver -->
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".html" />
    </bean>

</beans>

AppController.java

package com.rahul.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AppController {

   @RequestMapping("/hello")
   public String indexRediect(Model model){
       model.addAttribute("name","Rahul");
       return "hello";
   }

}

的index.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>

    <title>Hello Spring MVC</title>

    <!--<link th:href="@{/resources/css/bootstrap.min.css}" rel="stylesheet"/>-->
</head>
<body>
<div class="container">

    <h2 th:text="Sample Page"></h2>
    <form action="#" th:action="@{/hello}" method="post">
        <p>Id: <input type="text"/></p>
        <p>Message: <input type="text"/></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>

</div>
<!-- /container -->
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script th:src="@{/resources/js/bootstrap.min.js}"></script>-->
</body>
</html>

hello.html的

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>

    <title>Hello Spring MVC</title>

    <!--<link th:href="@{/resources/css/bootstrap.min.css}" rel="stylesheet"/>-->
</head>
<body>
<div class="container">

    <h2 th:text="'Hello ' + ${name} + '!'"></h2>

</div>
<!-- /container -->
<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script th:src="@{/resources/js/bootstrap.min.js}"></script>-->
</body>
</html>

我还尝试在applicationcontext xml以及项目构建路径中更改html文件夹路径。但没有运气。

0 个答案:

没有答案