c:forEach with Arraylist form:checkbox

时间:2015-06-01 11:22:35

标签: java jsp spring-mvc spring-form

我有这个Expeption并且不知道如何解决它...

我有一个布尔列表,我想填写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()返回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;
}
}

和我的dbChange对象绑定到&#34; index&#34;在这里:

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:无效的属性&#39;系统[0]&#39; bean class [com.bla.dbchanges.DbChange]:非法尝试获取财产&#39;系统&#39;抛出异常;嵌套异常是org.springframework.beans.NullValueInNestedPathException:无效的属性&#39; systems&#39; bean类[com.bla.dbchanges.DbChange]:无法实例化属性类型[java.lang.Boolean]以自动增长嵌套属性路径:java.lang.InstantiationException:java.lang.Boolean   org.springframework.beans.InvalidPropertyException:无效的属性&#39; systems [0]&#39; bean class [com.bla.dbchanges.DbChange]:非法尝试获取财产&#39;系统&#39;抛出异常;嵌套异常是org.springframework.beans.NullValueInNestedPathException:无效的属性&#39; systems&#39; bean类[com.bla.dbchanges.DbChange]:无法实例化属性类型[java.lang.Boolean]以自动增长嵌套属性路径:java.lang.InstantiationException:java.lang.Boolean       在org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:827)       在org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)       at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)       在org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:218)       在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:120)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)       at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)       at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)       在org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:81)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)       在org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:83)       在org.springframework.web.servlet.tags.form.CheckboxTag.writeTagContent(CheckboxTag.java:53)       在org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)       在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)       在org.apache.jsp.WEB_002dINF.pages.index_jsp._jspService(index_jsp.java:242)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)       at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)       在org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)       在org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)       在org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)       在org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)       在org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)       在org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)       在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:620)       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:727)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)       在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)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       在java.lang.Thread.run(Thread.java:745)   引起:org.springframework.beans.NullValueInNestedPathException:无效的属性&#39;系统&#39; bean类[com.bla.dbchanges.DbChange]:无法实例化属性类型[java.lang.Boolean]以自动增长嵌套属性路径:java.lang.InstantiationException:java.lang.Boolean       在org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:633)       在org.springframework.beans.BeanWrapperImpl.growCollectionIfNecessary(BeanWrapperImpl.java:865)       在org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:770)       ......还有56个

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

你的代码中有拼写错误。

RPLACE

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

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

变量名称为system而不是systems

答案 1 :(得分:0)

找到一个解决方案,将List更改为DbChanges.java中的Set:

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>