• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Spring Boot开发Web应用详解

Spring Boot开发Web应用详解

作者:翟永超 字体:[增加 减小] 来源:互联网 时间:2017-05-28

翟永超 通过本文主要向大家介绍了spring boot web,spring boot 搭建web,spring boot web项目,spring boot web.xml,spring boot web开发等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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>
</div>

可以看到Thymeleaf主要以属性的方式加入到html标签中,浏览器在解析html时,当检查到没有的属性时候会忽略,所以Thymeleaf的模板可以通过浏览器直接打开展现,这样非常有利于前后端的分离。

在Spring Boot中使用Thymeleaf,只需要引入下面依赖,并在默认的模板路径src/main/resources/templates下编写模板文件即可完成。

 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</div>

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

 @Controller
public class HelloController {
  @RequestMapping("/")
  public String index(ModelMap map) {
    // 加入一个属性,用来在模板中读取
    map.addAttribute("host", "http://blog.didispace.com");
    // return模板文件的名称,对应src/main/resources/templates/index.html
    return "index"; 
  }
}
</div>
<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8" />
  <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
</div>

如上页面,直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/,则是展示Controller中host的值:http://www.weikejianghu.com,做到了不破坏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.
</div>

支持JSP的配置

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

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Spring Boot(二)之web综合开发
  • 利用Spring boot如何创建简单的web交互应用
  • Spring Boot开发Web应用详解
  • Spring Boot(二)之web综合开发
  • 利用Spring boot如何创建简单的web交互应用
  • Spring Boot开发Web应用详解

相关文章

  • 2017-05-28java中将一个List等分成n个list的工具方法(推荐)
  • 2017-05-28浅谈spring和spring MVC的区别与关系
  • 2017-05-28java 面试题闰年判断详解及实例
  • 2017-05-28详谈fastjson将对象格式化成json时的两个问题
  • 2017-05-28浅谈SpringMVC中的session用法及细节记录
  • 2017-05-28Java装饰器设计模式_动力节点Java学院整理
  • 2017-05-28springboot集成spring cache缓存示例代码
  • 2017-05-28Spring Boot中整合Spring Security并自定义验证代码实例
  • 2017-10-21Java中多线程通信实例:生产者消费者模式
  • 2017-05-28java 中Map详解及实例代码

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 关于javaWeb中405错误的解决方法
    • 深入理解java异常处理机制的原理和开发应用
    • Java数据结构之散列表(动力节点Java学院整理)
    • Java自定义注解实现Redis自动缓存的方法
    • 详解Java单元测试Junit框架实例
    • Spring Boot的Controller控制层和页面
    • Ubuntu快速安装jdk的教程
    • 详解java 单例模式及方法总结
    • 深入理解Java嵌套类和内部类
    • 详解spring boot配置单点登录

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有