我是spring mvc的新手,我正在尝试使用@ResponseStatus注释显示自定义错误消息,但无法实现所需的输出。
我该如何解决?
我应该使用json来解决这个问题吗?
我是否需要在web.xml中进行任何更改?
控制器类
@Controller
public class IndexController {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND,reason = "My Response Status Change....!!")
public String handleResourceNotFoundException() {
return "notfound";
}
@RequestMapping(value="/index")
public String indexController(Model model){
return "index";
}
}
ResourceNotFoundException类
package com.news.errorHandlers;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ControllerAdvice
public class ResourceNotFoundException extends RuntimeException{
}
notFound.jsp
<h1 class="btn btn-lg btn-danger">${value}</h1>