ssm中使用thymeleaf

5 篇文章 0 订阅

**

注意:

**
①controller的作用域必须入参,要不然返回结果为null
②thymeleaf的 视图解析器会冲突,只配置一个即可

1、pom依赖

<!--    (使用Thymeleaf模板引擎)-->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>

2、springmvc-servlet.xml配置

  <!--    thymeleaf的视图解析器   会与冲突ContentNegotiatingViewResolver-->
    <bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine" ref="templateEngine"/>
    </bean>
    <!-- 模板引擎 -->
    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>
    <!-- 模板解析器 -->
    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <constructor-arg ref="servletContext"/>
        <property name="prefix" value="/WEB-INF/muke/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
        <property name="cacheable" value="false"/>
        <property name="characterEncoding" value="UTF-8"/>

    </bean>

3、controller存放到作用域中

参数必须入参:

@RequestMapping(value = "/adminLogin",method = RequestMethod.POST)
public String adminLogin(HttpServletRequest req,
                         HttpServletResponse resp,
                         Map<String, Object> map,
                         Model model,

存放值

	    req.getSession().setAttribute("admin", admin);
        req.setAttribute("name", admin);
        map.put("admin1", admin);
        model.addAttribute("model", admin);

4、html中去对应的作用域的值

html头部增加支持

	<html xmlns:th="http://www.thymeleaf.org">

取值

	    <span class="user" th:text="${session.admin.adminName}"></span>
        <span class="user" th:text="${name.adminName}"></span>
        <span class="user" th:text="${model.adminName}"></span>
        <span class="user" th:text="${admin1.adminName}"></span>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卧虎3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值