SpringBoot之WEB开发-专题二

三、Web开发

3.1、静态资源访问

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

默认配置

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

/static

/public

/resources

/META-INF/resources

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

3.2、渲染Web页面

渲染Web页面

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

模板引擎

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

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

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

3.3、使用Freemarker模板引擎渲染web视图

3.3.1、pom文件引入:
  1. <!-- 引入freeMarker的依赖包. -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-freemarker</artifactId>
  5. </dependency>
3.3.2、后台代码

在src/main/resources/创建一个templates文件夹,后缀为*.ftl

  1. @RequestMapping("/index")
  2. public String index(Map<String, Object> map) {
  3. map.put("name","hello...");
  4. return "index";
  5. }
3.3.3、前台代码
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. ${name}
  9. </body>
  10. </html>
3.3.4、Freemarker其他用法
  1. @RequestMapping("/freemarkerIndex")
  2. public String index(Map<String, Object> result) {
  3. result.put("name", "yushengjun");
  4. result.put("sex", "0");
  5. List<String> listResult = new ArrayList<String>();
  6. listResult.add("zhangsan");
  7. listResult.add("lisi");
  8. listResult.add("hello");
  9. result.put("listResult", listResult);
  10. return "index";
  11. }
  12. <!DOCTYPE html>
  13. <html>
  14. <head lang="en">
  15. <meta charset="UTF-8" />
  16. <title>首页</title>
  17. </head>
  18. <body>
  19. ${name}
  20. <#if sex=="1">

  21. <#elseif sex=="2">

  22. <#else>
  23. 其他
  24. </#if>
  25. <#list userlist as user>
  26. ${user}
  27. </#list>
  28. </body>
  29. </html>
3.3.5、Freemarker配置

新建application.properties文件

这里使用properties配置文件,yml文件后面会写

  1. spring.freemarker.allow-request-override=false
  2. spring.freemarker.cache=true
  3. spring.freemarker.check-template-location=true
  4. spring.freemarker.charset=UTF-8
  5. spring.freemarker.content-type=text/html
  6. spring.freemarker.expose-request-attributes=false
  7. spring.freemarker.expose-session-attributes=false
  8. spring.freemarker.expose-spring-macro-helpers=false
  9. #spring.freemarker.prefix=
  10. #spring.freemarker.request-context-attribute=
  11. #spring.freemarker.settings.*=
  12. spring.freemarker.suffix=.ftl
  13. spring.freemarker.template-loader-path=classpath:/templates/
  14. #comma-separated list
  15. #spring.freemarker.view-names= # whitelist of view names that can be resolved

3.4、使用JSP渲染Web视图

3.4.1、pom文件引入以下依赖
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.0.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <!-- SpringBoot web 核心组件 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-tomcat</artifactId>
  15. </dependency>
  16. <!-- SpringBoot 外部tomcat支持 -->
  17. <dependency>
  18. <groupId>org.apache.tomcat.embed</groupId>
  19. <artifactId>tomcat-embed-jasper</artifactId>
  20. </dependency>
  21. </dependencies>
3.4.2、在application.properties创建以下配置
  1. spring.mvc.view.prefix=/WEB-INF/jsp/
  2. spring.mvc.view.suffix=.jsp
3.4.3、后台代码
  1. @Controller
  2. public class IndexController {
  3. @RequestMapping("/index")
  4. public String index() {
  5. return "index";
  6. }
  7. }

注意:创建SpringBoot整合JSP,一定要为war类型,否则会找不到页面.

不要把JSP页面存放在resources// jsp 不能被访问到

模板引擎这块会单独写,期待关注!

3.5、全局捕获异常

@ExceptionHandler 表示拦截异常

  • @ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类

  • @ControllerAdvice 可以指定扫描范围

  • @ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,需要使用 @ResponseBody 进行 json 转换

  • 返回 String,表示跳到某个 view

  • 返回 modelAndView

  • 返回 model + @ResponseBody

  1. @ControllerAdvice
  2. public class GlobalExceptionHandler {
  3. @ExceptionHandler(RuntimeException.class)
  4. @ResponseBody
  5. public Map<String, Object> exceptionHandler() {
  6. Map<String, Object> map = new HashMap<String, Object>();
  7. map.put("errorCode", "101");
  8. map.put("errorMsg", "系統错误!");
  9. return map;
  10. }
  11. }

