c:forEach 与 Arraylist form:checkbox

c:forEach with Arraylist form:checkbox

我遇到了这个问题,不知道怎么解决...

我有一个布尔列表我想填写一个JSP,复选框的名称在另一个字符串列表中,如果列表中有3个字符串,则应该有3个复选框,如果 4 个字符串,则 4 个,依此类推

这是我的 JSP

<form:form action="/SpringMVC/dbChanges/form" commandName="index" method="POST">
    <div>
        <h2>neu anlegen:</h2>
        <table>
            <tr><td>Name:&nbsp;<form:input path="name" style="float: right"/></td><td><div class="errors"><form:errors path="name"/></div></td></tr>
            <tr><td>Datum:&nbsp;<form:input path="date" style="float: right"/></td><td><div class="errors"><form:errors path="date"/></div></td></tr>
            <tr><td>Nummer:&nbsp;<form:input path="number" style="float: right"/></td><td><div class="errors"><form:errors path="number"/></div></td></tr>
            <c:forEach var="systemListEntry" items="${systemList}" varStatus="status">
                <tr><td>${systemListEntry.name}: <form:checkbox path="system[${status.index}]" style="float: right"/></td></tr>
            </c:forEach>
        </table>
        <input type="submit" value="Speichern" />
    </div>
</form:form>

这是我的控制器中的方法:

