2016-06-28 69 views
0

我想春天4 Thymeleaf整合,但我得到了沒有映射的DispatcherServlet發現HTTP請求的URI具有與Thymeleaf和Spring 4

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豆完全從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豆和<!--<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 
public final class MainController { 
    @RequestMapping(value = "/") 
    public String displayFirstView() { 
     return "index.html"; 
    } 
} 

但它並沒有幫助。 解決方案:我錯過了中的<mvc:annotation-driven/>,我必須爲第一個視圖設置一個控制器(如我在這裏包含的MainController)。感謝那些幫助我解決這個問題的用戶。

+1

查看服務器日誌。 mybe a部署中的問題 – Jens

+0

@Jens我只有'org.hsqldb.HsqlException:用戶缺少權限或找不到對象:PUBLIC.LANGUAGES_LIST'與Tomcat Catalina'選項卡中的Hibernate相關,但是當我使用沒有Thymeleaf的JSP時,它也發生了,但index.jsp顯示 –

+1

看起來像你必須檢查你的數據庫訪問數據 – Jens

回答

1

你缺少這讀取@Controller註釋需要<mvc:annotation-driven/>。嘗試添加它。

+0

感謝您的幫助,這對我來說很奇怪,因爲在之前的項目中,我沒有使用Controller作爲第一視圖(index.jsp),也許是因爲我有'@ RestController's和AngularJS,我很新用JSP/Thymeleaf進行模板化。再次感謝你,現在它像一個魅力。 –

+1

我很高興;)如果你在以前的項目中使用過AspectJ,我認爲這是因爲有aspectj自動代理或類似的東西。我不知道很激動,試着看看Spring文檔。但是當你需要在Spring MVC中使用註解@Controller時,你必須添加它。 – Hrabosch

1

從我可以從下面的鏈接看到,你的viewResolver應該引用templateEngine。

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

+0

我添加到viewResolver一個templateEngine:'p:templateEngine-ref =「templateEngine」'但它仍然不起作用。我還加了一個網頁。XML內容,也許有什麼地方是錯誤的,我找不到 –

+1

是不是隻是一個映射到您的servlet的問題?這應該是'/*' –

+0

@ J.Mengelie感謝您的關注,我添加了它,但仍然無法工作。 –

相關問題