aspcms网站打不开,免费店铺logo,wordpress多站点 文章导入,2012r2做网站Thymeleaf介绍Thymeleaf#xff0c;是一个XML/XHTML/HTML模板引擎#xff0c;开源的java库#xff0c;可以用于SpingMVC项目中#xff0c;用于代替JSP、FreeMarker或者其他的模板引擎#xff1b;页面与数据分离#xff0c;提高了开发效率#xff0c;让代码重用更容易。S…Thymeleaf介绍Thymeleaf是一个XML/XHTML/HTML模板引擎开源的java库可以用于SpingMVC项目中用于代替JSP、FreeMarker或者其他的模板引擎页面与数据分离提高了开发效率让代码重用更容易。Springboot集成Thymeleaf文章示例环境配置信息jdk版本:1.8开发工具Intellij iDEA 2020.1springboot:2.3.9.RELEASE依赖引入dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId
/dependency
dependencygroupIdognl/groupIdartifactIdognl/artifactIdversion3.1.26/version
/dependency配置文件spring.thymeleaf.cachefalse
spring.thymeleaf.suffix.html
spring.thymeleaf.prefixclasspath:/templates/
spring.thymeleaf.servlet.content-typetext/html
spring.thymeleaf.encodingutf-8
spring.messages.basenamei18n/messages集成示例thymeleaf的用法其实和jsp、freemarker差不多下面用一个示例实际看一下thymeleaf是怎么使用的1、定义一个ExampleController类注意这里使用Controller注解标记ExampleController不要使用RestController2、controller层具体处理请求的方法内增加一个形参org.springframework.ui.Model用于携带后台的处理数据3、返回值的“index”表示classpath下templates中模板名称是index后缀是.html的模板4、contoller层处理完后携带后台处理数据到达视图层进行数据的渲染。Controller
RequestMapping(/example)
public class ExampleController {GetMapping(/index)public String index(Model model) {model.addAttribute(userName, fanfu);model.addAttribute(msg, thymeleaf模板内容);return index;}
}这里注意一下通过xmlns:thhttp://www.thymeleaf.org引入了thymeleaf命名空间th:text用于处理html标签体的文本内容但是html5不允许使用th:*这些非标准属性的因此可以切换到thymeleaf的data-th-*方法来替换th:*方法因此可以这么理解th:*和data-th-*的用法是等效的为了遵循标准的用法这篇文章的所有示例都采用data-th-*的写法。!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8title测试/title
/head
bodydiv你好span data-th-text${userName}/span/divdiv这是一个span data-th-text${msg}/span 。/div
/body
/htmlThymeleaf语法表达式thymeleaf内置了5种标准表达式如下1、${...}变量表达式取出上下文环境中变量的值p data-th-text${username}/p2、*{...}选择变量表达式取选择的对象的属性值div data-th-object${formObj}p data-th-text*{title}/pp data-th-text*{creator}/p
/div3、#{...}消息表达式使用文字消息的国际化p data-th-text#{welcome.message}/p
p data-th-text#{welcome.user.message(${formObj.creator})}/p4、{...}链接表达式用于表示各种超链接地址 p data-th-text{http://localhost:8080/example/index(creator${formObj.creator},status1)}/p
a data-th-href{url} target_blank超链接/a5、~{...}片段表达式引用一段公共的代码片段如下“example”表示另外一个模板名字里面是一些通用代码片段可以使用这种方式引入到当前模板中p data-th-text~{example}/p遍历p data-th-eachstudent:${students} data-th-text${student.name}/p条件判断条件判断语句有三种分别是th:if、th:unless、th:switchth:if如果表达式内容为真则显示内容p data-th-if${userName!null}如果username不是null,我就会显示/pth:unless如果表达式内容为假则显示内容p data-th-unless${userNamenull}如果username不是null,我就会显示/pth:switch为多路选择语句需要搭配th:case来使用; div data-th-switch${userName}p data-th-casefanfu凡夫贬夫你好/pp data-th-casetest这是一个test/p
/divThymeleaf应用场景Thymeleaf可以替代JSP来进行动态网页的开发但是在前后端分离、前端组件更加丰富多元化的今天依然采用JSP的模式用Thymeleaf来替代JSP进行动态网页的开发未免有些落后了因此Thymeleaf就没有用武之地吗当然不。Thymeleaf是模板引擎不仅可以处理html模板还可以处理xml、CSS等其他一些格式的模板文件。例如输出一些有样式的制式文本如通知公告、申请书、建议书等。下面是一个具体的示例student.html是一个学生成绩展示的模板但是学生会有很多成绩也不一样但是如果需要用一个制式的格式来展示这些数据可以这么做1、先拿到学生的成绩数据2、然后用编程式的方法使用thymeleaf模板引擎根据模板生成静态的带有样式且加载好数据的html网页代码3、通常成绩、通知这类信息一旦形成基本上是不会改了因此拿到已经加载数据的html网页内容就可以直接渲染显示了Test
public void test() throws IOException {//测试数据ListStudent students this.students();String staticDir ResourceUtils.getFile(classpath:static\\).getPath() File.separator;String targetFilePathstaticDir/student-data.html;//thymeleaf引擎上下文环境Context contextnew Context();//在thymeleaf引擎上下文环境中装载模板上要渲染的数据context.setVariable(students,students);File file new File(targetFilePath);if (!file.exists()) {file.delete();}//定义thymeleaf模板打印输出流PrintWriter printWriternew PrintWriter(file);//定义thymeleaf模板解析器FileTemplateResolver fileTemplateResolver new FileTemplateResolver();//thymeleaf模板解析器解析内容的后缀fileTemplateResolver.setSuffix(.html);//thymeleaf模板解析器解析内容的前缀String tempDir ResourceUtils.getFile(classpath:templates\\).getPath()File.separator;fileTemplateResolver.setPrefix(tempDir);//定义//thymeleaf模板引擎TemplateEngine templateEngine new TemplateEngine();//装载thymeleaf模板解析器templateEngine.setTemplateResolver(fileTemplateResolver);//执行thymeleaf模板引擎的模板解析能力有三个参数分别是模板名字不包括后缀和前缀、上下文环境、打印输出流templateEngine.process(student,context,printWriter);
}
public ListStudent students() {ListStudent students new ArrayList();for (int i 0; i 10; i) {Student student new Student();student.setName(张三 i);student.setScore(98);students.add(student);}return students;
}!DOCTYPE html
html langen xmlns:thhttp://www.thymeleaf.org
headmeta charsetUTF-8/title学生信息/title
/head
body
divpspan data-th-text姓名/span-----span data-th-text成绩/span/pp data-th-eachstudent:${students}span data-th-text${student.name}/span-----span data-th-text${student.score}/span/p
/div
/body
/html