Spring - 绑定到一个对象而不是一个String或者原语 [英] Spring - binding to an object rather than a String or primitive

查看:117
本文介绍了Spring - 绑定到一个对象而不是一个String或者原语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下命令对象:

  class BreakfastSelectCommand {
列表<早餐&可能的
早餐selectBreakfast;
}

如何从列表中选择一个早餐来填充selectedBreakfast?



我正在想我会在我的jsp中做这样的事情:

 < form:radiobuttons items =$ {possibleBreakfasts}path =selectedBreakfast/> 

但这似乎不起作用。任何想法?



谢谢,



- 摩根

解决方案

所有这一切都是PropertyEditor。



您需要为您的Breakfast类定义一个PropertyEditor,然后在控制器的initBinder方法中使用registerCustomEditor配置ServletDataBinder。



示例:

  public class BreakfastPropertyEditor extends PropertyEditorSupport {
public void setAsText(String incomingming){
早餐b = yourDao.findById(Integer.parseInt(incomingming));
setValue(b);
}
public String getAsText(){
return((Breakfast)getValue())。getId();
}
}

注意你需要一些空的检查等,但你得到的想法。在您的控制器中:

  public BreakfastFooBarController扩展SimpleFormController {
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){
binder.registerCustomEditor(Breakfast.class,new BreakfastPropertyEditor(yourDao));
}
}

需要注意的事项:




  • PropertyEditor不是线程安全的

  • 如果你需要spring bean,手动注入它们或者在spring中定义它们范围和使用方法注入到您的控制器中

  • throw IllegalArgumentException如果入站参数无效/未找到,spring将正确将其转换为绑定错误



希望这有帮助。



编辑(回应评论):
看起来有点在给定的例子中很奇怪,因为BreakfastSelectCommand看起来不像一个实体,我不知道你的实际情况是什么。说这是一个实体,例如像 Person ,一个早餐属性,然后是 formBackingObject( )方法将从 PersonDao 加载Person对象,并将其作为命令返回。绑定阶段将根据所选值更改早餐属性,以便在 onSubmit 中的命令具有全部设置的早餐属性。



根据您的DAO对象的实现调用它们两次或尝试加载相同的实体两次,实际上并不意味着您将得到两个运行的SQL语句。这特别适用于Hibernate,它保证它返回与给定标识符的会话中相同的对象,从而运行,让绑定尝试加载早餐选择即使没有改变也不应该导致任何不当的开销。


Let's say I have the following command object:

class BreakfastSelectCommand{
    List<Breakfast> possibleBreakfasts;
    Breakfast selectedBreakfast;
}

How can I have spring populate "selectedBreakfast" with a breakfast from the list?

I was figuring I'd do something like this in my jsp:

<form:radiobuttons items="${possibleBreakfasts}" path="selectedBreakfast"  />

But this doesn't seem to work. Any ideas?

thanks,

-Morgan

解决方案

The key to it all of this is the PropertyEditor.

You need to define a PropertyEditor for your Breakfast class and then configure the ServletDataBinder using registerCustomEditor in the initBinder method of your controller.

example:

public class BreakfastPropertyEditor extends PropertyEditorSupport{
    public void setAsText(String incomming){
        Breakfast b = yourDao.findById( Integer.parseInt(incomming));
        setValue(b);
    }
    public String getAsText(){
        return ((Breakfast)getValue()).getId();
    }
}

note you'll be needing some null checking etc, but you get the idea. In your controller:

public BreakfastFooBarController extends SimpleFormController {
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Breakfast.class, new BreakfastPropertyEditor(yourDao));
    }
}

things to watch out for:

  • PropertyEditor's are not thread safe
  • if you need spring beans, either manually inject them or define them in spring as prototype scope and use method injection into your controller
  • throw IllegalArgumentException if the inbound parameter is not valid/not found, spring will convert this into a binding error correctly

hope this helps.

Edit (in response to the comment): It looks a little strange in the given example because BreakfastSelectCommand doesn't look like an entity, I'm not sure what the actual scenario you have is. Say it is an entity, for example like Person with a breakfast property then the formBackingObject() method would load the Person object from the the PersonDao and return it as the command. The binding phase would then change the breakfast property depending on the selected value, such that the command that arrives in onSubmit has the breakfast property all set up.

Depending on the implementation of your DAO objects calling them twice or attempting to load the same entity twice doesn't actually mean that you will get two SQL statements being run. This applies particularly to Hibernate, where it guarantees that it will return the same object that is in it's session for a given identifier, thus running letting the binding attempt to load the Breakfast selection even through it hasn't changed shouldn't result in any undue overhead.

这篇关于Spring - 绑定到一个对象而不是一个String或者原语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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