目录
Spring
内部的一种配置方式 采用JavaBean
的形式来代替传统的xml
配置文件形式进行针对框架个性化定制@Configuration
@ComponentScan(basePackages="com.czxy.mvc.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/pages/",".jsp");
}
}
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/selectAll2")
public String selectAll2() {
return "list";
}
}
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 最终访问路径 /user/selectAll.action
* @return
*/
@RequestMapping("/selectAll")
public String selectAll() {
return "forward:/pages/list.jsp";
}
}
@Controller
@RequestMapping("/demo03")
public class Demo03BookController {
@RequestMapping("/list")
public ModelAndView list() {
//1 创建对象
ModelAndView modelAndView = new ModelAndView();
//2 设置视图
modelAndView.setViewName("book"); //之前 return "book";
//3 设置模型数据 key-value
modelAndView.addObject("username","jack");
return modelAndView;
}
}
public String list() {
return "forward:物理视图"; //jsp路径
}
public String list() {
return "redirect:物理视图"; //jsp路径
}
@RequestMapping("/void1")
public void void1() {
System.out.println("void1 执行了 ");
}
@RequestMapping("/void2")
public void void2(HttpServletRequest request , HttpServletResponse response) throws Exception {
request.setAttribute("username","void2");
// 请求转发
request.getRequestDispatcher("/WEB-INF/pages/book.jsp").forward(request, response);
// 请求包含
//request.getRequestDispatcher("/WEB-INF/pages/book.jsp").include(request, response);
}
@RequestMapping("/void3")
public void void3(HttpServletRequest request , HttpServletResponse response) throws Exception {
response.getWriter().print("void3");
}
@RequestMapping("/void4")
@ResponseBody
public void void4() throws Exception {
System.out.println("哈哈");
}
在Spring MVC中,提供了一个全局异常处理器,用于对系统中出现的异常进行统一的处理。在一般的系统中,DAO,Service及Controller层都出现的异常都以“throws Exception”的形式向上层抛出,最后都会由Spring MVC的前端控制器DispatcherServlet统一由全局异常处理器进行异常处理。
对于预期的异常,通常要定义一个自定义异常类,该类用于在发生异常后,存储异常信息,最终交给全局异常处理器处理该异常。
使用@ControllerAdvice对Controller增强
使用@ExceptionHandler捕获异常
/demo04/item.action?id=1
public class CustomExcption extends RuntimeException {
public CustomExcption() {
}
public CustomExcption(String message) {
super(message);
}
public CustomExcption(String message, Throwable cause) {
super(message, cause);
}
public CustomExcption(Throwable cause) {
super(cause);
}
public CustomExcption(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
2.编写Controller 接受参数
@Controller
@RequestMapping("/demo04")
public class Demo04ItemController {
@RequestMapping("/item")
public String item(Integer id ) {
if(id == 1) {
// 系统异常
int i = 1 / 0;
} else if( id == 2 ) {
// 自定义
throw new CustomExcption("自定义异常信息");
}
//正常
return "forward:/index.jsp";
}
}
3.编写访问连接
<a href="${pageContext.request.contextPath}/demo04/item.action?id=1">异常处理 id =1 系统异常</a> <br/>
<a href="${pageContext.request.contextPath}/demo04/item.action?id=2">异常处理 id =2 自定义异常 </a> <br/>
<a href="${pageContext.request.contextPath}/demo04/item.action?id=3">异常处理正常</a> <br/>
@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
// 1 统一异常
CustomExcption customExcption = null;
if(e instanceof CustomExcption) {
customExcption = (CustomExcption) e;
} else {
customExcption = new CustomExcption("系统繁忙,请稍后重试!");
}
// 2 错误信息返回
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message" ,customExcption.getMessage());
modelAndView.setViewName("forward:/error.jsp");
return modelAndView;
}
}
@ControllerAdvice
public class GlobalExceptionResolver {
/**
* 自定义异常处理器
* @param ec
* @param model
* @return
*/
@ExceptionHandler(CustomExcption.class)
public String custom(CustomExcption ec, Model model) {
model.addAttribute("message", ec.getMessage() + "Global");
return "forward:/error.jsp";
}
/**
* 其他异常处理器
* @param e
* @param model
* @return
*/
@ExceptionHandler(Exception.class)
public String other(Exception e, Model model) {
model.addAttribute("message", "系统繁忙,请稍后重试!" + "Global");
return "forward:/error.jsp";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>友好页面</title>
</head>
<body>
${message}
</body>
</html>
视图解析器:
可以帮助我们快速修改视图路径 不用一个一个Controller去修改
方法返回值:
方法返回值默认是ModelAndView Model默认是request作用域 绕过视图解析器 使用请求转发或者重定向
异常处理器:
更加友好的提示异常