Spring Boot学习笔记-MVC

原创
2017/12/08 21:13
阅读数 40

重定向

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";
    }
}
展开阅读全文
打赏
0
1 收藏
分享
加载中
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部