Thursday, December 15, 2011

Populating An Array List on Form Submit in Struts 2.0

I always came across situations where I wanted that if there may have been some way to populate a whole list by a form submit. This feature was there with the request.getParameterValues(), but it would always return a String array. I needed this in the following two situations.
  1. Populating a list on form submit (String, int, float, double, boolean)
  2. Populating a list of keys and their values.


Populating a list on form submit:
            This is very simple. Write the following html in a jsp file say index.jsp

            <form action="submitAction.action" method="post">
                  <input type="text" name="idList" /><br>
                  <input type="text" name="idList" /><br>
                  <input type="text" name="idList" /><br>
                  <input type="text" name="idList" /><br>
                  <input type="text" name="idList" /><br>
                  <input type="text" name="idList" /><br>
                  <input type="submit" value="submit">
            </form>

Yes, Struts 2.0 works with normal html tags but they can only be used for submit purposes and not for display. For display, we will have to use <s:property> tag of Struts 2.0 if we want to use html tags.
Now make an action in struts.xml as
  
<action name="view" class="com.soft.struts.actions.SubmitAction" method="execute">
<result name="view" >view.jsp</result>
</action>

Now create a class named SubmitAction.java and declare a list as a private member in the action class and make its getter/setter as.

private List<int> idList;

public List<int> getIdList() {
      return idList;
}

public void setIdList(List<int> idList) {
      this.idList = idList;
}

That’s it in the action class. Now make a method for the action declared in Struts.xml as follows
      public String execute() throws Exception {
            for(int i=0; i<idList.size;i++){
                  System.out.println(idList.get(i));
}
            return "view";
      }
Make a jsp named view.jsp at the root of the application, so that the action forwards request to it.

That’s it. When you run the application and insert values into the text fields of the index.jsp and then press submit. Those values will automatically be inserted into the idList and you will be able to access the list in execute function.

Populating a list of keys and their values:
            This one is a bit tricky. If you want to populate a list of hidden keys and a list of their values, write the following html

<form action="submitAction.action" method="post">
           <input type="hidden" name="keyList" value=”Address1”/><br>
           <input type="text" name="valueList" /><br>

           <input type="hidden" name="keyList" value=”City”/><br>
           <input type="text" name="valueList" /><br>

           <input type="hidden" name="keyList" value=”State”/><br>
           <input type="text" name="valueList" /><br>

           <input type="submit" value="submit">
</form>

In the action class, declare two private lists and make their getters and setters as
     
private List<String> keyList;
      private List<String> valueList;

      public List<String> getKeyList() {
            return keyList;
      }

      public void setKeyList(List<String> keyList) {
            this.keyList = keyList;
      }

      public List<String> getValueList() {
            return valueList;
      }

      public void setValueList(List<String> valueList) {
            this.valueList = valueList;
      }


When you press submit, the keys will be populated into keyList and the values will be populated into valueList in the same order.

I hope it may help someone out there.

No comments:

Post a Comment