自己怎么建h5商城网站,珠海有什么好的互联网公司,网站备案率是什么,云浮头条新闻Spring Boot快速入门中我们完成了一个简单的RESTful Service#xff0c;体验了快速开发的特性。在留言中也有朋友提到如何把处理结果渲染到页面上。那么本篇就在上篇基础上介绍一下如何进行Web应用的开发。 静态资源访问
在我们开发Web应用的时候#xff0c;需要引用大量的j… Spring Boot快速入门中我们完成了一个简单的RESTful Service体验了快速开发的特性。在留言中也有朋友提到如何把处理结果渲染到页面上。那么本篇就在上篇基础上介绍一下如何进行Web应用的开发。 静态资源访问
在我们开发Web应用的时候需要引用大量的js、css、图片等静态资源。
默认配置
Spring Boot默认提供静态资源目录位置需置于classpath下目录名需符合如下规则
/static/public/resources/META-INF/resources
举例我们可以在src/main/resources/目录下创建static在该位置放置一个图片文件。启动程序后尝试访问http://localhost:8080/D.jpg。如能显示图片配置成功。
渲染Web页面
在之前的示例中我们都是通过RestController来处理请求所以返回的内容为json对象。那么如果需要渲染html页面的时候要如何实现呢
模板引擎
在动态HTML实现上Spring Boot依然可以完美胜任并且提供了多种模板引擎的默认配置支持所以在推荐的模板引擎下我们可以很快的上手开发动态网站。
Spring Boot提供了默认配置的模板引擎主要有以下几种
ThymeleafFreeMarkerVelocityGroovyMustache
Spring Boot建议使用这些模板引擎避免使用JSP若一定要使用JSP将无法实现Spring Boot的多种特性具体可见后文支持JSP的配置
当你使用上述模板引擎中的任何一个它们默认的模板配置路径为src/main/resources/templates。当然也可以修改这个路径具体如何修改可在后续各模板引擎的配置属性中查询并修改。
Thymeleaf
Thymeleaf是一个XML/XHTML/HTML5模板引擎可用于Web与非Web环境中的应用开发。它是一个开源的Java库基于Apache License 2.0许可由Daniel Fernández创建该作者还是Java加密库Jasypt的作者。
Thymeleaf提供了一个用于整合Spring MVC的可选模块在应用开发中你可以使用Thymeleaf来完全代替JSP或其他模板引擎如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码开发者只需将标签属性添加到模板中即可。接下来这些标签属性就会在DOM文档对象模型上执行预先制定好的逻辑。
示例模板
table thead tr th th:text#{msgs.headers.name}Name/td th th:text#{msgs.headers.price}Price/td /tr /thead tbody tr th:eachprod : ${allProducts} td th:text${prod.name}Oranges/td td th:text${#numbers.formatDecimal(prod.price,1,2)}0.99/td /tr /tbody/table可以看到Thymeleaf主要以属性的方式加入到html标签中浏览器在解析html时当检查到没有的属性时候会忽略所以Thymeleaf的模板可以通过浏览器直接打开展现这样非常有利于前后端的分离。
在Spring Boot中使用Thymeleaf只需要引入下面依赖并在默认的模板路径src/main/resources/templates下编写模板文件即可完成。
dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId/dependency在完成配置之后举一个简单的例子在快速入门工程的基础上举一个简单的示例来通过Thymeleaf渲染一个页面。
Controllerpublic class HelloController { RequestMapping(/) public String index(ModelMap map) { // 加入一个属性用来在模板中读取 map.addAttribute(host, http://blog.didispace.com); // return模板文件的名称对应src/main/resources/templates/index.html return index; }}!DOCTYPE htmlhtmlhead langen meta charsetUTF-8 / title/title/headbodyh1 th:text${host}Hello World/h1/body/html如上页面直接打开html页面展现Hello World但是启动程序后访问http://localhost:8080/则是展示Controller中host的值http://blog.didispace.com做到了不破坏HTML自身内容的数据逻辑分离。
更多Thymeleaf的页面语法还请访问Thymeleaf的官方文档查询使用。
Thymeleaf的默认参数配置
如有需要修改默认配置的时候只需复制下面要修改的属性到application.properties中并修改成需要的值如修改模板文件的扩展名修改默认的模板路径等。
# Enable template caching.spring.thymeleaf.cachetrue # Check that the templates location exists.spring.thymeleaf.check-template-locationtrue # Content-Type value.spring.thymeleaf.content-typetext/html # Enable MVC Thymeleaf view resolution.spring.thymeleaf.enabledtrue # Template encoding.spring.thymeleaf.encodingUTF-8 # Comma-separated list of view names that should be excluded from resolution.spring.thymeleaf.excluded-view-names # Template mode to be applied to templates. See also StandardTemplateModeHandlers.spring.thymeleaf.modeHTML5 # Prefix that gets prepended to view names when building a URL.spring.thymeleaf.prefixclasspath:/templates/ # Suffix that gets appended to view names when building a URL.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支持
代码示例
本文的相关例子可以查看下面仓库中的chapter3-1-2目录
Githubhttps://github.com/dyc87112/SpringBoot-LearningGiteehttps://gitee.com/didispace/SpringBoot-Learning
如果您觉得本文不错欢迎Star支持您的关注是我坚持的动力