Solved

Dynamic form processing in struts

Posted on 2006-12-01
11
1,180 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
  • 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 250 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading data  from excel sheet and displaying 1 74
java cigar challenge 58 206
excpetion with multiple catch 11 113
Books that can get me started on JAVA 2 81
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
The new Gmail Phishing Scam going around is surprising even the savviest of users with its sophisticated techniques. This attack comes as a nightmare trifecta for email filtering services; sent from a familiar contact, using authentic tone and verbi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now