zoukankan      html  css  js  c++  java
  • ssm中使用thymeleaf

    **

    ## 注意:

    **
    ①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>

  • 相关阅读:
    gulp教程、gulp-less安装
    vue学习总结
    javascript数组去重
    【操作系统】操作系统高频面试考点总结
    【面经系列】一线互联网大厂前端面试技巧深入浅出总结
    【编程题与分析题】Javascript 之继承的多种实现方式和优缺点总结
    【计算机网络】TCP基础知识详解
    【操作系统】操作系统面试基础知识点总结
    【数据结构与算法】数据结构基础知识总结(面试考点)
    【前端知识体系-JS相关】JS-Web-API总结
  • 原文地址:https://www.cnblogs.com/xwd2366846227/p/11200485.html
Copyright © 2011-2022 走看看