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

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

}

在JSP:

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

这是我快速创建测试出的问题,我与另一视图 - 控制器的控制器。正如你可以在控制器看到CustomeDateEditor定义。在我的实际控制该编辑器工作正常;当你比如11/01/2010在表单字段这是很好的转换成编辑器中输入的日期;还可以追溯到形式的日期又被很好地转换回一个字符串。

然而,当我(在的TestController)要设置窗体上的默认日期,那么这个被简单地显示Date.toString()的表单字段而不是使用CustomDateEditor.getAsText返回值的()!一些调试后,我才知道,我InitBinder方法不调用时RequestMethod == GET。这是正常吗

我敢肯定,我可以不使用此解决方法

感谢您的帮助,点击 斯泰恩

有帮助吗?

解决方案

使用@ModelAttribute设置域转发到页面之前。

仔细使用new当你处理春天,它只会创建对象外Spring上下文的新实例,你可以不使用任何弹性功能(如网络绑定,校验等)。

示例:

@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());

其他提示

我不能肯定,但在registerCustomEditor方法的第二个参数被设置为无效。这个参数是设置要与编辑器关联的字段名,所以我不知道它到底是什么打算时,它的设置为空发生根本。如果你想使用这个编辑器与特定的所有字段中键入它存在同样的方法没有这个参数:

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)

我会尝试这一点,虽然我不知道这会解决这个问题。

binder.registerCustomEditor(Date.class, 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没有被调用。 这里的“秘密”是修改我的“@RequestMapping”的方法,包括 - 在它的原型 - 2个参数 我并不需要: 点击             @ModelAttribute( “类”)分类类别, 点击             BindingResult结果 点击 这解决了一切(我知道这不是魔术,只是烟雾,镜子和Java反射 - 但我希望 出版物和网络传播会妥善处理简单的用例是这样)。

下面是我与JSP文件相关的代码:



        <div>
        Select a category: 
        <form:select path="category">
                    <form:options items="${categoryList}" itemValue="id" itemLabel="name" 

    />
        </form:select>
        </div>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top