I'm looking for a clean way to return customized 404 errorpages in Spring 4 when a requested resource was not found. Queries to different domain types should result in different error pages.
当找不到请求的资源时,我正在寻找一种在Spring 4中返回自定义404错误的简洁方法。对不同域类型的查询应该导致不同的错误页面。
Here some code to show my intention (Meter is a domain class):
这里有一些代码来表明我的意图(Meter是一个域类):
@RequestMapping(value = "/{number}", method = RequestMethod.GET)
public String getMeterDetails(@PathVariable("number") final Long number, final Model model) {
final Meter result = meterService.findOne(number);
if (result == null) {
// here some code to return an errorpage
}
model.addAttribute("meter", result);
return "meters/details";
}
I imagine several ways for handling the problem. First there would be the possibility to create RuntimeException
s like
我想到了几种处理问题的方法。首先,可以创建类似的RuntimeExceptions
@ResponseStatus(HttpStatus.NOT_FOUND)
public class MeterNotFoundExcption extends RuntimeException { }
and then use an exception handler to render a custom errorpage (maybe containing a link to a list of meters or whatever is appropriate).
然后使用异常处理程序呈现自定义errorpage(可能包含指向米列表或任何适当的列表的链接)。
But I don't like polluting my application with many small exceptions.
但我不喜欢用很多小例子来污染我的应用程序。
Another possibility would be using HttpServletResponse
and set the statuscode manually:
另一种可能是使用HttpServletResponse并手动设置状态代码:
@RequestMapping(value = "/{number}", method = RequestMethod.GET)
public String getMeterDetails(@PathVariable("number") final Long number, final Model model,
final HttpServletResponse response) {
final Meter meter = meterService.findOne(number);
if (meter == null) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return "meters/notfound";
}
model.addAttribute("meter", meter);
return "meters/details";
}
But with this solution I have to duplicate the first 5 lines for many controller methods (like edit, delete).
但是使用这个解决方案,我必须复制许多控制器方法的前5行(如编辑,删除)。
Is there an elegant way to prevent duplicating these lines many times?
是否有一种优雅的方法可以防止多次重复这些行?
7 个解决方案
#1
33
The solution is much simpler than thought. One can use one generic ResourceNotFoundException
defined as follows:
解决方案比想象的要简单得多。可以使用如下定义的一个通用ResourceNotFoundException:
public class ResourceNotFoundException extends RuntimeException { }
then one can handle errors within every controller with an ExceptionHandler
annotation:
然后可以使用ExceptionHandler注释处理每个控制器中的错误:
class MeterController {
// ...
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "meters/notfound";
}
// ...
@RequestMapping(value = "/{number}/edit", method = RequestMethod.GET)
public String viewEdit(@PathVariable("number") final Meter meter,
final Model model) {
if (meter == null) throw new ResourceNotFoundException();
model.addAttribute("meter", meter);
return "meters/edit";
}
}
Every controller can define its own ExceptionHandler
for the ResourceNotFoundException
.
每个控制器都可以为ResourceNotFoundException定义自己的ExceptionHandler。
#2
17
modified your web.xml file.Using following code.
修改了您的web.xml文件。使用以下代码。
<display-name>App Name </display-name>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
Access this by following code.
通过以下代码访问它。
response.sendError(508802,"Error Message");
Now add this code in web.xml.
现在在web.xml中添加此代码。
<error-page>
<error-code>508802</error-code>
<location>/error500.jsp</location>
</error-page>
#3
9
You can map the error codes in web.xml like the following
您可以在web.xml中映射错误代码,如下所示
<error-page>
<error-code>400</error-code>
<location>/400</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500</location>
</error-page>
Now you can create a controller to map the url's that are hit when any of these error is found.
现在,您可以创建一个控制器来映射发现任何这些错误时被击中的URL。
@Controller
public class HTTPErrorHandler{
String path = "/error";
@RequestMapping(value="/404")
public String error404(){
// DO stuff here
return path+"/404";
}
}
For full example see this tutorial
有关完整示例,请参阅本教程
#4
4
You should follow this article where you can find detailed information about exception handling in Spring MVC projects.
您应该阅读本文,您可以在其中找到有关Spring MVC项目中的异常处理的详细信息。
@ControllerAdvice may help you in this case
@ControllerAdvice可以在这种情况下帮助你
#5
4
We can just add following lines of code into web.xml file and introduce a new jsp file named errorPage.jsp into root directory of the project to get the requirement done.
我们可以在web.xml文件中添加以下代码行,并将一个名为errorPage.jsp的新jsp文件引入项目的根目录,以完成需求。
<error-page>
<error-code>400</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorPage.jsp</location>
</error-page>
#6
4
Simple answer for 100% free xml:
100%免费xml的简单答案:
-
Set properties for DispatcherServlet
设置DispatcherServlet的属性
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] {AppConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } //that's important!! @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { boolean done = registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); // -> true if(!done) throw new RuntimeException(); }
}
-
Create @ControllerAdvice:
@ControllerAdvice public class AdviceController { @ExceptionHandler(NoHandlerFoundException.class) public String handle(Exception ex) { return "redirect:/404"; } @RequestMapping(value = {"/404"}, method = RequestMethod.GET) public String NotFoudPage() { return "404"; }
}
-
Create 404.jsp page with any content
使用任何内容创建404.jsp页面
创建@ControllerAdvice:@ControllerAdvice public class AdviceController { @ExceptionHandler(NoHandlerFoundException.class) public String handle(Exception ex){ return“redirect:/ 404”; } @RequestMapping(value = {“/ 404”},method = RequestMethod.GET) public String NotFoudPage(){ 返回“404”; } }
That's all.
#7
1
I also needed to NOT use org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer
.
我还需要不要使用org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer。
According to org.springframework.web.servlet.DispatcherServlet.setThrowExceptionIfNoHandlerFound(boolean)
: "Note that if DefaultServletHttpRequestHandler is used, then requests will always be forwarded to the default servlet and a NoHandlerFoundException would never be thrown in that case."
根据org.springframework.web.servlet.DispatcherServlet.setThrowExceptionIfNoHandlerFound(boolean):“请注意,如果使用DefaultServletHttpRequestHandler,那么请求将始终转发到默认的servlet,并且在这种情况下永远不会抛出NoHandlerFoundException。”
Before
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.web")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
// ...
}
After
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.web")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
// ...
}
#1
33
The solution is much simpler than thought. One can use one generic ResourceNotFoundException
defined as follows:
解决方案比想象的要简单得多。可以使用如下定义的一个通用ResourceNotFoundException:
public class ResourceNotFoundException extends RuntimeException { }
then one can handle errors within every controller with an ExceptionHandler
annotation:
然后可以使用ExceptionHandler注释处理每个控制器中的错误:
class MeterController {
// ...
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "meters/notfound";
}
// ...
@RequestMapping(value = "/{number}/edit", method = RequestMethod.GET)
public String viewEdit(@PathVariable("number") final Meter meter,
final Model model) {
if (meter == null) throw new ResourceNotFoundException();
model.addAttribute("meter", meter);
return "meters/edit";
}
}
Every controller can define its own ExceptionHandler
for the ResourceNotFoundException
.
每个控制器都可以为ResourceNotFoundException定义自己的ExceptionHandler。
#2
17
modified your web.xml file.Using following code.
修改了您的web.xml文件。使用以下代码。
<display-name>App Name </display-name>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
Access this by following code.
通过以下代码访问它。
response.sendError(508802,"Error Message");
Now add this code in web.xml.
现在在web.xml中添加此代码。
<error-page>
<error-code>508802</error-code>
<location>/error500.jsp</location>
</error-page>
#3
9
You can map the error codes in web.xml like the following
您可以在web.xml中映射错误代码,如下所示
<error-page>
<error-code>400</error-code>
<location>/400</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500</location>
</error-page>
Now you can create a controller to map the url's that are hit when any of these error is found.
现在,您可以创建一个控制器来映射发现任何这些错误时被击中的URL。
@Controller
public class HTTPErrorHandler{
String path = "/error";
@RequestMapping(value="/404")
public String error404(){
// DO stuff here
return path+"/404";
}
}
For full example see this tutorial
有关完整示例,请参阅本教程
#4
4
You should follow this article where you can find detailed information about exception handling in Spring MVC projects.
您应该阅读本文,您可以在其中找到有关Spring MVC项目中的异常处理的详细信息。
@ControllerAdvice may help you in this case
@ControllerAdvice可以在这种情况下帮助你
#5
4
We can just add following lines of code into web.xml file and introduce a new jsp file named errorPage.jsp into root directory of the project to get the requirement done.
我们可以在web.xml文件中添加以下代码行,并将一个名为errorPage.jsp的新jsp文件引入项目的根目录,以完成需求。
<error-page>
<error-code>400</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorPage.jsp</location>
</error-page>
#6
4
Simple answer for 100% free xml:
100%免费xml的简单答案:
-
Set properties for DispatcherServlet
设置DispatcherServlet的属性
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] {AppConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } //that's important!! @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { boolean done = registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); // -> true if(!done) throw new RuntimeException(); }
}
-
Create @ControllerAdvice:
@ControllerAdvice public class AdviceController { @ExceptionHandler(NoHandlerFoundException.class) public String handle(Exception ex) { return "redirect:/404"; } @RequestMapping(value = {"/404"}, method = RequestMethod.GET) public String NotFoudPage() { return "404"; }
}
-
Create 404.jsp page with any content
使用任何内容创建404.jsp页面
创建@ControllerAdvice:@ControllerAdvice public class AdviceController { @ExceptionHandler(NoHandlerFoundException.class) public String handle(Exception ex){ return“redirect:/ 404”; } @RequestMapping(value = {“/ 404”},method = RequestMethod.GET) public String NotFoudPage(){ 返回“404”; } }
That's all.
#7
1
I also needed to NOT use org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer
.
我还需要不要使用org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer。
According to org.springframework.web.servlet.DispatcherServlet.setThrowExceptionIfNoHandlerFound(boolean)
: "Note that if DefaultServletHttpRequestHandler is used, then requests will always be forwarded to the default servlet and a NoHandlerFoundException would never be thrown in that case."
根据org.springframework.web.servlet.DispatcherServlet.setThrowExceptionIfNoHandlerFound(boolean):“请注意,如果使用DefaultServletHttpRequestHandler,那么请求将始终转发到默认的servlet,并且在这种情况下永远不会抛出NoHandlerFoundException。”
Before
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.web")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
// ...
}
After
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.web")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
// ...
}