2016-06-28 203 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感谢您的关注,我添加了它,但仍然无法工作。 –

相关问题