SpringBoot之WEB开发-专题二的更多相关文章

  1. SpringBoot整合WEB开发--(二)静态资源访问

    1.默认策略: 静态资源的位置一共5个,开发者可以将静态资源放到其中任意一个,分别是: "classpath:/META-INF/resources/", "classp ...

  2. SpringBoot:Web开发

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 , 基于atguigu 1.5.x 视频优化 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处 ...

  3. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  4. springboot java web开发工程师效率

    基础好工具 idea iterm2 和 oh-my-zsh git 热加载 java web项目每次重启时间成本太大. 编程有一个过程很重要, 就是试验, 在一次次试验中探索, 积累素材优化调整程序模 ...

  5. 十二、springboot之web开发之静态资源处理

    springboot静态资源处理 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性. 建议大家使用Spring Boot的默 ...

  6. 【SpringBoot】Web开发

    一.简介 1.1 引入SpringBoot模块 1.2 SpringBoot对静态资源的映射规则 二.模版引擎 2.1 简介 2.2 引入thymeleaf 2.3 Thymeleaf使用 一.简介 ...

  7. SpringBoot的Web开发

    一.创建Web项目 创建的时候勾选对应web选项即可,会自动引入相应的starter,pom如下: <dependency> <groupId>org.springframew ...

  8. SpringBoot与Web开发

    web开发1).创建SpringBoot应用,选中我们需要的模块:2).SpringBoot已经默认将这些场景已经配置好了,只需要在配置文件中指定少量配置就可以运行起来3).自己编写业务代码: 自动配 ...

  9. SpringBoot日记——Web开发篇

    准备开始实战啦!~~~~ 我们先来看,SpringBoot的web是如何做web开发的呢?通常的步骤如下: 1.创建springboot应用,指定模块: 2.配置部分参数配置: 3.编写业务代码: 为 ...

随机推荐

  1. awk函数实现将简化IPV6地址补全

    在用awk处理文本时,有些场景需要将简化的IPV6地址补充成完整的IPV6地址,下边函数可简单实现: IPV6地址补全函数 # ipv6地址补全函数 function compipv6(orig_ad ...

  2. oracle增加字段,循环

    alter table PARAMETETER_CONFIGURATION add (INPUT_IS VARCHAR2(20) ): declare sum_i int:=0; --定义整型变量,存 ...

  3. 吴裕雄 python 人工智能——智能医疗系统后台用户注册、登录和初诊简约版代码展示

    #用户注册.登录模块 #数据库脚本 CREATE TABLE usertable( userid number(8) primary key not null , username varchar(5 ...

  4. Java IO流详解(六)——转换流

    转换流也是一种处理流,它提供了字节流和字符流之间的转换.在Java IO流中提供了两个转换流:InputStreamReader 和 OutputStreamWriter,这两个类都属于字符流.其中I ...

  5. redis(一)动态字符串

    redis 动态字符串 概述 Sda(Simple Dynamic String) 简单动态字符串是 redis中用来表示字符串的结构,而不是传统 C 字符串. 主要的特点就是Sda要做到高效和 二进 ...

  6. 【MySQL】库的操作

    "SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语言分为3种类型: DDL语句 数据库定义语言:数据库.表.视图.索引.存储过程,例如C ...

  7. laravel 报错The Mix manifest does not exist.

    这是因为我们在 resources/views/layouts/app.blade.php 中使用 mix() 方法,而我们还未运行 Laravel Mix 进行编译,找不到 mix-manifest ...

  8. 免费https/ssl通配证书(letsencrypt)安装

    教程:免费https/ssl通配证书(letsencrypt)安装 前置条件 开发443端口 关闭nginx .获取脚本 wget https://dl.eff.org/certbot-auto .执 ...

  9. java中关于类和对象的一些思考

    就这个问题而言 第一种和第二种定义的变量并不是一种形式 前者我们称为原始数据变量 后者我们称为对象变量 这两种变量的创建方式,定义方式,使用方式都有着很多不同 需要引起注意. 在java中,有着基本的 ...

  10. 取消Oracle数据库密码期限 取消用户锁定

    1.首先查用户被锁时间:sql>select username,account_status,lock_date from dba_users where username='SA'; 2.解锁 ...