Spring Boot学习笔记-MVC
2017-12-08 本文已影响0人
Michael_ZX
重定向
302重定向
@GetMapping(value = "redirect")
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("http://www.baidu.com");
}
控制器之间跳转
@GetMapping(value = "redirect2")
public String redirect2() {
return "redirect:/otherController/otherAction?param1=1¶m2=1";
}
自定义错误页面
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
@Configuration
public class ErrorConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return container -> {
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST,"/400"));
container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/500"));
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404"));
container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,"/401"));
};
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ErrorCtrl {
@RequestMapping(value = "/500")
@ResponseBody
public String serverError(){
return "500";
}
@RequestMapping(value = "/400")
@ResponseBody
public String badRequest(){
return "400";
}
@RequestMapping(value = "/404")
@ResponseBody
public String notFound(){
return "404";
}
}