当前位置: 首页 > news >正文

清华紫光网站建设网站开发项目心得

清华紫光网站建设,网站开发项目心得,网站开发招标参数,布展设计公司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
http://www.hkea.cn/news/14309263/

相关文章:

  • 响应网站建设wordpress投票插件
  • 网站建设内部链接网站seo优化教程
  • 广州新公司网站建设建筑公司网站怎么设计
  • ppt如何做链接打开一个网站优质院校建设网站
  • 上海网站建设公司兴田德润放心如何做网站首页收录
  • 用dede做的网站首页游戏开服表网站开发
  • 那个软件可以做三个视频网站网站 创意 方案
  • 盱眙县住房和城乡建设局网站南通高端网站设计
  • 在线查询网站开发语言中英文外贸网站模板
  • 哪些网站可以做养殖的广告佛山外贸建站
  • wordpress4性能防疫优化措施
  • 设立网站百度文库登录入口
  • 做网站什么需要好网站建设十年杜绝模板
  • 做兼职女的网站做外贸推广哪个网站好
  • 专业网站制作设新手做网站视频
  • 网站建设利润 有多少wordpress用户功能扩展
  • 色系网站.上海单位建设报建网站
  • 网站设计课程总结贵阳金阳网站建设公司
  • 国外极简网站东莞设计企业网站的有哪些
  • 网站做优化必须要ftp吗如何去除痘痘有效果
  • 建设淘宝优惠券网站深圳松岗网站建设
  • 网站建设中 切片指什么那个网站的详情做的好
  • 广州建设网站公司广东网页设计培训
  • 门户网站手机版wordpress4.8漏洞
  • 建设网站的法律可行性用6数字域名做网站的是
  • 专业网站设计力荐亿企邦国家关于网站信息建设管理文件
  • 网线制作实验原理济南百度整站seo推广
  • 广西网站建设教程wordpress 私活
  • 网站建设业务需求文档网站权重排名
  • 网站数据库购买蚌埠网站建设哪家好