为什么thmeleaf 的th:replace 只有再转发之后才起作用

为什么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不显示
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值