美文网首页
Spring Boot学习笔记-MVC

Spring Boot学习笔记-MVC

作者: Michael_ZX | 来源:发表于2017-12-08 20:49 被阅读0次

    重定向

    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&param2=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";
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring Boot学习笔记-MVC

          本文链接:https://www.haomeiwen.com/subject/bosyixtx.html