SSM框架整合thymeleaf-spring5模板引擎

SSM框架整合thymeleaf-spring5模板引擎| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |

| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 14633479| SSM框架整合thymeleaf-spring5模板引擎| 2021-04-08T17:29:00| | BlogPost|

1、pom.xml文件增加依赖:

<!--thymeleaf-spring5 -->
<dependency>
     <groupId>org.thymeleaf</groupId>
     <artifactId>thymeleaf-spring5</artifactId>
     <version>3.0.11.RELEASE</version>
</dependency>

2、将spring-mvc.xml中原先jsp的视图解析器修改:

复制代码
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".html"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="order" value="1"/>
    <property name="templateMode" value="HTML5"/>
    <property name="cacheable" value="false"/>
</bean>

<bean id="templateEngine"
class
="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>

<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>

复制代码

3、测试,controller:

复制代码
@Controller
@RequestMapping("/test")
public class TestController {
    @RequestMapping("")
    public String test(Model model)
    {
        model.addAttribute("name","I'm thymeleaf !");
        return "test";
    }
}
复制代码

4、编写test.html

复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${name}"></h1>
</body>
</html>
复制代码

注意:

  • <property name="prefix" value="/WEB-INF/templates/"/>这里写页面相应的目录。
  • 在html头添加xmlns:th="http://www.thymeleaf.org",以便提示thymeleaf语法
| 648658| | 2024-04-29T21:16:00| false| | 2021-04-08T17:28:42.657| true| 1、pom.xml文件增加依赖: <!--thymeleaf-spring5 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.11.RE| Anonymous|
posted @   RalphLauren  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示