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
Binding 显示表单时未调用Spring@InitBinder=>;未定义自定义编辑器_Binding_Spring Mvc_Annotations_Propertyeditor - Fatal编程技术网

Binding 显示表单时未调用Spring@InitBinder=>;未定义自定义编辑器

Binding 显示表单时未调用Spring@InitBinder=>;未定义自定义编辑器,binding,spring-mvc,annotations,propertyeditor,Binding,Spring Mvc,Annotations,Propertyeditor,我有以下(简化为骨骼)控制器: @Controller public class TestController { @RequestMapping(value = "/test.htm", method = RequestMethod.GET) public String showForm(final ModelMap map) { final TestFilter filter = new TestFilter(); filter.setStartDate(new Da

我有以下(简化为骨骼)控制器:

@Controller  
public class TestController  {

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(final ModelMap map) {
    final TestFilter filter = new TestFilter();
    filter.setStartDate(new Date(System.currentTimeMillis()));
    map.addAttribute("reportPerResourceForm", filter);
    return "test";
}

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}


        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Category.class, new CategoryEditor(categoryService));
        }

        @ModelAttribute("categoryList") // Populate reference-data (EG select-lists) in the view. (p. 390-

    391).
        public List<Category> populateCategoryList() {
            return categoryService.list();
        }

        // Note: without adding "BindingResult result" to the following prototype
        // (and preceding it with a @ModelAttribute("categoryList") -
        // my initBibder() method does not get called!
        // I discovered and added this hokum in response to the following links:
        // http://forum.springsource.org/showthread.php?46837-InitBinder-not-called
        // http://forum.springsource.org/showthread.php?46876-Custom-date-format-on-GET-requests&p=154820
        @RequestMapping("/site/list.htm")
        @ModelAttribute("sites")  // 20110819
        public ModelAndView listSite(
                @ModelAttribute("category") Category category,
                BindingResult result
                )
        {
    //        List<Site> sites = siteService.list();
            List<Site> sites = new ArrayList<Site>(); // = siteService.list();
            return new ModelAndView("siteList", "sites", sites);
        }
    }

}

jsp:

<form:form commandName="reportPerResourceForm" id="reportForm">
    <form:input path="startDate" />
</form:form>

这是我快速创建的一个控制器,用于测试我与另一个视图控制器的问题。正如您在控制器中看到的,定义了CustomeDateEditor。在我的实际控制人中,这个编辑器工作得很好;例如,当您在表单字段中输入11/01/2010时,编辑器会很好地将其转换为日期;此外,当返回表单时,日期再次被很好地转换回字符串

但是,当我(在TestController中)希望在表单上设置默认日期时,它只会在表单字段中显示一个date.toString(),而不是使用CustomDateEditor.getAsText()返回的值!经过一些调试,我了解到当RequestMethod==GET时不会调用InitBinder方法。这正常吗

我相信我可以通过不使用 谢谢你的帮助,

Stijn

我不确定,但是registerCustomEditor方法中的第二个参数设置为null。此参数用于设置要将编辑器与之关联的字段名,因此我不知道当它设置为null时会发生什么。如果要将此编辑器用于特定类型的所有字段,则存在不带此参数的相同方法:

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)
我会试试这个,虽然我不确定这是否能解决问题

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));

希望有帮助。

我不确定,但registerCustomEditor方法中的第二个参数设置为null。此参数用于设置要将编辑器与之关联的字段名,因此我不知道当它设置为null时会发生什么。如果要将此编辑器用于特定类型的所有字段,则存在不带此参数的相同方法:

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)
我会试试这个,虽然我不确定这是否能解决问题

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));

希望有帮助。

在转发到页面之前,请使用
@modeldattribute
设置域

当您处理spring时,小心地使用
new
,它只会在spring上下文之外创建一个新的对象实例,您不能使用spring的任何功能(例如web绑定、验证等)

例如:

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(@ModelAttribute yourDomain, final ModelMap map)
在您的域中,您可以使用:

@DateTimeFormat(pattern="dd/MM/yyyy")
private Date balance = new Date(System.currentTimeMillis());

在转发到页面之前,使用
@modeldattribute
设置域

当您处理spring时,小心地使用
new
,它只会在spring上下文之外创建一个新的对象实例,您不能使用spring的任何功能(例如web绑定、验证等)

例如:

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(@ModelAttribute yourDomain, final ModelMap map)
在您的域中,您可以使用:

@DateTimeFormat(pattern="dd/MM/yyyy")
private Date balance = new Date(System.currentTimeMillis());

为了解决这个问题,我自己的控制器中有以下代码:

