SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP

版权声明:本文为作者原创,转载请注明出处,联系qq:32248827 https://blog.csdn.net/dataiyangu/article/details/85559513

SpringBoot访问静态文件

什么是静态文件?
不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件。
所有的资源文件都应该在src/main/resources(maven项目中会自动创建这个文件夹)文件夹下面,但在SpringBoot中,系统默认扫描静态文件在static或者public文件夹下,这里我们在src/main/resources目录下创建一个static文件夹,我们copy一张图片到static目录下,将图片命名为hello.png,我们启动项目并通过浏览器进行访问这张图片
例如:
src/main/resources------>static------->hello.png
访问:localhost:8080/helo.png

SpringBoot采用默认扫描的方式,配置:默认扫描application.properties文件
同样是放在src/main/resources下。

SpringBoot捕获全局异常

以前捕获全局异常需要进行aop封装,这个功能在SpringBoot中的到了封装,我们直接用就好了
@ExceptionHandler 表示拦截异常
@ControllerAdvice 是 controller 的一个辅助类,最常用的就是作为全局异常处理的切面类
@ControllerAdvice 可以指定扫描范围
@ControllerAdvice 约定了几种可行的返回值,如果是直接返回 model 类的话,使用 @ResponseBody 进行 json转换

我们在项目下面新建一个包com.exception,新建一个异常拦截类GlobalExceptionHandler进行拦截Controller抛出的异常,我们这里拦截RuntimeException运行时异常,只需要在Controller里面制造一个异常,在hello请求里面增加 int a = 10 / 0;即可被捕获,其他的异常类型大家自己下去演示。
代码:

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, Object> exceptionHandler() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("errorCode", "404");
map.put("errorMsg", "找不到页面了!");
return map;
}
}
}

SpringBoot集成thymleaf

在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?
我们说到了SpringBoot是SSM框架和SSH框架的替代品,那么毫无疑问也是用于搭建Web项目,前面简单了提到了控制层的使用和异常的捕获,这里,我们讲一下前台页面的展示。
SSM框架使用的JSP作为前台展示页面,JSP在运行时将要编译成一个Servlet,这样对服务器是一个大大的消耗。而今天,我们只需要使用前台模版引擎,这种方式就好比静态页面的方式,不需要占用服务器太多的资源,这又是我们转投SpringBoot的有一个不可抗拒的理由。

几种前台模版引擎:

Thymeleaf
FreeMarker
Groovy
这里介绍三种,这些都是SpringBoot官方推荐使用的模版,而且特别说出了避免使用JSP,目前用的最多的就是前两种,
当我们使用上述模板引擎中的任何一个,SpringBoot默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。所以今天我们需要在src/main/resources目录下创建一个templates文件夹用来存放我们的前台模版页面。
当使用模版的时候我们需要在配置文件中添加几条配置,SpringBoot自动会读取默认的配置文件路径是在resources下的application.properties文件,这里我们在配置文件中配置下面的参数

spring.thymeleaf.suffix=.html  
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

我们使用Thymeleaf的话需要引入相关的jar包

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

这里我们在前台页面目录templates下新建一个index.html文件,内容如下

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h2>Welcome To Majiaxueyuan For Study</h2>
</body>
</html>

然后我们在com.controller下新建一个Controller命名为CommonController,要返回到页面我们和SSM框架一样,在类上注解Controller而不是RestController,RestController代表是返回JSON字符串给前台,而Controller注解是返回页面,具体代码如下:

@Controller
public class CommonController {
@RequestMapping("/index")
public String index() {
return "index";
}
}

然后这里就配置完成了我们的基本的页面和跳转,我们就可以通过浏览器进行访问了。
到这里我们访问127.0.0.1:8080/index就可以跳转到我们的index.html页面了。

thymleaf展示数据

@Controller
public class CommonController {
@RequestMapping("/index")
public String index(model) {
List<String> list = new ArrayList<>();
List.add("AAAA");
List.add("bbbbb")
model.addAttribute("list","list")
model.addAttribute("name","我们一起学猫叫")
return "index";
}
}
<input type="text" class="layui-input" th:value="${name}" />
<li th:text="${name}"></li>
<table border="1">  
 <tr>  
   <th>用户名</th>  
   <th>邮箱</th>  
 </tr>  
 <tr  th:each="user : ${userlist}">  
    <td th:text="${user.userName}">Onions</td>  
    <td th:text="${user.email}">test@test.com.cn</td>
 </tr>
</table>

<table>
		<tr>
			<th>编号</th>
		</tr>
		<tr th:each="l:${list}">
			<td th:text="${l;}"></td>
		</tr>

</table>
<li th:if="${name == null}">为空才显示</li>
<a th:></a>
<img th:src="@{/img/logo.png}"/>
//包括css等都要加th:    @{}

Thymeleafz=中@符号的作用就是获取当前项目的路径

SpringBoot集成JSP(不推荐)

引入jsp的包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

配置文件中配置下面的参数

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

这样的话我们就可以像以前一样使用JSP进行访问了

猜你喜欢

转载自blog.csdn.net/dataiyangu/article/details/85559513