@Controller
@RequestMapping("/dbChanges")
@SessionAttributes("changes")
public class DbChangesControllerImpl implements DbChangeController {

private DbChangeService dbChangeService;
private DbChangeValidator validator;

@Autowired
public DbChangesControllerImpl(final DbChangeValidator pValidator,
        final DbChangeService pDbChangeService) {
    validator = pValidator;
    dbChangeService = pDbChangeService;
}

// Changes //

@Override
@RequestMapping(value = "index.html", method = RequestMethod.GET)
public ModelAndView getChanges() {

    final List<DbChange> list = dbChangeService.getDbEntrys();

    final ModelAndView model = new ModelAndView("index", "index",
            new DbChange());
    model.addObject("dbChanges", list);
    model.addObject("systemList", dbChangeService.getSystems());

    model.addObject("year",
            java.util.Calendar.getInstance().get(java.util.Calendar.YEAR));

    return model;
}
...

dbChangeService.getSystems() returns FdcSystems 列表:

public class FdcSystem {
private String oid;
private String name;

public String getName() {
    return name;
}

public void setName(final String pName) {
    name = pName;
}

public String getOid() {
    return oid;
}
}

我绑定到 "index" 的 dbChange 对象在这里:

package com.bla.dbchanges;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Fabio Stuber
 *
 */
// TODO: Impl machen
public class DbChange {
private String name;
private String date;
private int number;
private List<Boolean> systems;

private String oid;

/**
 * Konstruktor ohne Id für die Tests
 */
public DbChange(final String pName, final String pDate, final int pNumber) {
    name = pName;
    date = pDate;
    number = pNumber;
}

public DbChange(final String pOid, final String pName, final String pDate, final int pNumber) {
    oid = pOid;
    name = pName;
    date = pDate;
    number = pNumber;
}

/**
 * default Konstruktor, vom JSP benötigt
 */
public DbChange() {
    systems = new ArrayList<Boolean>();
}

public String getName() {
    return name;
}

public void setName(final String pName) {
    name = pName;
}

public String getDate() {
    return date;
}

public void setDate(final String pDate) {
    date = pDate;
}

public int getNumber() {
    return number;
}

public void setNumber(final int pNumber) {
    number = pNumber;
}

public String getOid() {
    return oid;
}

public void setOid(final String pOid) {
    oid = pOid;
}

public List<Boolean> getSystems() {
    return systems;
}

public void setSystems(final List<Boolean> pSystems) {
    systems = pSystems;
}

public void addSystem(final Boolean system) {
    systems.add(system);
}

}

dbChangesServiceImpl

package com.bla.dbchanges;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.bla.dbchanges.context.DbChangeFactory;
import com.bla.dbchanges.model.FdcDbchange;
import com.bla.dbchanges.model.FdcSystem;

public class DbChangeServiceImpl implements DbChangeService {
private final DbChangeDataStoreManager manager;
private final DbChangeFactory factory;

@Autowired
public DbChangeServiceImpl(final DbChangeDataStoreManager pNewManager,
        final DbChangeFactory pFactory) {
    manager = pNewManager;
    factory = pFactory;
}

private Date convertStringToDate(final String date) {
    Date parsedDate = new Date();

    try {
        parsedDate = new SimpleDateFormat("dd.MM.yyyy").parse(date);
    } catch (final ParseException e) {
        e.printStackTrace();
    }

    return parsedDate;
}

@Override
public List<DbChange> getDbEntrys() {
    final List<DbChange> list = new ArrayList<DbChange>();

    for (final FdcDbchange fdcChange : manager.getAllChanges()) {
        list.add(factory.createDbChange(fdcChange));
    }

    return list;
}

@Override
public DbChange getSingleChange(final String pOid) {
    final FdcDbchange change = manager.getSingleChange(pOid);

    return factory.createDbChange(change);
}

// TODO: Error, er will statt auf meinen schon vorhandenen Eintrag
// zuzugreiffen, einen neuen erstellen mit der gleichen OID
@Override
public void saveChange(final DbChange pChange) {
    final FdcDbchange change = factory.createFdcDbchange();
    final Set<FdcSystem> systems = new HashSet<FdcSystem>();
    final List<FdcSystem> allSystems = manager.getAllSystems();

    // TODO: evtl Chain sobald error gefixt ist
    if (pChange.getTwa()) {
        systems.add(allSystems.get(0));
    }
    if (pChange.getIwa()) {
        systems.add(allSystems.get(1));
    }
    if (pChange.getPwa()) {
        systems.add(allSystems.get(2));
    }

    change.setChangeDate(convertStringToDate(pChange.getDate()));
    change.setChangeName(pChange.getName());
    change.setChangeNumber(pChange.getNumber());
    change.setSystems(systems);

    manager.saveChange(change);
}

@Override
public void delete(final String pOid) {
    final FdcDbchange changeToDelete = manager.getSingleChange(pOid);

    manager.deleteChange(changeToDelete);
}

@Override
public void editChange(final DbChange pNewChange) {
    final FdcDbchange changeToEdit = manager.getSingleChange(pNewChange
            .getOid());
    final List<FdcSystem> allSystems = manager.getAllSystems();
    final Set<FdcSystem> systems = new HashSet<FdcSystem>();

    // TODO: hier die selbe Chain
    if (pNewChange.getTwa()) {
        systems.add(allSystems.get(0));
    }
    if (pNewChange.getIwa()) {
        systems.add(allSystems.get(1));
    }
    if (pNewChange.getPwa()) {
        systems.add(allSystems.get(2));
    }

    changeToEdit.setSystems(systems);

    manager.editChange(changeToEdit);
}

@Override
public List<FdcSystem> getSystems() {
    final List<String> systems = new ArrayList<String>();
    // for (final FdcSystem system : manager.getAllSystems()) {
    // systems.add(system.getName());
    // }

    return manager.getAllSystems();
}

}

最后,我的例外:

SCHWERWIEGEND: Invalid property 'systems[0]' of bean class [com.bla.dbchanges.DbChange]: Illegal attempt to get property 'systems' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'systems' of bean class [com.bla.dbchanges.DbChange]: Could not instantiate property type [java.lang.Boolean] to auto-grow nested property path: java.lang.InstantiationException: java.lang.Boolean org.springframework.beans.InvalidPropertyException: Invalid property 'systems[0]' of bean class [com.bla.dbchanges.DbChange]: Illegal attempt to get property 'systems' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'systems' of bean class [com.bla.dbchanges.DbChange]: Could not instantiate property type [java.lang.Boolean] to auto-grow nested property path: java.lang.InstantiationException: java.lang.Boolean at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:827) at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699) at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99) at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:218) at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:120) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152) at org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:81) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143) at org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:83) at org.springframework.web.servlet.tags.form.CheckboxTag.writeTagContent(CheckboxTag.java:53) at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80) at org.apache.jsp.WEB_002dINF.pages.index_jsp._jspx_meth_form_005fcheckbox_005f0(index_jsp.java:851) at org.apache.jsp.WEB_002dINF.pages.index_jsp._jspx_meth_c_005fforEach_005f3(index_jsp.java:814) at org.apache.jsp.WEB_002dINF.pages.index_jsp._jspService(index_jsp.java:242) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338) at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.NullValueInNestedPathException: Invalid property 'systems' of bean class [com.bla.dbchanges.DbChange]: Could not instantiate property type [java.lang.Boolean] to auto-grow nested property path: java.lang.InstantiationException: java.lang.Boolean at org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:633) at org.springframework.beans.BeanWrapperImpl.growCollectionIfNecessary(BeanWrapperImpl.java:865) at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:770) ... 56 more

谁能帮我解决这个问题?

您的代码中有拼写错误。

R地方

${system.name}: <form:checkbox path="systems[${status.index}]" style="float: right"/>

符合

${system.name}: <form:checkbox path="system[${status.index}]" style="float: right"/>

因为变量名是 system 而不是 systems

找到解决方案,将列表更改为 DbChanges.java 中的集合:

private Set<String> systems;

并将 JSP 更改为:

<c:forEach var="systemListEntry" items="${systemList}">
    <tr><td>${systemListEntry.name}: <form:checkbox path="systems" value="${systemListEntry.name}" style="float: right"/></td></tr>
</c:forEach>