为什么thmeleaf 的th:replace 只有再转发之后才起作用
最近写了个项目 一直在爬坑,因为想直接用html代替jsp 就用了thmeleaf 这个java模板引擎 然后就一直出错,现在只能求助大神们解答了
我做的是一个 ssm整合项目
开始上图
在网上找的如何引入thmeleaf
一、maven坐标
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
二、配置spring-mvc
<!-- 模板解析器 -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<constructor-arg ref="servletContext"/>
<property name="prefix" value="/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean>
<!-- 模板引擎 -->
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<!--设置静态资源不过滤-->
<mvc:resources mapping="/css/" location="/css/" />
<mvc:resources mapping="/img/" location="/images/" />
<mvc:resources mapping="/js/" location="/js/" />
<!-- 开启对SpringMVC注解的支持-->
<mvc:annotation-driven/>
三、web.xml
<!--前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet)-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数 ,创建完DispatcherServlet对象,加载springmvc.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--服务器启动时让DispatcherServlet对象创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
controller层中的代码
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;
@RequestMapping("/findAll.do")
public String findAll (Model model) throws Exception {
List<Product> all = productService.findAll();
model.addAttribute("productList",all);
System.out.println(all);
return "product_list";
}
}
前端页面代码
页面 product_list
<html lang="en" xmlns:th="http://www.thymeleaf.org" ><html>
<!--引入-->
<!--</header>-->
<div th:replace="header::header" ></div>
只有在访问 findAll.do 转发之后才能成功引入
直接访问 pages/product_list不显示