无法将值从jsp赋值给嵌套的ArrayList变量

时间:2014-07-31 04:20:04

标签: java jsp struts2 ognl

我正在尝试为jsp页面empList变量employeeAttdList值出勤率和dt分配值

我的pojo课程

public class Employee implements java.io.Serializable {
private Long id;
private List employeeAttdList = new ArrayList();
//getter and setter
}

 public class EmployeeAttd  implements java.io.Serializable {
 private Long id;
 private Employee employee;
 private String attendance;
 private Date dt;
 }

在jsp页面中,我尝试为attendancedt

分配值
 <s:iterator value="empList" var="elst" status="st">
          <tr><td width="10%"><s:property value="%{#st.index+1}"/></td>
             <td width="70%">
              <s:hidden name="empList[%{#st.index}].id" cssClass="form-control" id="id" value="%{id}"/>
              <label><s:property value="name"/></label>
              </td>
               <td width="20%">
                    <s:textfield name="empList[%{#st.index}].employeeAttdList[0].attendance" cssClass="attd" id="attendance[%{#st.index}]" value="%{attendance}" cssStyle="width:50px"/>
              <s:hidden name="empList[%{#st.index}].employeeAttdList[0].dt" cssClass="form-control dta" id="dt[%{#st.index}]" value="1/1/1"/>
                </td>
         </tr>
     </s:iterator>

行动中

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee> {
   Employee e = new Employee();
   List<Employee> empList = new ArrayList<Employee>();
   //getter and setter

 public String insertAtted() {
      System.out.println("Size is " + getEmpList().size());
      for (Employee ee : getEmpList()) {
           ee.setClient(lc.getClient());
           System.out.println("id " + ee.getId() + ", ClientId  " + ee.getClient().getId());
          System.out.println(" Attd list "+ee.getEmployeeAttdList());
          for (EmployeeAttd ea : (ArrayList<EmployeeAttd>) ee.getEmployeeAttdList()) {
                  System.out.println("--------Employee Attd in -------");
                  ea.setClient(lc.getClient());
                  ea.setDt(null);
                  System.out.println("att " + ea.getAttendance() + " dt " + ea.getDt() + " " + ea.getClient().getId());
        }
  }

  return SUCCESS;
}
}

显示错误

Jul 30, 2014 4:49:46 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: Error setting value
Class: com.opensymphony.xwork2.ObjectFactory
File: ObjectFactory.java
Method: buildBean
Line: 131 - com/opensymphony/xwork2/ObjectFactory.java:131:-1
    at com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor.getProperty(XWorkListPropertyAccessor.java:113)
    at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2317)
    at ognl.ASTProperty.getValueBody(ASTProperty.java:114)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.ASTChain.setValueBody(ASTChain.java:222)
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
    at ognl.SimpleNode.setValue(SimpleNode.java:301)
    at ognl.Ognl.setValue(Ognl.java:737)
    at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:234)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:183)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:170)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:148)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:318)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:231)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:131)
    at com.opensymphony.xwork2.ognl.accessor.XWorkListPropertyAccessor.getProperty(XWorkListPropertyAccessor.java:111)
    ... 64 more

并在显示输出错误后

Size is 2
id 1, ClientId  1
 Attd list []
id 2, ClientId  1
 Attd list []

如何为attendancedt分配值。

2 个答案:

答案 0 :(得分:0)

您使用的列表并非专门针对任何类型,OGNL如何知道它构建的对象类型。让列表输入到作为集合元素的对象。

private List<EmployeeAttd> employeeAttdList = new ArrayList<>(); 
//getter and setter

EmployeeAttd应该有一个默认构造函数,由对象工厂使用。

答案 1 :(得分:0)

您需要调用ParametersInterceptor才能使用custombean进行编辑。有关详细信息,请参阅以下链接

http://struts.apache.org/release/2.0.x/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/ParametersInterceptor.html