gpt4 book ai didi

java - 在 Thymeleaf 和 Spring 4 中,DispatcherServlet 中未找到带有 URI 的 HTTP 请求的映射

转载 作者:行者123 更新时间:2023-12-02 03:31:07 26 4
gpt4 key购买 nike

我想将 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/>-->评论因为我认为这里不需要它们。我有来自 Maven 的 Thymeleaf 依赖项,包含在 pom.xml 中:

<!-- 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>

所以,我很确定我错过了一些简单的东西,比如一个配置行。如果有人能帮助我,我将不胜感激,提前谢谢。 附注 我还包括 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我必须有一个用于第一个 View 的 Controller (例如我在此处包含的MainController)。感谢用户为我解决这个问题提供了很多帮助。

最佳答案

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

关于java - 在 Thymeleaf 和 Spring 4 中,DispatcherServlet 中未找到带有 URI 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com