• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5291
  • Last Modified:

Struts - How to retrieve the dynamic form objects in the Action class on Form submitting ?

Hi All,

I have an dynamic html form. Using struts I am able to display the fields in the JSP page using the <c:forEach tag. But on submitting the form I have to update the fields in the database.
What I want in the action class is to retrieve the list of objects. I was thinking about using DynaActionForm. But I am not sure how to go for that.

In the struts config file I have given like
<form-beans>
        <form-bean name="monthlyFeeForm" dynamic="true" type="org.apache.struts.action.DynaActionForm" >
              <form-property name="monthlyFees" type="java.util.ArrayList" />  
        </form-bean>
    </form-beans>
 
Can I do like this ?
I have declared the bean class like

public class MonthlyFeeForm extends DynaActionForm {
  private String prop1;
  ....
  .....

public void setProp1(String prop1){
}

public String getProp1(){
}
.
.
.
}

In the above Form class how can I set the ArrayList property defined in the <form-bean >tag?  

if I define the bean class as above, in the Action class how I can retrieve the list of MonthlyFee objects or Is it possible to retrieve the objects like  

public class FeeAction extends MappingDispatchAction {

public ActionForward setMonthlyFee(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
DynaActionForm feeForm = (DynaActionForm) form;
ArrayList feeList = (ArrayList) feeForm.get(" ... ");  

}

Now I am using the jstl <c:forEach tag to populate the list in the JSP page. Is there anything like the property or id of the <logic:iterate>  tag. Or if I use the iterate tag I can work around as something like above. Since I am somewhat new to struts I am in a confusion like how to go for it.    

Hopes some body can help to work out this.

Thanx in Advance,
Susan
0
smj100
Asked:
smj100
3 Solutions
 
runa_paathakCommented:
Is there any specific reason why you want to use DynaActionForms? I think you can just use regular ActionForms.
0
 
bloodredsunCommented:
With JSTL you just access it directly using dot notation (you can also prepend the scope if you want)

<c:forEach var="listItem" items="${feeForm.feeList}">
      <c:out value="${listItem}"/>
</c:forEach>

There's loads of articles about how much better HSTL is than logic tags, a simple google will give them to you.
0
 
smj100Author Commented:
Hi runa,

No I don't have any specific reason to use DynaActionForms. If I use regular ActionForm how I can retrieve the objects in the Action class like


public ActionForward setMonthlyFee(ActionMapping mapping, ActionForm form, HttpServletRequest request,
          HttpServletResponse response) throws Exception {

FeeForm feeForm = (FeeForm) form;
ArrayList feeList = (ArrayList) feeForm.get(" ... ");     /** I am confused here */  

}

and my Jsp page is like

<form id="form1" name="monthlyFeeForm" method="post" action="setMonthlyFee">

<c:forEach var="monthlyFee" items="${monthlyFeeList}" >                  
   <input type="hidden" id="feeId" name="feeId" value="${monthlyFee.id}";/>
   <input type="text" id="feeValue" name="feeValue" value="${monthlyFee.feeValue}";/>
</c:forEach>

</form>




0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
runa_paathakCommented:
See the Map-backed ActionForms section on this page. I think this is what you are looking for:

http://www.jsn-server3.com/struts-documentation/userGuide/building_controller.html
0
 
smj100Author Commented:

Ya Somewhat like. But my jsp page is add/modify page. While adding I won't be having an id while submitting. Also if I have n number of properties, can I use the same logic ?
0
 
runa_paathakCommented:
If you have many properties like feeList, you have to use an ArrayList for each of them and use the same logic.
0
 
smj100Author Commented:
I have only one List, but the bean contains more than one property like feeID, feeValue, Company etc.
0
 
runa_paathakCommented:
If you know which are the properties you will have, there is no need to go for any of these complicated solutions. From what you are telling, I understand that you have a HTML form with many fields like feeId, feeValue, company etc. If this is the case, you just need to have an ActionForm with those fields. Then in your Action, you can do form.getFeeId(), form.getFeeValue() etc.
0
 
smj100Author Commented:
I fear I haven't put the requirement correctly/ cofused you. In the html form  n number of feeId fields are there.
like

<tr>
<input type="text" name="feeId" value="" />
<input type="text" name="feeValue" value="" />
<input type="text" name="company" value="" />
</tr>
<tr>
<input type="text" name="feeId" value="" />
<input type="text" name="feeValue" value="" />
<input type="text" name="company" value="" />
</tr>

the no. of rows is dynamic.



0
 
runa_paathakCommented:
I think I understood it this time. :) See if this helps: http://www.jguru.com/faq/view.jsp?EID=1067793
0
 
smj100Author Commented:

Not helping much. But gave some info on this.  Any way Thanx for trying to help.
 
0
 
boonlengCommented:
You can try using nested beans with iteration.
See http://www.laliluna.de/struts-nested-iteration-tutorial.html (Example 3)
0
 
runa_paathakCommented:
You can use map-backed method as follows: have an ArrayList for each field like feeId, feeValue, company in your ActionForm. See the first link I posted on map-backed ActionForms.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now