使用Thymeleaf和Spring 4在DispatcherServlet中找不到具有URI的HTTP请求的映射

时间:2016-06-28 11:19:17

标签: java spring jsp spring-mvc thymeleaf

我想将Spring 4与Thymeleaf集成,但我得到了

WARNING [http-apr-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/hire-platform/] in DispatcherServlet with name 'mvc-dispatcher'

不显示/WEB-INF/templates/index.html的内容。这是mvc-dispatcher-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:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>

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

<bean
    id="servletContext"
    class="beans.ServletContextFactory"
></bean>

<bean
    id="templateResolver"
    class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"
    p:prefix="/WEB-INF/templates/"
    p:suffix=".html"
    p:templateMode="HTML5"
>
    <constructor-arg ref="servletContext"/>
</bean>
<bean
    id="templateEngine"
    class="org.thymeleaf.spring4.SpringTemplateEngine"
    p:templateResolver-ref="templateResolver"
>
</bean>

servletContext bean从How to set ServletContext property for a bean in Spring XML metadata configuration answer完全复制粘贴的位置,需要它作为我在上一个问题Failed to instantiate [org.thymeleaf.templateresolver.ServletContextTemplateResolver]: No default constructor found中找到的构造函数参数。我认为它应该重定向到HTML文件(index.html),当我使用JSP(index.jsp)而不是Thymeleaf时,它可以工作。也许我在applicationContext.xml文件中遗漏了一些东西?以下是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:jdbc="http://www.springframework.org/schema/jdbc"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
   ">

<jdbc:embedded-database id="DataSource" type="HSQL">
</jdbc:embedded-database>

<context:component-scan base-package="beans"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory"></property>
</bean>

<tx:annotation-driven />

我删除了Spring AOP部分,SessionFactory bean和<!--<mvc:annotation-driven/>-->评论,因为我认为这里不需要它们。 我在pom.xml中包含来自Maven的Thymeleaf依赖项:

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.0-b01</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring4</artifactId>
  <version>3.0.0.RELEASE</version>
</dependency>

所以,我很确定我错过了一些简单的东西,比如一条配置线。如果有人会帮助我,我将不胜感激,谢谢你。的 P.S。  我还包括web.xml文件,因为我看到类似的问题了:

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

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

  <!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <listener>
    <listener-class>
      org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>

  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

我也试过添加一个Controller:

@Controller
public final class MainController {
    @RequestMapping(value = "/")
    public String displayFirstView () {
        return "index.html";
    }
}

但它没有帮助。 解决方案:我在<mvc:annotation-driven/>中错过了applicationContext.xml,我必须拥有第一个视图的控制器(例如我在此处包含的MainController)。感谢那些帮助我解决这个问题的用户。

2 个答案:

答案 0 :(得分:1)

从我可以从下面看到的链接,你的viewResolver应该引用templateEngine。

http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

答案 1 :(得分:1)

您缺少阅读<mvc:annotation-driven/>注释所需的@Controller。尝试添加它。