免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1912 | 回复: 1
打印 上一主题 下一主题

Spring Cloud Spring Boot mybatis分布式微服务云架构(八)开发Web应用(2) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-06-13 11:23 |只看该作者 |倒序浏览
在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面。
  1. @Controller
  2. public class HelloController {

  3.     @RequestMapping("/")
  4.     public String index(ModelMap map) {
  5.         // 加入一个属性,用来在模板中读取
  6.         map.addAttribute("host", "http://blog.didispace.com");
  7.         // return模板文件的名称,对应src/main/resources/templates/index.html
  8.         return "index";  
  9.     }

  10. }
复制代码
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4.     <meta charset="UTF-8" />
  5.     <title></title>
  6. </head>
  7. <body>
  8. <h1 th:text="${host}">Hello World</h1>
  9. </body>
  10. </html>
复制代码

如上页面,直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/,则是展示Controller中host的值:http://blog.didispace.com,做到了不破坏HTML自身内容的数据逻辑分离。

更多Thymeleaf的页面语法,还请访问Thymeleaf的官方文档查询使用。

Thymeleaf的默认参数配置

如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。

  1. # Enable template caching.
  2. spring.thymeleaf.cache=true
  3. # Check that the templates location exists.
  4. spring.thymeleaf.check-template-location=true
  5. # Content-Type value.
  6. spring.thymeleaf.content-type=text/html
  7. # Enable MVC Thymeleaf view resolution.
  8. spring.thymeleaf.enabled=true
  9. # Template encoding.
  10. spring.thymeleaf.encoding=UTF-8
  11. # Comma-separated list of view names that should be excluded from resolution.
  12. spring.thymeleaf.excluded-view-names=
  13. # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
  14. spring.thymeleaf.mode=HTML5
  15. # Prefix that gets prepended to view names when building a URL.
  16. spring.thymeleaf.prefix=classpath:/templates/
  17. # Suffix that gets appended to view names when building a URL.
  18. spring.thymeleaf.suffix=.html  spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
复制代码
支持JSP的配置

Spring Boot并不建议使用,但如果一定要使用,可以参考此工程作为脚手架:JSP支持


论坛徽章:
0
2 [报告]
发表于 2018-06-13 11:23 |只看该作者
对源码感兴趣的朋友可以一起交流~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP