ssm 日志输出html,ssm整合Thymeleaf的详细教程以及案列

1.需要添加必要的jar包   链接:https://pan.baidu.com/s/1Y3bA9D7_QAHG3jRq670m6A  密码:29m9

6605c679c690256d9f13e23334853de4.png

ec85bf370e08085bdc856d0ca1e7b933.png

e5b1fca68d4c00f8734d04d785b8dbcf.png

2. SpringMvc.xml配置

class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">

class="org.thymeleaf.spring4.SpringTemplateEngine">

3. web.xml进行配置

springMvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:SpringMvc.xml

1

springMvc

/*

springMvc

*.html

4.  编写controller

@RequestMapping(value = "/thymeleaf", method=RequestMethod.GET)

public  String  thymeleaf(Model model) {

model.addAttribute("hello", "jack")

return "ce";

}

5. 编写ce.html

ceshi

//基础知识

Thymeleaf笔记

1.前端html页面标签中引入

2.表达式

2.1变量表达式: ${…}

9fb77822ed57a22d8542413e49eace63.png

${books}从上下文中选择名为books的变量,并在th:each中使用循环将其评估为迭代器

2.2选择表达式: *{…}

选择表达式就像变量表达式一样,它们不是整个上下文变量映射上执行,而是在先前选择的对象。 它们看起来像这样:

*{customer.name}

它们所作用的对象由th:object属性指定:

...

...

...

a626190d8194072ce13a9f6f76bed0c0.png

2.3消息表达式: #{…}  国际化时使用,也可以使用内置的对象,比如date格式化数据

2.4链接表达式: @{…}  用来配合link src href使用的语法

链接表达式也可以是绝对的:

...

2.5片段表达式: ~{…}     用来引入公共部分代码片段,并进行传值操作使用的语法

片段表达式是一种简单的方法用来表示标记的片段并将其移动到模板中。 由于这些表达式,片段可以被复制,传递给其他模板的参数等等。最常见的是使用th:insert或th:replace来插入片段

~{templatename::fragmentname}

templatename:模版名,Thymeleaf会根据模版名解析完整路径:/resources/templates/templatename.html,要注意文件的路径。

fragmentname:片段名,Thymeleaf通过th:fragment声明定义代码块

如:创建footer.html目前测试在本页面才能够引用

© 2011 The Good Thymes Virtual Grocery

© 2011 The Good Thymes Virtual Grocery

© 2011 The Good Thymes Virtual Grocery

© 2011 The Good Thymes Virtual Grocery

3. 文字

文本文字,例如:'one text', 'Another one!',…

数字文字,例如:0,10, 314, 31.01, 112.83,…

布尔文字,例如:true,false

Null文字,例如:Null

文字标记,例如:one, sometext, main,…

文本操作:

字符串连接:+

文字替换:|The name is ${name}|

算术运算:

二进制操作:+, -, *, /, %

减号(一元运算符):-

布尔运算:

二进制运算符,and,or

布尔否定(一元运算符):!,not

比较和相等:

比较运算符:>,=,<=(gt,lt,ge,le)

相等运算符:==, != (eq, ne)

条件操作符:

If-then:(if) ? (then)

If-then-else:(if) ? (then) : (else)

Default: (value) ?: (defaultvalue)

4.标准的URL语法

@{…}

绝对网址

2. 上下文相关URL

URL:http://localhost:8080/myapp来访问,myapp就是上下文名称。

3. 与服务器相关URL

4. 协议相关URL

5. 添加参数

上面示例代码,最终将输出为:

也可以添加几个参数,用逗号分隔它们:

HTML

上面代码将输出结果为:

还可以使用正常参数的路径变量的形式包含参数,但在URL的路径中指定一个占位符:

HTML

上面输出结果为:

包括条件表达式,例如:

6.简单格式化输出

6f1e41a4f6c827b5d1300d8be717f5b3.png

13487f2c4ada124c533dc769aacb9397.png

7.迭代列表

97acad4cabeb1c3839ebe8f99d8507b2.png

45163c05b601a0411583831691165d29.png

条件判断针对于价格大于100,在备注一栏显示字体特殊提供

2eaaaf65f74dad82a84ccd79cf83e568.png

8.form表单提交数据

f75d41e2d2a99fdc3773a61b4a2c30fc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM(Spring + Spring MVC + MyBatis)是一种经典的Java开发框架组合,而Thymeleaf是一种流行的模板引擎。下面是SSM整合Thymeleaf的基本步骤: 1. 首先,确保你已经正确配置好了Spring、Spring MVC和MyBatis。 2. 添加Thymeleaf的依赖到你的项目中。你可以在Maven的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- 其他依赖... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 3. 在Spring配置文件中启用Thymeleaf视图解析器。在Spring MVC的配置文件(例如`springmvc.xml`)中添加以下配置: ```xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 其他配置... --> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".html" /> <property name="order" value="2" /> <!-- 设置优先级,确保Thymeleaf视图解析器在其他视图解析器之前 --> </bean> <bean id="thymeleafViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.ThymeleafView" /> <property name="templateEngine" ref="templateEngine" /> <property name="order" value="1" /> <!-- 设置优先级,确保Thymeleaf视图解析器优先于其他视图解析器 --> </bean> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver" /> </bean> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/" /> <property name="suffix" value=".html" /> <property name="templateMode" value="HTML" /> </bean> ``` 4. 创建Thymeleaf的模板文件。在`/WEB-INF/templates/`目录下创建HTML模板文件,例如`index.html`。 5. 在你的Controller中返回Thymeleaf视图。在你的Controller方法中,使用`ModelAndView`对象返回Thymeleaf视图的名称(不包含后缀),例如: ```java @GetMapping("/index") public ModelAndView getIndexPage() { ModelAndView modelAndView = new ModelAndView("index"); // 设置模型数据 modelAndView.addObject("message", "Hello, Thymeleaf!"); return modelAndView; } ``` 6. 在Thymeleaf模板文件中使用模型数据。在Thymeleaf模板文件中,使用Thymeleaf的表达式语法来展示模型数据。例如,在`index.html`中展示消息: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>SSM with Thymeleaf</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这样,你就完成了SSMThymeleaf的整合。当访问`/index`路径时,将会渲染`index.html`模板并显示消息 "Hello, Thymeleaf!"。请根据你的项目需求进行适当的配置和调整。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值