Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在Spring MVC中使用StringTrimmerEditor时出现错误400_Spring Mvc - Fatal编程技术网

Spring mvc 在Spring MVC中使用StringTrimmerEditor时出现错误400

Spring mvc 在Spring MVC中使用StringTrimmerEditor时出现错误400,spring-mvc,Spring Mvc,我们将SpringMVC3.2.3.RELEASE(注解)与google app engine 1.8.0一起使用 我们添加了一个StringTrimmerEditor,用于将表单中的空白字符串转换为空值,它工作正常,但作为一个副作用,所有在控制器中使用@RequestParams的方法都希望填充所有@RequestParams,否则它们将抛出http错误400。我们尝试了不同的@RequestParam设置,如(required=false)和(defaultValue=“some value

我们将SpringMVC3.2.3.RELEASE(注解)与google app engine 1.8.0一起使用

我们添加了一个StringTrimmerEditor,用于将表单中的空白字符串转换为空值,它工作正常,但作为一个副作用,所有在控制器中使用@RequestParams的方法都希望填充所有@RequestParams,否则它们将抛出http错误400。我们尝试了不同的@RequestParam设置,如(required=false)和(defaultValue=“some value”),但它不起作用

下面是我们如何使用它

@ControllerAdvice
public class ControllerSetup
{
    @InitBinder
    public void initBinder ( WebDataBinder binder )
    {
        StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);
        binder.registerCustomEditor(String.class, stringtrimmer);

    }
}
控制器

@RequestMapping(value="/addreportitems", method=RequestMethod.POST)
    public String saveEditForm(@ModelAttribute DCReport dcReport,
            @ModelAttribute("loggedInEmployee") Employee someEmployee,
            @RequestParam Integer someInteger,
            Model m) {
....
}
如果我们不在表单中放入整数,我们会得到一个错误 错误400所需的整数参数“someInteger”不存在


如果我们移除StringTrimmer,它工作得非常好,我们是否遗漏了什么?非常感谢您的帮助。

以下内容对我有用:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

@ResponseBody
@RequestMapping("/foo")
public String renderFoo(@RequestParam(required=false) String bar) {
    return bar;
}
当您使用
required=false
进行测试时,您一定在某个地方出错了