当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot开发Web应用详解

Spring Boot开发Web应用详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

spring boot快速入门中我们完成了一个简单的restful service,体验了快速开发的特性。在留言中也有朋友提到如何把处理结果渲染到页面上。那么本篇就在上篇基础上介绍一下如何进行web应用的开发。

静态资源访问

在我们开发web应用的时候,需要引用大量的js、css、图片等静态资源。

默认配置

spring boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

  1. /static
  2. /public
  3. /resources
  4. /meta-inf/resources

举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/d.jpg。如能显示图片,配置成功。

渲染web页面

在之前的示例中,我们都是通过@restcontroller来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?

模板引擎

在动态html实现上spring boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。

spring boot提供了默认配置的模板引擎主要有以下几种:

  1. thymeleaf
  2. freemarker
  3. velocity
  4. groovy
  5. mustache

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:each="prod : ${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>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-thymeleaf</artifactid>
</dependency>

在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过thymeleaf渲染一个页面。

 @controller
public class hellocontroller {
  @requestmapping("/")
  public string index(modelmap map) {
    // 加入一个属性,用来在模板中读取
    map.addattribute("host", "http://blog.didispace.com");
    // return模板文件的名称,对应src/main/resources/templates/
    return "index"; 
  }
}
<!doctype html>
<html>
<head lang="en">
  <meta charset="utf-8" />
  <title></title>
</head>
<body>
<h1 th:text="${host}">hello world</h1>
</body>
</html>

如上页面,直接打开html页面展现hello world,但是启动程序后,访问http://localhost:8080/,则是展示controller中host的值://www.jb51.net,做到了不破坏html自身内容的数据逻辑分离。

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

thymeleaf的默认参数配置

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

 # enable template caching.
spring.thymeleaf.cache=true 
# check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# content-type value.
spring.thymeleaf.content-type=text/html 
# enable mvc thymeleaf view resolution.
spring.thymeleaf.enabled=true 
# template encoding.
spring.thymeleaf.encoding=utf-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.mode=html5 
# prefix that gets prepended to view names when building a url.
spring.thymeleaf.prefix=classpath:/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支持

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网