0 Replies Latest reply: Aug 27, 2008 8:53 AM by 843844 RSS

    Struts 2: Doesn't populate an array of java beans in action classes

    843844
      Hello,

      Currently i am working on Struts 2 based application, in which i need to populate an array of Java beans from the form.

      The problem, I am facing is:
      Struts 2 is not instantiating an array of beans

      I have gone through the Data transfer and Type Conversion of struts of 1.2, but every where I can see only following:

      1 An array of primitive data types
      2 List of primitive / Java bean
      3 Map of primitive / Java bean

      but i can't see the docs on An array of Java Beans.

      What I can't do in my application is:

      1 Can not convert an array of beans into java.util.List<beans>
      2 Can not instantiate the bean array (As the number of elements will be decided by the front end - form)


      Please have a look at my configuration file:

      struts.xml:+


      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">
      
      <struts>
      <constant name="struts.devMode" value="true" />
      
      <include file="test/test.xml"/>
      </struts>
      test.xml:+
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">
      
      <struts>
      <package name="test" namespace="/test" extends="struts-default">
      
      <action name="TestInput">
      <result>/test/input.jsp</result>
      </action>     
      
      <action name="TestOutput" class="test.Controller">
      <result name="SUCCESS">/test/output.jsp</result>
      </action>
      
      </package>
      </struts>
      Controller.java+
      package test;
      
      public class Controller {
      
      private String id = null;
      
      private Contact[] contacts = null;
      
      public String execute()  {
      System.out.println("***********************************");
      System.out.println("id::: " + getId());
      System.out.println("contacts::: " + getContacts());
      System.out.println("***********************************");
      return "SUCCESS";
      }
      
      public Contact[] getContacts() {
      return contacts;
      }
      
      public void setContacts(Contact[] contacts) {
      this.contacts = contacts;
      }
      
      public String getId() {
      return id;
      }
      
      public void setId(String id) {
      this.id = id;
      }
      }
      Contact.java:+
      package test;
      
      public class Contact {
      
      private String name = null;
      
      public String getName() {
      return name;
      }
      
      public void setName(String name) {
      this.name = name;
      }
      }
      + input.jsp:+
      <%@ page contentType="text/html; charset=UTF-8" %>
      <%@ taglib prefix="s" uri="/struts-tags" %>
      <html>
      
      <head>
      <title>Test Form</title>
      </head>
      <SCRIPT type="text/javascript">
      // Total number of contacts in the mail request form
      var totalContacts = 0;
      
      function addContact() { 
      var tbody = document.getElementById('testTable').getElementsByTagName('tbody')[0];
      var row = document.createElement('TR');
      
      var td1 = document.createElement('TD');
      var td2 = document.createElement('TD');
      var inputBox = document.createElement('INPUT');
      
      td1.innerHTML = new String('Contact ' + (totalContacts + 1) + ' :');
      inputBox.type = 'text';
      inputBox.name = 'contacts['+totalContacts+'].name';
      //inputBox.name = 'contacts.name';
      td2.appendChild(inputBox);
      
      row.appendChild(td1);
      row.appendChild(td2);
      
      tbody.appendChild(row);
      
      // Add to the total number of contacts
      totalContacts += 1;
      }
      </SCRIPT>
      
      <body>
      <h4>Mail Request Form</h4>      
      <s:form action="TestOutput">
      <table border="1" id="testTable">
      <tr>
      <td colspan="2">
      <s:textfield name="id" label="Request Id"/>
      </td>
      </tr>
      <tr>
      <td align="left">
      <input type="button" value="Add Contact" onklick="javascript:addContact();"/>
      </td>                              
      <td align="right">
      <s:submit/>                         
      </td>     
      </tr>     
      </table>
      </s:form>
      </body>
      </html>
      
      
      
       
      + output.jsp:+ Not concerned right now

      Exception got in the logs after submitting the form:+
      java.lang.InstantiationException: [Ltest.Contact;
      at java.lang.Class.newInstance0(Unknown Source)
      at java.lang.Class.newInstance(Unknown Source)
      at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:123)
      at com.opensymphony.xwork2.util.InstantiatingNullHandler.createObject(InstantiatingNullHandler.java:123)
      at com.opensymphony.xwork2.util.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:104)
      at ognl.ASTProperty.getValueBody(ASTProperty.java:94)
      at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
      at ognl.SimpleNode.getValue(SimpleNode.java:210)
      at ognl.ASTChain.setValueBody(ASTChain.java:168)
      at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
      at ognl.SimpleNode.setValue(SimpleNode.java:246)
      at ognl.Ognl.setValue(Ognl.java:476)
      at com.opensymphony.xwork2.util.OgnlUtil.setValue(OgnlUtil.java:186)
      at com.opensymphony.xwork2.util.OgnlValueStack.setValue(OgnlValueStack.java:158)
      at com.opensymphony.xwork2.util.OgnlValueStack.setValue(OgnlValueStack.java:146)
      at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:193)
      at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:159)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
      at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
      at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
      at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
      at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
      at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
      at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
      at java.lang.Thread.run(Unknown Source)
      
      
      
       
      Please give me suggestions to move forward

      Thanks in advance

      Vikas Parikh