?
Solved

Dynamic form processing in struts

Posted on 2006-12-01
11
Medium Priority
?
1,225 Views
Last Modified: 2009-07-29
Hi all,
 I need your help regarding dynamic form elements processing & validations.

Here is my JSP Script,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<TITLE>Processing Dynamic Forms</TITLE>

<script type="text/javascript">
//function which adds new row
function AddRow()
{
      tb=document.getElementById("demo");
      // attach counter
      lnrows = tb.rows.length;
      //alert(lnrows);
      newrow = tb.insertRow(lnrows);
      
      var fourth_col = "amt"+lnrows;
      cell3=newrow.insertCell(0);
      cell3.innerHTML="<center><input type='text' id='"+fourth_col+"' name='"+fourth_col+"' size='10'/></center>";
      
      document.getElementById("cntr").value = lnrows;
}

// function to delete row
function DeleteRow()
{
      tb=document.getElementById("demo");
      lnrows = tb.rows.length;
      //alert(lnrows);
      if(lnrows > 2)
      {
            tb.deleteRow(lnrows-1);
            document.getElementById("cntr").value = tb.rows.length - 1;
      }
}
</script>
</HEAD>

<BODY>
<html:form action="/dynaActions">
<TABLE id='demo' align='center' width='80%' border='1'>
<TR>
      <TH width='25%'>Party Name</TH>
</TR>
<TR>
      <TD align='center'><input type='text' id="party1" name="party1" size="30" /></TD>
</TR>
</TABLE>


<TABLE align='center' width='80%' border='0'>
<TR align="right">
<td>

      <html:button property="Add" value="Add" onclick="AddRow();" ></html:button>
      <html:button property="Remove"  value="Remove" onclick="DeleteRow();"></html:button>

</td>
</TR>

<TR align="center">
<td>
      <input type='hidden' id="cntr" name="cntr" value="1" />
      <html:submit/>
</td>
</TR>
</TABLE>

</html:form>

</BODY>
</html:html>

  As you seen there is dynamically form elements generated as many as required. Now how do I process this with ActionForm & Action in order to get me all the data properly?

Regards,
Mahesh
0
Comment
Question by:anilgalve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
11 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 18056324
Hi,

look at the example(s) here, try looking into all the left side
Struts
Lesson 1
Lesson 2
Lesson 3 ....

http://www.learntechnology.net/struts-lesson-1.do

R.K
0
 

Author Comment

by:anilgalve
ID: 18059402
Hi rama_krishna580,
  The link which you have given me is just for basic struts. What I want is dynamic Action form bean so as I can get all the values for dynamic generated elements i.e.
  party1, party2, party3, party4, party5, party6 ..................................... upto anything.

 Now these elements are generated on the fly & at client side using javascript hence I can't make use of struts tags, I need to use pure html to generate those elements. All those things have been done already as I posted earlier.

 Now I have to process above form using struts framework.
 I hope it clears now.

Regards,
AG
 
 

 
0
 
LVL 10

Accepted Solution

by:
jaggernat earned 1000 total points
ID: 18060105
To get all values from the input field,you have to use a DynaValidatorForm in struts-config like this

<form-beans>
<form-bean name="FORMBEANNAME" type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="party1" type="java.lang.String"/>
<form-bean>
</form-beans>
 
<action
        path="/getQuotes"
        type="com.kainos.quickquotes.struts.QQGetQuotesAction"
        name="FORMBEANNAME"
        parameter="save"
        scope="session">
        <forward name="success" path="/pages/jsp/result.jsp"/>
        <forward name="back" path="/userform1.do"/>
 </action>



If you are trying to get a bunch of elemets in a list , do this
 
<form-bean name="FORMBEANNAME" type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="partyList" type="java.util.List"/>
<form-bean>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:anilgalve
ID: 18074712
Hi jaggernat,
 Thanks for reply.
 As you said, to track bunch of elements I need to make use of list. That seems good.
 Can you send me code regarding this. I never done such things.
 As you can see the number of elements are not fixed they can be as much as user wants to create.
 I need to check again the elements should not be empty.
 Pls send me some example code which give me more clear picture.

 Thanks & Regards,
  Ag    
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 18081641
doesnt have to be List. Can also be an array like this <form-property name="name" type="java.lang.String[]"/>


look at these examples


http://www.developer.com/java/ejb/article.php/3321521

http://forum.java.sun.com/thread.jspa?threadID=717030&messageID=4141686
0
 

Author Comment

by:anilgalve
ID: 18179879
Thanks for links.
  But can you tell me how do I process using String[] array.
For example if I go with party[1], party[2], party[3],.......................etc.

How do I process it. Give me some sample code. Again I need to validate them as well.

0
 
LVL 10

Expert Comment

by:jaggernat
ID: 18247454
>>>>>How do I process it
the link i gave you clearly explains this .
this is how you process an array elements in your action class


DynaValidatorForm df = (DynaValidatorForm) form;  
Person[] p = (Person[]) df.get("people");
for (int i = 0; i < p.length; i++)
 {
//iterate through the array values and do something
 }
 
0
 

Author Comment

by:anilgalve
ID: 18282852
This will be done serverside validation, but If I need to make use of validation.xml, how should I proceed?
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 18289341
go through this article (page1 and page2). it  explains how you have to use validation.xml

http://www.onjava.com/pub/a/onjava/2002/12/11/jakartastruts.html?page=1
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question