Spring @MVC 和 @RequestParam 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:59:34  来源:igfitidea点击:

Spring @MVC and @RequestParam validation

springspring-mvc

提问by Brandon Yarbrough

I would like to use the @RequestParam annotation like so:

我想像这样使用@RequestParam 注释:

@RequestMapping
public void handleRequest( @RequestParam("page") int page ) {
   ...
}

However, I want to show page 1 if the user fiddles with the URL parameters and tries to go to page "abz" or something non-numerical. Right now, the best I can get Spring to do is return a 500. Is there a way to override this behavior cleanly without having to take in the parameter as a String?

但是,如果用户摆弄 URL 参数并尝试转到页面“abz”或非数字页面,我想显示第 1 页。现在,我可以让 Spring 做的最好的事情是返回 500。有没有办法干净地覆盖这种行为而不必将参数作为字符串接受?

I looked at the @ExceptionHandler annotation, but it doesn't seem to do anything when I set I use @ExceptionHandler(TypeMismatchException.class). Not sure why not.

我查看了 @ExceptionHandler 注释,但是当我设置 I use 时它似乎没有做任何事情@ExceptionHandler(TypeMismatchException.class)。不知道为什么不。

Suggestions?

建议?

P.S. Bonus question: Spring MVC is called Spring MVC. Is Spring MVC with annotations just called Spring @MVC? Google treats them as the same name, which is annoying.

PS Bonus 问题:Spring MVC 被称为 Spring MVC。带有注释的 Spring MVC 只是称为 Spring @MVC 吗?谷歌将它们视为同名,这很烦人。

回答by Janning

The ConversionServiceis a nice solution, but it lacks a value if you give an empty string to your request like ?page=The ConversionService is simply not called at all, but pageis set to null(in case of Integer) or an Exception is thrown (in case of an int)

ConversionService是一个不错的解决方案,但如果您为请求提供空字符串,则它缺少值,例如?page=根本没有调用 ConversionService,而是page设置为null(在 的情况下Integer)或抛出异常(在 的情况下int

This is my preferred solution:

这是我的首选解决方案:

@RequestMapping
public void handleRequest( HttpServletRequest request ) {
    int page = ServletRequestUtils.getIntParameter(request, "page", 1);
}

This way you always have a valid int parameter.

这样你总是有一个有效的 int 参数。

回答by axtavt

Since Spring 3.0, you can set a ConversionService. @InitBinder's valuespecifies a particular parameter to apply that service to:

从 Spring 3.0 开始,您可以设置一个ConversionService. @InitBinder'svalue指定一个特定参数以将该服务应用于:

@InitBinder("page")
public void initBinder(WebDataBinder binder) {
    FormattingConversionService s = new FormattingConversionService();
    s.addFormatterForFieldType(Integer.class, new Formatter<Integer>() {
        public String print(Integer value, Locale locale) {
            return value.toString();
        }

        public Integer parse(String value, Locale locale)
                throws ParseException {
            try {
                return Integer.valueOf(value);
            } catch (NumberFormatException ex) {
                return 1;
            }
        }
    });
    binder.setConversionService(s);
}