【问题标题】:Spring automatic binding for dropdown lists for bean propertiesbean属性下拉列表的Spring自动绑定
【发布时间】:2010-02-17 13:16:34
【问题描述】:

有没有办法使用 spring 的 form.select 为另一种类型的 bean 绑定 bean 属性。 示例:

我有一个需要在视图中使用名为 BeanB 的属性更新的 bean:

public class BeanA {    
  private BeanB bean;
  private int id;

  private void setId(int id){
     this.id = id;
  }

  private int getId(){
     return this.id;
  }

  public void setBean(BeanB bean){
    this.bean = bean;
  }

  public BeanB getBean(){
   return this.bean;
  }
}

public class BeanB{
    private int id;

    private void setId(int id){
       this.id = id;
    }

    private int getId(){
       return this.id;
    }
}

对于视图,我想发送一个 BeanB 列表以使用 spring 的表单控制器进行选择:

public class MyController extends SimpleFormController{

protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
  BeanA bean = new BeanA();
  //... init the bean or retrieve from db

  List<BeanB> list = new ArrayList<BeanB>();
  //... create list of objects

  ModelAndView modelAndView = super.handleRenderRequestInternal(request, response);
  modelAndView.getModel().put("beans", list);
  modelAndView.getModel().put("bean", bean);

  return modelAndView ;
}
}

在 jsp 中,我想使用 form.select 从给定列表中选择要为 BeanA 设置的项目,例如:

<form:select path="${bean.bean}" items="${beans}"/>

它看起来不像这样工作。还有其他简单的解决方案吗?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    在 HTML 中创建选择标记:

    <form:select path="bean" items="${candidates}" itemValue="id" itemLabel="name"/>
    

    提交表单时,该值将作为 String 传递给 Spring,然后需要将其转换为所需类型的 bean。 Spring为此使用WebDataBinder,使用PropertyEditors进行与String的转换。由于您的 'id' 属性可能已经可序列化为字符串,因此您已经看到了一半的工作。

    你正在寻找这个:http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html#mvc-ann-webdatabinder

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(BeanB.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                // some code to load your bean.. 
                // the example here assumes BeanB class knows how to return
                //  a bean for a specific id (which is an int/Integer) by
                //  calling the valueOf static method
                // eg:
                setValue(BeanB.valueOf(Integer.valueOf(text)));
            }
        });
    }
    

    Spring 2.5.6 的文档似乎建议 @Controller 和 @InitBinder 注释在配置后可以工作,您必须根据您的环境进行推断。

    @见http://static.springsource.org/spring/docs/2.5.6/api/index.html?org/springframework/web/bind/WebDataBinder.html

    【讨论】:

    • 谢谢,虽然我还没有测试过,但它看起来是正确的答案。为了简单起见,我将 UI 属性排除在示例之外。
    • 我试过这个: 但它不起作用。它可以显示值,但不能自动插入 bean。有趣的是,添加此行时甚至没有调用控制器中的 onSubmitAction() (没有抛出异常)。我认为返回的值是一个字符串,它试图对失败的 BeanB 进行强制转换。还有其他想法吗?
    • 您可能需要为 WebDataBinder 配置一个 PropertyEditor,我会用其他一些潜在的东西来更新我的答案
    猜你喜欢
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多