自定义类型转换器,实现String----->employee对象的转换和封装
<form action="${ctp}/quickAdd">
<%--将员工所有信息都写上,自动封装对象--%>
<input type="text" name="empInfo" value="大忽悠-总裁办-@123-1"/>
<input type="submit" value="快速添加员工"/>
</form>
快速添加员工的方法:
@RequestMapping("quickAdd")
//获取请求参数empInfo
//employee=request.getParame("empInfo")---->大忽悠-总裁办-@123-1
public String quickAdd(@RequestParam("empInfo") Employee employee)
{
employeeDao.save(employee);
return "redirect:/emps";
}
/*
* 两个泛型S,T
* S:source
* T:target
* 把s转换为t
* */
//自定义类型转换器
public class MyConverter implements Converter<String, Employee> {
//自定义转换规则
public Employee convert(String source) {
System.out.println("要转换的字符串:"+source);
Employee employee=new Employee();
if(source.contains("-"))
{
//大忽悠-总裁办-@123-1
String[] ret=source.split("-");
employee.setName(ret[0]);
employee.setDepartment(ret[1]);
employee.setEamil(ret[2]);
employee.setGender(Integer.parseInt(ret[3]));
}
return employee;
}
}
converters在源码中是一个set集合
<!--告诉SpringMVC别用默认的ConversionService,
而是使用自定义的ConversionService
,里面有我们自定义的converter-->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<!--在converters转换器中添加我们自定义的类型转换器-->
<property name="converters">
<set>
<bean class="com.Converter.MyConverter"/>
</set>
</property>
</bean>
<!--使用我们自己的配置的类型转换组件-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!--在converters转换器中添加我们自定义的类型转换器-->
<property name="converters">
<set>
<bean class="com.Converter.MyConverter"/>
</set>
</property>
</bean>
<!--使用我们自己的配置的类型转换组件-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
//规定提交的日期格式
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birth;
@NumberFormat(pattern = "#,###,###.##")//类似这样的数字格式: 1,000,000.98
private double salary;
<!-- maven管理引入springmvc注解数据校验所需jar包:-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
@NotEmpty
@Length(min=6,max=8)//至少6个字符,最多8个字符
private String name;
private String department;
@Email
private String eamil;
//只接收Post请求
@RequestMapping(value = "/emp",method = RequestMethod.POST)
//@Valid注解,告诉SpringMVC,封装这个javabean对象时,按照这个对象里面变量规定的校验规则进行校验
public String addEmp(@Valid Employee employee)//这里会自动赋值
{
System.out.println("要添加的员工信息:"+employee);
employeeDao.save(employee);
//返回列表页面,直接重定向到查询所有员工的请求
return "redirect:/emps";
}
//只接收Post请求
@RequestMapping(value = "/emp",method = RequestMethod.POST)
//@Valid注解,告诉SpringMVC,封装这个javabean对象时,按照这个对象里面变量规定的校验规则进行校验
public String addEmp(@Valid Employee employee,BindingResult res)//这里会自动赋值
{
System.out.println("要添加的员工信息:"+employee);
employeeDao.save(employee);
//返回列表页面,直接重定向到查询所有员工的请求
return "redirect:/emps";
}
//只接收Post请求
@RequestMapping(value = "/emp",method = RequestMethod.POST)
//@Valid注解,告诉SpringMVC,封装这个javabean对象时,按照这个对象里面变量规定的校验规则进行校验
public String addEmp(@Valid Employee employee,BindingResult res)//这里会自动赋值
{
System.out.println("要添加的员工信息:"+employee);
//获取是否有校验错误
boolean hasErrors = res.hasErrors();
if(hasErrors)
{
System.out.println("有校验错误");
return "addPage";
}
else
{
employeeDao.save(employee);
//返回列表页面,直接重定向到查询所有员工的请求
return "redirect:/emps";
}
}
<%pageContext.setAttribute("ctp",request.getContextPath());%>
<form:form modelAttribute="employee" action="${ctp}/emp" method="post">
<%--显示错误信息,有默认的错误信息,也可也自己写显示的错误西信息--%>
员工id:<form:input path="id"/><form:errors path="id"></form:errors><br/>
姓名:<form:input path="name"/><form:errors path="name"></form:errors><br/>
邮箱:<form:input path="eamil"/><form:errors path="eamil"></form:errors><br/>
性别:<br/>
男:<form:radiobutton path="gender" value="1"/>
女:<form:radiobutton path="gender" value="0"/>
请选择员工所在的部门:
<form:select path="department" items="${departments}"/>
<br/>
<input type="submit" value="提交">
</form:form>
//只接收Post请求
@RequestMapping(value = "/emp",method = RequestMethod.POST)
//@Valid注解,告诉SpringMVC,封装这个javabean对象时,按照这个对象里面变量规定的校验规则进行校验
public String addEmp(@Valid Employee employee,BindingResult res,Model model)//这里会自动赋值
{
System.out.println("要添加的员工信息:"+employee);
//获取是否有校验错误
boolean hasErrors = res.hasErrors();
Map<String,String> errorMap=new HashMap<String, String>();
if(hasErrors)
{
List<FieldError> fieldErrors = res.getFieldErrors();
for(FieldError fieldError:fieldErrors)
{
System.out.println("错误消息提示:"+fieldError.getDefaultMessage());
System.out.println("错误的字段:"+fieldError.getField());
System.out.println(fieldError);
System.out.println("----------------------------------------");
errorMap.put(fieldError.getField(),fieldError.getDefaultMessage());
}
model.addAttribute("errorInfo",errorMap);
System.out.println("有校验错误");
return "addPage";
}
else
{
employeeDao.save(employee);
//返回列表页面,直接重定向到查询所有员工的请求
return "redirect:/emps";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>员工添加页面</title>
</head>
<body>
<%pageContext.setAttribute("ctp",request.getContextPath());%>
<form:form modelAttribute="employee" action="${ctp}/emp" method="post">
员工id:<form:input path="id"/>${errorInfo.id}<br/><%--取不出来就为空--%>
姓名:<form:input path="name"/>${errorInfo.name}<br/>
邮箱:<form:input path="eamil"/>${errorInfo.eamil}<br/>
性别:<br/>
男:<form:radiobutton path="gender" value="1"/>
女:<form:radiobutton path="gender" value="0"/>
请选择员工所在的部门:
<form:select path="department" items="${departments}"/>
<br/>
<input type="submit" value="提交">
</form:form>
</body>
</html>
error_en_US.properties.properties:
Email.email=email error--->
NotEmpty=not empty--->
Length.java.lang.String=type is not I want--->
error_zh_CN.properties.properties:
Email.email=邮箱书写的格式有误
NotEmpty=不能为空哦
Length.java.lang.String=长度不合法哦
<!--让SpringMVC管理国际化资源文件-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!--basename:指定国际化资源文件的基础名-->
<property name="basename" value="error"></property>
<!-- 支持UTF-8的中文 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
@NotEmpty(message = "你小子用户名填的有问题呀")
private String name;
<!--引入jquery的依赖-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-2</version>
</dependency>
</dependencies>
<!--jackSon的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
@JsonIgnore//输出数据的时候,忽略id字段
private Integer id;
@JsonFormat与@DateTimeFormat注解的使用
ajaxController:
@Controller
public class ajaxController {
@Autowired
EmployeeDao employeeDao;
@ResponseBody//将返回的数据放在响应体中,如果返回的是对象,jackson自动将对象转换为json格式
@RequestMapping("/getAllAjax")
public List<Employee> getAjaxAll()
{
List<Employee> allEmployees = employeeDao.getAllEmployees();
return allEmployees;
}
}
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<html>
<head>
<title>JQuery显示页面</title>
</head>
<body>
<script type="text/javascript" src="${ctx}/webjars/jquery/3.3.1-2/jquery.min.js"></script>
<script type="text/javascript">
$("#b1").click(function (){
//1.发送ajax请求,获取所有员工
$.ajax(
{
url:"${ctx}/getAllAjax",
type:"get",
success:function (data)
{
$.each(data,function (){
var empInfo=data.name+" "+data.department;
$("div").append(empInfo);
})
}
}
)
})
</script>
<button id="b1">ajax请求发送</button>
<div>大忽悠</div>
</body>
</html>
ajaxController:
@Controller
public class ajaxController {
@Autowired
EmployeeDao employeeDao;
//放在视图解析器进行拼串
@ResponseBody//将返回的数据放在响应体中,如果返回的是对象,jackson自动将对象转换为json格式
@RequestMapping("/getAllAjax")
public List<Employee> getAjaxAll()
{
System.out.println("ajax方法调用");
List<Employee> allEmployees = employeeDao.getAllEmployees();
return allEmployees;
}
//获取一个请求的请求体
@RequestMapping("/Body")
public String testRequestBody(@RequestBody String Body)
{
System.out.println("请求体"+Body);
return "success";
}
}
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<html>
<head>
<title>无标题</title>
</head>
<body>
<form action="${ctx}/Body" method="post" enctype="multipart/form-data">
<input name="username" value="大忽悠"/>
<input type="file" name="file"><%--文件上传--%>
<button>提交</button>
</form>
</body>
</html>
//获取一个请求的请求体,直接封装为people对象
@RequestMapping("/peo")
public String getPeople(@RequestBody people p)
{
System.out.println("p的值为:"+p);
return "success";
}
index.ja
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<head>
<title>JQuery显示页面</title>
</head>
<body>
<script type="text/javascript" src="${ctx}/webjars/jquery/3.3.1-2/jquery.js"></script>
<script>
$(function ()
{
$("#b1").click(function (){
console.log("=====");
var emp={"name":"大忽悠","age":"18"};
//打印emp的类型----》是一个json对象
alert(typeof emp);
//将这个json对象,转换为一个json形式的字符串
var stringEmp=JSON.stringify(emp);
//打印stringEmp的类型----->字符串
alert(typeof stringEmp);
//1.发送ajax请求,获取所有员工
$.ajax(
{
url:"${ctx}/peo",
type:"post",
data:stringEmp,
//指定要提交的数据格式为json----告诉浏览器
contentType:"application/json",
success:function (data)
{
alert(data);
}
}
)
return false;
})
})
</script>
<a id="b1" href="${ctx}/peo">点击,ajax请求发送</a>
</body>
</html>
@RequestMapping("/peo")
public String getPeople(HttpEntity<String> ret)
{
System.out.println(ret);
return "success";
}
@ResponseBody
@RequestMapping("/peo")
public String getPeople(HttpEntity<String> ret)
{
return "<h1>success<h1>";
}
@RequestMapping("/peo")
public ResponseEntity<String> getPeople(HttpEntity<String> ret)
{
//三个参数: 响应体,响应头,状态码
String body="Success";
MultiValueMap<String, String> headers=new HttpHeaders();
//让浏览器保存一个cookie
headers.add("Set-Cookie","username=大忽悠");
return new ResponseEntity<String>(body,headers,HttpStatus.OK);
}
@RequestMapping("/peo")
public ResponseEntity<Byte[]> download(HttpServletRequest request) throws IOException {
//1.得到要下载的文件的流
//找到要下载的文件在服务器中的真实路径
ServletContext context=request.getSession().getServletContext();
//当前项目路径下的picture包下的pig.img资源
String realPath=context.getRealPath("/picture/pig.img");
//获取要下载的文件流---》创建一个要读取的输入流
FileInputStream is=new FileInputStream(realPath);
byte[] temp=new byte[is.available()];
is.read(temp);
is.close();
//将要下载的文件流返回给浏览器
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Disposition","attachment;filename="+"pig.img");
return new ResponseEntity<byte[]>(temp,httpHeaders,HttpStatus.OK);
}
<!--文件上传的依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<head>
<title>JQuery显示页面</title>
</head>
<body>
<script type="text/javascript" src="${ctx}/webjars/jquery/3.3.1-2/jquery.js"></script>
<form action="${ctx}/upload" method="post" enctype="multipart/form-data">
用户头像:<input type="file" name="head"/><br/>
用户名:<input type="text" name="username"/><br/>
<input type="submit"/>
</form>
结果: ${msg}
</body>
</html>
<!--文件上传解析器: id必须是multipartFile-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传最大量为5m-->
<property name="maxUploadSize" value="5242880"></property><!--spel运算符,计算最大可上传文件体积-->
<!--设置默认的编码-->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
@Controller
public class uploadController {
@RequestMapping("/upload")
public String upload(
@RequestParam(value = "username",required = false) String username,
@RequestParam("head") MultipartFile file
,Model model)
{
System.out.println("上传的文件信息:");
System.out.println("文件项的name:"+file.getName());
System.out.println("文件的名字"+file.getOriginalFilename());
//文件保存
try {
file.transferTo(new File("D:\\dhy\\"+file.getOriginalFilename()));
model.addAttribute("msg","文件上传成功");
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("msg","文件上传失败");
}
return "forward:/index.jsp";
}
}
@Controller
public class uploadController {
@RequestMapping("/upload")
public String upload(
@RequestParam(value = "username",required = false) String username,
@RequestParam(value = "head") MultipartFile[] file1
,Model model)
{
System.out.println("上传的文件信息:");
for(MultipartFile file:file1)
{
if(!file.isEmpty())
{
try {
file.transferTo(new File("D:\\dhy\\"+file.getOriginalFilename()));
model.addAttribute("msg","文件上传成功");
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("msg","文件上传失败");
}
}
}
return "forward:/index.jsp";
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<head>
<title>JQuery显示页面</title>
</head>
<body>
<form action="${ctx}/upload" method="post" enctype="multipart/form-data">
用户头像:<input type="file" name="head"/><br/>
用户头像:<input type="file" name="head"/><br/>
用户头像:<input type="file" name="head"/><br/>
用户名:<input type="text" name="username"/><br/>
<input type="submit" value="提交"/>
</form>
结果: ${msg}
</body>
</html>
public class MyInterceptor implements HandlerInterceptor {
/**
* 该方法在目标方法之前被调用.
* 若返回值为 true, 则继续调用后续的拦截器和目标方法.
* 若返回值为 false, 则不会再调用后续的拦截器和目标方法.
*
* 可以考虑做权限. 日志, 事务等.
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("[MyInterceptor] preHandle");
return true;
}
/**
* 调用目标方法之后, 但渲染视图之前.
* 可以对请求域中的属性或视图做出修改.
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("[MyInterceptor] postHandle");
}
/**
* 渲染视图之后被调用. 释放资源
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("[MyInterceptor] afterCompletion");
}
}
<!--测试拦截器-->
<mvc:interceptors>
<!--配置某个拦截器,默认是拦截所有请求-->
<bean class="com.Controller.MyInterceptor"/>
<!--具体配置某个拦截器-->
<!-- <mvc:interceptor>
<!–只拦截test01请求–>
<mvc:mapping path="/test01"/>
<bean class="com.Controller.MyInterceptor"/>
</mvc:interceptor>-->
</mvc:interceptors>
<!--测试拦截器-->
<mvc:interceptors>
<!--配置某个拦截器,默认是拦截所有请求-->
<bean class="com.Controller.MyInterceptor"/>
<!--具体配置某个拦截器-->
<mvc:interceptor>
<!--只拦截hello请求-->
<mvc:mapping path="/hello"/>
<bean class="com.Controller.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
error_en_US.properties:
welcome=welocme page
username=username
password=password
error_zh_CN.properties:
welcome=登录界面
username=用户名
password=密码
<!--让SpringMVC管理国际化资源文件,id不能改-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!--basename:指定国际化资源文件的基础名-->
<property name="basename" value="error"></property>
<!-- 支持UTF-8的中文 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<head>
<h1>
<fmt:message key="welcome"/>
</h1>
</head>
<body>
<form action="">
<fmt:message key="username"/>:<input/><br/>
<fmt:message key="password"/>:<input/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
@Controller
public class hello {
@Autowired
private MessageSource messageSource;
@RequestMapping("/hello")
public String hai(Locale locale)
{
System.out.println(locale);//打印当前区域信息
String username = messageSource.getMessage("username", null, locale);//第二个参数是占位符数组
System.out.println("用户名:"+username);
return "guojihua";
}
}
打印结果为: zh_CN 用户名: 用户名
MyLocalResolve:
/*自定义区域解析器*/
public class MyLocalResolve implements LocaleResolver {
//解析返回locale
public Locale resolveLocale(HttpServletRequest request)
{
String localeStr=request.getParameter("locale");
Locale l=null;
//如果带了locale参数就用参数指定的区域信息,否则就用请求头的
if(localeStr!=null&&!"".equals(localeStr))
{
l=new Locale(localeStr.split("_")[0],localeStr.split("_")[1]);
}
else{
l=request.getLocale();
}
return l;
}
//修改locale
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)
{
}
}
国际化展示页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<head>
<h1>
<fmt:message key="welcome"/>
</h1>
</head>
<body>
<form action="">
<fmt:message key="username"/>:<input/><br/>
<fmt:message key="password"/>:<input/><br/>
<input type="submit" value="提交"/>
</form>
<a href="hello?locale=zh_CN">中文</a><br/>
<a href="hello?locale=en_US">英文</a><br/>
</body>
</html>
hello页面跳转类:
@Controller
public class hello {
@Autowired
private MessageSource messageSource;
@RequestMapping("/hello")
public String hai(Locale locale)
{
System.out.println(locale);//打印当前区域信息
String username = messageSource.getMessage("username", null, locale);//第二个参数是占位符数组
System.out.println("用户名:"+username);
return "guojihua";
}
}
<!--配置自定义区域信息解析器 id固定-->
<bean id="localeResolver" class="com.Controller.MyLocalResolve"></bean>
SessionLocaleResolver保存客户的Locale到HttpSession对象中,并且支持获取和修改
<!--区域信息从session中获取-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
@Controller
public class hello {
@Autowired
private MessageSource messageSource;
@RequestMapping("/hello") //如果有请求参数locale,那么就使用,否则默认为中文
public String hai(@RequestParam(value = "locale",defaultValue = "zh_CN") String localeStr,
Locale locale,//这里locale是获取当前请求头里面的区域信息
HttpSession httpSession)
{
Locale l=null;
//如果带了locale参数就用参数指定的区域信息,否则就用请求头的
if(localeStr!=null&&!"".equals(localeStr))
{
l=new Locale(localeStr.split("_")[0],localeStr.split("_")[1]);
}
else{
l=locale;
}
httpSession.setAttribute(SessionLocaleResolver.class.getName()+".LOCALE",l);
return "guojihua";
}
}
guojihua.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<head>
<h1>
<fmt:message key="welcome"/>
</h1>
</head>
<body>
<form action="">
<fmt:message key="username"/>:<input/><br/>
<fmt:message key="password"/>:<input/><br/>
<input type="submit" value="提交"/>
</form>
<a href="hello?locale=zh_CN">中文</a><br/>
<a href="hello?locale=en_US">英文</a><br/>
</body>
</html>
<!--区域信息从session中获取-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<mvc:interceptors>
<!--配置拦截器-->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
跳转类:
@Controller
public class hello {
@RequestMapping("/hello")
public String hai()
{
return "guojihua";
}
}
guojihua.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% pageContext.setAttribute("ctx",request.getContextPath());%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<head>
<h1>
<fmt:message key="welcome"/>
</h1>
</head>
<body>
<form action="">
<fmt:message key="username"/>:<input/><br/>
<fmt:message key="password"/>:<input/><br/>
<input type="submit" value="提交"/>
</form>
<a href="hello?locale=zh_CN">中文</a><br/>
<a href="hello?locale=en_US">英文</a><br/>
</body>
</html>
@Controller
public class exception
{
@RequestMapping("/dhy")
public String handle01(Integer i)
{
System.out.println(10/i);
return "success";
}
//里面参数可以填数组
//每一个参数代表当前处理异常的方法能够处理的异常类型,返回值可以跳转到定制的错误页面
@ExceptionHandler({ArithmeticException.class,NullPointerException.class})
public String exceptionHandle()
{
System.out.println("发生错误");
//视图解析器拼串
return "error";
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<a href="dhy?i=0">点我</a>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error</title>
</head>
<body>
error
</body>
</html>
@Controller
public class exception
{
@RequestMapping("/dhy")
public String handle01(Integer i)
{
System.out.println(10/i);
return "success";
}
//里面参数可以填数组
//每一个参数代表当前处理异常的方法能够处理的异常类型,返回值可以跳转到定制的错误页面
@ExceptionHandler({ArithmeticException.class,NullPointerException.class})
public ModelAndView exceptionHandle(Exception exception)
{
ModelAndView m=new ModelAndView("error");//参数:设置跳转页面
m.addObject("ex",exception);//将错误信息放到隐含模型中
return m;
}
}
error页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>错误信息为: ${ex}</h1>
</body>
</html>
@ControllerAdvice
public class exception
{
@ExceptionHandler({ArithmeticException.class,NullPointerException.class})
public ModelAndView exceptionHandle(Exception exception)
{
ModelAndView m=new ModelAndView("error");//参数:设置跳转页面
m.addObject("ex",exception);//将错误信息放到隐含模型中
return m;
}
}
ex异常类:
package com.Exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
//第一个参数自定义的错误信息,第二个参数是错误显示的状态码
@ResponseStatus(reason = "用户登录异常",value = HttpStatus.NOT_FOUND)
public class ex extends RuntimeException{
private static final long serialVersionUID=1l;
}
hello类:
@Controller
public class hello
{
@RequestMapping("/dhy")
public String show(String name)
{
System.out.println(name);
if(!name.equals("大忽悠"))
{
throw new ex();
}
return "success";
}
}
登录页面:
<form action="dhy">
用户名:<input type="text" name="name"><br/>
<input type="submit" value="登录"/>
</form>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--exceptionMappings配置哪些异常区哪些页面-->
<property name="exceptionMappings">
<props>
<!--key: 异常全类名 value:要去的页面视图名-->
<prop key="java.lang.NullPointerException">error</prop>
</props>
</property>
<!--指定错误信息取出时指定的key,默认是exception-->
<property name="exceptionAttribute" value="ex"></property>
</bean>
error页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>错误信息为: ${ex}</h1>
</body>
</html>
Hai:
@Controller
public class Hai
{
@RequestMapping("/dhy")
public String show(String name)
{
String s=null;
s.length();//引发空指针异常
return "success";
}
}
如果采用上面的合并配置文件,那么相当于系统一起动只有一个IOC容器,这样一部分报错,整个容器就凉凉
需要先禁止掉扫描所有包的默认行为
Spring.xml包扫描配置:
<context:component-scan base-package="com.service" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
SpringMVC包扫描配置:
<context:component-scan base-package="com.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--第二个是SpringMVC错误控制的注解-->
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>