@Controller  
public class TestController  {

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(final ModelMap map) {
    final TestFilter filter = new TestFilter();
    filter.setStartDate(new Date(System.currentTimeMillis()));
    map.addAttribute("reportPerResourceForm", filter);
    return "test";
}

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}


        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Category.class, new CategoryEditor(categoryService));
        }

        @ModelAttribute("categoryList") // Populate reference-data (EG select-lists) in the view. (p. 390-

    391).
        public List<Category> populateCategoryList() {
            return categoryService.list();
        }

        // Note: without adding "BindingResult result" to the following prototype
        // (and preceding it with a @ModelAttribute("categoryList") -
        // my initBibder() method does not get called!
        // I discovered and added this hokum in response to the following links:
        // http://forum.springsource.org/showthread.php?46837-InitBinder-not-called
        // http://forum.springsource.org/showthread.php?46876-Custom-date-format-on-GET-requests&p=154820
        @RequestMapping("/site/list.htm")
        @ModelAttribute("sites")  // 20110819
        public ModelAndView listSite(
                @ModelAttribute("category") Category category,
                BindingResult result
                )
        {
    //        List<Site> sites = siteService.list();
            List<Site> sites = new ArrayList<Site>(); // = siteService.list();
            return new ModelAndView("siteList", "sites", sites);
        }
    }


@InitBinder
公共绑定器(WebDataBinder绑定器){
binder.registerCustomEditor(Category.class,newcategoryeditor(categoryService));
}
@ModelAttribute(“categoryList”)//在视图中填充引用数据(例如选择列表)。(第390页)-
391).
公共列表populateCategoryList(){
return categoryService.list();
}
//注意:不向以下原型添加“BindingResult”
//(并在其前面加上@ModelAttribute(“categoryList”)-
//我的initBibder()方法没有被调用!
//我发现并添加了以下链接:
// http://forum.springsource.org/showthread.php?46837-未调用InitBinder
// http://forum.springsource.org/showthread.php?46876-GET请求上的自定义日期格式&p=154820
@请求映射(“/site/list.htm”)
@ModelAttribute(“站点”)//20110819
公共模型和视图列表站点(
@ModelAttribute(“类别”)类别,
BindingResult结果
)
{
//List sites=siteService.List();
List sites=new ArrayList();//=siteService.List();
返回新模型和视图(“站点列表”、“站点”、“站点”);
}
}
我的问题是我的“Category”类没有被识别,因为@InitBinder没有被调用。 这里的“秘密”是修改我的“@RequestMapping”方法,将-2参数包含在原型中 我不需要:
@ModelAttribute(“类别”)类别,
BindingResult结果
这解决了所有问题(我知道这不是魔法,只是烟雾、镜像和Java反射——但我希望 印刷品和在线文献将适当地处理这样的简单用例)。

以下是我相应JSP文件中的相关代码:


选择一个类别:

为了解决这个问题,我自己的控制器中有以下代码:

@Controller  
public class TestController  {

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(final ModelMap map) {
    final TestFilter filter = new TestFilter();
    filter.setStartDate(new Date(System.currentTimeMillis()));
    map.addAttribute("reportPerResourceForm", filter);
    return "test";
}

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}


        @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(Category.class, new CategoryEditor(categoryService));
        }

        @ModelAttribute("categoryList") // Populate reference-data (EG select-lists) in the view. (p. 390-

    391).
        public List<Category> populateCategoryList() {
            return categoryService.list();
        }

        // Note: without adding "BindingResult result" to the following prototype
        // (and preceding it with a @ModelAttribute("categoryList") -
        // my initBibder() method does not get called!
        // I discovered and added this hokum in response to the following links:
        // http://forum.springsource.org/showthread.php?46837-InitBinder-not-called
        // http://forum.springsource.org/showthread.php?46876-Custom-date-format-on-GET-requests&p=154820
        @RequestMapping("/site/list.htm")
        @ModelAttribute("sites")  // 20110819
        public ModelAndView listSite(
                @ModelAttribute("category") Category category,
                BindingResult result
                )
        {
    //        List<Site> sites = siteService.list();
            List<Site> sites = new ArrayList<Site>(); // = siteService.list();
            return new ModelAndView("siteList", "sites", sites);
        }
    }


@InitBinder
公共绑定器(WebDataBinder绑定器){
binder.registerCustomEditor(Category.class,newcategoryeditor(categoryService));
}
@ModelAttribute(“categoryList”)//在视图中填充参考数据(如选择列表)-
391).
公共列表populateCategoryList(){
return categoryService.list();
}
//注意:不向以下原型添加“BindingResult”
//(并在其前面加上@ModelAttribute(“categoryList”)-
//我的initBibder()方法没有被调用!
//我发现并添加了以下链接:
// http://forum.springsource.org/showthread.php?46837-未调用InitBinder
// http://forum.springsource.org/showthread.php?46876-GET请求上的自定义日期格式&p=154820
@请求映射(“/site/list.htm”)
@ModelAttribute(“站点”)//20110819
公共模型和视图列表站点(
@ModelAttribute(“类别”)类别,
BindingResult结果
)