Spring 3 mvc嵌套表单:选择

时间:2012-06-11 08:17:36

标签: forms spring spring-mvc

在我的场景中,我正在开发一个绑定到bean的嵌套选择:

public class WizardPanelp1Bean {

private Integer id;
private Stringofpanels stringofpanels;
private Paneltype paneltype;
private Integer number;
private String paneltypestring;
//and getters/setters... [Omitted]

现在我有了Paneltype对象,另一个简单的bean

Paneltype

private Integer id;
private double peakpower;
private double weight;
private String name;
private double dimension;
private double conversion;
private Set functions = new HashSet(0);
private Set sensors = new HashSet(0);
private Set panels = new HashSet(0);
//[Getters/setters omitted as usual]

所以,我准备了一个名为 wb 的bean的视图 一个简单的小组arraylist

 public class PanelsBean {
    private ArrayList<WizardPanelp1Bean> panels =new ArrayList<WizardPanelp1Bean>();

最后我去了 jsp (请注意这是在a)

<tbody>
         <c:forEach items="${wb.panels}" varStatus="loop" var="item">
            <tr>
                <td>${item.id}</td>
                <td>
                    <form:select  path="panels[${loop.index}].paneltype" >
                        <c:forEach var="type" items="${types}">
                            <c:choose>
                                <c:when test="${item.paneltype.id==type.id}">
                                    <form:option selected="selected" value="${type.id}" label="${type.name}" />
                                </c:when>
                                <c:otherwise>
                                    <form:option value="${type.id}" label="${type.name}" />
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </form:select>
                </td>
                    <td><form:input style="width:180px" path="panels[${loop.index}].number" /></td>
                    <td>
                    <div>
                        <form:input style="visibility: hidden ; width:0px" path="panels[${loop.index}].id" disabled="disabled" />
                        <a href="javascript:remove(${item.id},${stringofpanels.id})" class="wb.panels.remove" >Rimuovi</a>
                    </div>
                    </td>
                </tr>
            </c:forEach>    
     </tbody>

每次我得到一个对paneltype的null引用。我显然在控制器上使用了@InitBinder: 的 Initbinder

@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
            binder.registerCustomEditor(Paneltype.class, "paneltype", new PropertyEditorSupport() {
                @Override
                public void setAsText(String text) {
                    int i=0;
                    PaneltypeDAO pDAO=new PaneltypeDAO();
                    setValue(pDAO.findById(Integer.parseInt(text)));
                }
            });
        }

但代码永远不会达到此目的。这就像jsp确定值为null

连连呢?感谢

1 个答案:

答案 0 :(得分:0)

我认为当您尝试提交此表单并且未在模型中获取绑定值时,您的问题仍然存在。

尝试使用 LazyList 初始化列表。请在PanelsBean类中替换您的面板声明,如下所示。

private List<WizardPanelp1Bean> panels = LazyList.decorate(new ArrayList<WizardPanelp1Bean>(),FactoryUtils.instantiateFactory(WizardPanelp1Bean.class));

希望这会对你有所帮助。欢呼声。