目录
Controller统一异常处理
url解析
static下静态资源文件的访问
配置类
如何访问static下的资源文件
yaml基础语法
注解赋值
批量注入
单个注入
Controller统一异常处理
Controller统一异常处理@ControllerAdvice:统一为Controller进行"增强"
@ExceptionHandler : 异常处理
@RequestMapping("/vali")
@ResponseBody
public String dovali(@Valid Dog dog, BindingResult result){
//获取错误结果个数
int count = result.getErrorCount();
//判断
if (count>0){
List<FieldError> fieldErrors = result.getFieldErrors();
for (int i = 0; i < fieldErrors.size(); i++) {
FieldError fieldError = fieldErrors.get(i);
System.out.println(fieldError.getField());
System.out.println(fieldError.getRejectedValue());
System.out.println(fieldError.getDefaultMessage());
}
return "失败";
}else{
System.out.println(dog);
return "成功";
}
}
1.实体类数据校验2.在url路径里面传来的参数的key是对的,但是参数格式不对,email格式不对。比如,url路径为 http://localhost:8081/demo01/exercise?name=summer&email=283
url解析

参数绑定(1)

url2

url3
包名: import org.springframework.web.bind.annotation.ExceptionHandler;
package com.apesource.springboot_init_01.util;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class BindExceptionUtilAdvice {
@ExceptionHandler(BindException.class)
@ResponseBody
public String handlerexception(BindException bindException){
// System.out.println(bindException.getMessage());
return "全局异常处理器成功";
}
}
参数绑定(2)
@Validated
public class Person {
@NotNull(message = "用户名不能为空")
private String name;
private String lastName;
@NotNull(message = "年龄不能为空")
@Max(value = 130,message = "够了,机会还是要留给年轻人的")
private int age;
private boolean marry;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
代码片段3
Static下静态资源文件的访问
在enjoy和spring的整合下,就会涉及到static下静态资源文件的访问,加载enjoy模版引擎之后,就会要去加载一个视图文件
配置类
加载视图文件需要配置类。在Usercontroller接受到请求之后,就会找到对应的方法,执行完方法的逻辑之后,这个时候拦截器由于SpringBootconfig,就会返回界面。没有SpringBootconfig,就得不到xx.html。
package com.apesource.springboot_web_01.config;
import com.jfinal.template.Engine;
import com.jfinal.template.ext.spring.JFinalViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @version 1.0
* @Author
* @since 2023/4/11
*/
@Configuration
public class SpringBootConfig {
@Bean(name = "jfinalViewResolver")
public JFinalViewResolver getJFinalViewResolver() {
// 创建用于整合 spring boot 的 ViewResolver 扩展对象
JFinalViewResolver jfr = new JFinalViewResolver();
// 对 spring boot 进行配置
jfr.setSuffix(".html");
jfr.setContentType("text/html;charset=UTF-8");
jfr.setOrder(0);
// 设置在模板中可通过 #(session.value) 访问 session 中的数据
jfr.setSessionInView(true);
// 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
Engine engine = JFinalViewResolver.engine;
// 热加载配置能对后续配置产生影响,需要放在最前面
engine.setDevMode(true);
// 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
engine.setToClassPathSourceFactory();
// 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
// 代替 jfr.setPrefix("/view/")
engine.setBaseTemplatePath("/templates/");
// 更多配置与前面章节完全一样
// engine.addDirective(...)
// engine.addSharedMethod(...);
return jfr;
}
}
代码片段4
如何访问static下的资源文件
1.只要静态资源放在类路径下:/static、/public、/resources、/META-INF/resources可以被直
接访问-对应文件WebProperties.java源码
private static final String[] CLASSPATH_RESOURCE_LOCATIONS={"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"};
2.常见静态资源:JS、CSS、图片(.jpg .png .gif .bmp .svg)、字体文件(Fonts)等3.访问方式:默认:项目根路径/+静态资源名 比如 http://localhost:8080/hi.jpg
原文链接:https://blog.csdn.net/weixin_49764008/article/details/128668539s
顺带说一下,因为在WEB_INF下的资源文件有安全访问限制,所以不可以直接通过url访问这些资源文件,只可以通过在controller中的方法返回的网页的前缀名(String)访问这些资源文件。
位置:application.yml
spring.mvc.static-path-pattern=/prefix/** 语句1
未加语句1之前 http://localhost:8080/1.jpg,加之后 http://localhost:8080/prefix/1.jg
Yaml基础语法
数组commpany: [xxx,xxxx,xxxx]address:- beijing- shanghai对象/mapperson:name: wangzhuo# 行内写法person: {name: wangzhuo}对象数组users2 : [ { name : Tom , age : 4 },{ name : Jerry , age : 5 } ]
YAML :参数引用name : wangzhuoperson :pet : $ { name }
注解赋值
批量注入
@ConfigurationProperties( prefix = "person1" )匹配 @PropertySource ( value = "classpath:dog.yml" )中前缀名为person1的格式和内容。支持复杂类型
单个注入
@Value不支持复杂类型@Value("${lists}")List<String> lists;如果复杂类型有了value注解,就会导致参数绑定异常
在xml中的应用表单
<form action="/one/show4" method="post"> 员工编号:<input type="text" name="eid" ><br/> 员工姓名:<input type="text" name="ename" ><br/> 员工性别:<input type="text" name="esex" ><br/> 部门编号:<input type="text" name="dept.did" ><br/> 部门名称:<input type="text" name="dept.dname" ><br/> <input type="submit" value="发送请求4"/> </form>员工编号1:<input type="text" name="mylist[0].eid" ><br/> 员工姓名1:<input type="text" name="mylist[0].ename" ><br/> 员工性别1:<input type="text" name="mylist[0].esex" ><br/> 员工编号2:<input type="text" name="mylist[1].eid" ><br/> 员工姓名2:<input type="text" name="mylist[1].ename" ><br/> 员工性别2:<input type="text" name="mylist[1].esex" ><br/> 给List<emp>当中传入两个对象 员工编号3:<input type="text" name="myMap['one'].eid" ><br/> 员工姓名3:<input type="text" name="myMap['one'].ename" ><br/> 员工性别3:<input type="text" name="myMap['one'].esex" ><br/> 员工编号4:<input type="text" name="myMap['two'].eid" ><br/> 员工姓名4:<input type="text" name="myMap['two'].ename" ><br/> 员工性别4:<input type="text" name="myMap['two'].esex" ><br/>map的key为中括号当中的内容,value为用户输入的内容 <form action="/one/map" method="post"> 员工编号:<input type="text" name="xxx"><br/> 员工姓名:<input type="text" name="yyy"><br/> 员工性别:<input type="text" name="zzz"><br/> <input type="submit" value="发送请求4(map)"/> </form>以map接收,key为name,value为用户输入的内容