1.对所有异常进行处理
package com.springboot2.thyemleaf.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by lpw'ASUS on 2018/5/28.
*/
@Controller
@RequestMapping(value = "error")
public class ErrorBaseeController implements ErrorController {
private static final Logger log = LoggerFactory.getLogger(ErrorBaseeController.class);
@Override
public String getErrorPath() {
log.info("出错了进入自定义错误控制器");
return "error/error";
}
@RequestMapping
public String error(){
return getErrorPath();
}
}
实现errorController类,
当有异常的时候,在配置好的路径下找到error.html
结果
2.直接自定义错误页面,
在resources下面创建文件夹public,在里面创建404.html,
当报404错误的时候就会跳入。
3.通过控制器来自定义异常,统一的异常处理。
package com.springboot2.thyemleaf.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by lpw'ASUS on 2018/5/28.
*
* @ControllerAdvice 控制器的通知
*/
@ControllerAdvice
public class ErrorExceptionHandle {
private static final Logger log = LoggerFactory.getLogger(ErrorExceptionHandle.class);
@ExceptionHandler
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(Exception e){
log.info("----自定义异常处理 ---- exception---");
ModelAndView m =new ModelAndView();
m.addObject("zdyException",e.getMessage());
m.addObject("b","王五");
m.setViewName("error/error");
return m;
}
@ExceptionHandler
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException e){
log.info("----自定义RuntimeException异常处理 ---- exception---");
ModelAndView m =new ModelAndView();
m.addObject("zdyException",e.getMessage());
m.addObject("a","zhansan");
m.setViewName("error/runtime");
return m;
}
}
可以处理不同的异常,然后返回到指定的页面,进行处理。页面放在配置的模板路径下面。
package com.springboot2.thyemleaf.controller; import com.springboot2.thyemleaf.entity.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * Created by lpw'ASUS on 2018/5/28. */ @Controller @RequestMapping("/thyemleaf") public class ThymeleafController { @RequestMapping("demo") public String testthymeleaf(Model model)throws Exception{ model.addAttribute("str","hello springboot2.0 -thymeleaf"); User user = new User("张三",23); User user1 = new User("王五",46); model.addAttribute("user",user); List<User> list=new ArrayList<User>(); list.add(user); list.add(user1); model.addAttribute("list",list); System.out.print(2525); System.out.print(5212); model.addAttribute("imgsrc","/img/1.jpg"); if(true){ throw new Exception("非运行时异常"); } return "template"; } }
相互学习,重在交流。