Spring Boot (二):Web 开发篇
来源:互联网 发布:关联交易 数据库 编辑:程序博客网 时间:2024/06/10 07:41
Springboot之Web 开发篇
1、静态资源访问
在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。
2、默认配置
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static/public/resources/META-INF/resources
springboot 的源码如下:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
举例:我们可以在src/main/resources/目录下创建static文件夹,在该位置放置一个图片文件(demo.jpg)。启动程序后,尝试访问http://localhost:8080/demo.jpg
如果能显示图片说明配置成功。
3、渲染Web页面
在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?
和spring mvc 一样,我们用@Controller 和 @RequestMapping
4、模板引擎
在动态HTML实现上Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。
Spring Boot提供了默认配置的模板引擎主要有以下几种:
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
Spring Boot建议使用这些模板引擎,避免使用JSP,
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,在application.properties中配置 spring.thymeleaf.prefix=classpath:/templates/
更多的配置参数如下:
# 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 # Order of the template resolver in the chain. spring.thymeleaf.template-resolver-order= # Comma-separated list of view names that can be resolved.spring.thymeleaf.view-names=
Demo 示例
html页面
<!DOCTYPE html><html><head><meta charset="UTF-8"/><title>index</title></head><body><h1 th:text="${title}">Hello World</h1><h1><a href="http://www.thymeleaf.org" th:href="@{http://www.lrshuai.top}" th:text="${atext}">Thymeleaf</a></h1></body></html>
java 代码
@Controllerpublic class HelloController { @RequestMapping(value={"/index","/","/hello"}) public String index(Model model){ model.addAttribute("title", "测试"); model.addAttribute("atext", "这个冬天不太Cool"); return "index"; }}
Github 代码示例:https://github.com/rstyro/spring-boot/tree/master/springboot-web
正文到此结束,谢谢观看,觉得有用,点个赞可好!
我的博客地址:http://www.lrshuai.top/blog
阅读全文
0 0
- Spring Boot (二):Web 开发篇
- Spring Boot系列之二 Web开发
- spring boot之web开发(二)
- Spring Boot Web 开发注解篇
- Spring Boot Web 开发注解篇
- Spring Boot Web 开发注解篇
- Spring Boot Web 开发注解篇
- spring boot(二):web综合开发
- Spring Boot (二):Web 综合开发
- spring boot(二):web综合开发
- spring boot(二):web综合开发
- spring boot(二):web综合开发
- Spring Boot的Web开发
- spring boot开发web应用
- Spring Boot开发Web应用
- 3、spring-boot Web开发
- Spring Boot: 开发web 应用
- Spring Boot: 开发web 应用
- BGP笔记-4
- 匿名内部类----概述
- 动态规划-LCS的两种求解策略
- 学习嵌入式必备知识
- Java Web学习笔记
- Spring Boot (二):Web 开发篇
- crontab 自动任务的备份与恢复
- mysql中的锁
- BGP笔记-5
- Qt的一些学习资源
- 前端:ATOM环境搭建 + 插件列表
- Java多线程系列(四)—CAS操作和Automic原子类
- 确实很简单的简单计算233 (simple calculation)
- Ecipse 安装Spring Boot插件