Spring MVC:如何返回自定义404 errorpages? [英] Spring MVC: How to return custom 404 errorpages?

查看:119
本文介绍了Spring MVC:如何返回自定义404 errorpages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种干净的方法,在找不到请求的资源时,在Spring 4中返回自定义的404错误。对不同域类型的查询应该会产生不同的错误页面。

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.

这里有一些代码来表明我的意图(Meter是一个域类):

Here some code to show my intention (Meter is a domain class):

@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";
}

我想到了几种处理问题的方法。首先,有可能创建 RuntimeException s,如

I imagine several ways for handling the problem. First there would be the possibility to create RuntimeExceptions like

@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).

但我不喜欢污染我的应用程序有许多小例外。

But I don't like polluting my application with many small exceptions.

另一种可能是使用 HttpServletResponse 并手动设置状态代码:

Another possibility would be using HttpServletResponse and set the statuscode manually:

@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";
}

但是使用这个解决方案我必须复制许多控制器方法的前5行(比如编辑,删除)。

But with this solution I have to duplicate the first 5 lines for many controller methods (like edit, delete).

是否有一种优雅的方法可以防止多次重复这些行?

Is there an elegant way to prevent duplicating these lines many times?

推荐答案

解决方案比想象的要简单得多。可以使用一个通用 ResourceNotFoundException 定义如下:

The solution is much simpler than thought. One can use one generic ResourceNotFoundException defined as follows:

public class ResourceNotFoundException extends RuntimeException { }

然后可以使用 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";
    }
}

每个控制器都可以定义自己的 ExceptionHandler 用于 ResourceNotFoundException

Every controller can define its own ExceptionHandler for the ResourceNotFoundException.

这篇关于Spring MVC:如何返回自定义404 errorpages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