我想春天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
)。感謝那些幫助我解決這個問題的用戶。
查看服務器日誌。 mybe a部署中的問題 – Jens
@Jens我只有'org.hsqldb.HsqlException:用戶缺少權限或找不到對象:PUBLIC.LANGUAGES_LIST'與Tomcat Catalina'選項卡中的Hibernate相關,但是當我使用沒有Thymeleaf的JSP時,它也發生了,但index.jsp顯示 –
看起來像你必須檢查你的數據庫訪問數據 – Jens