Solved

Dynamic form processing in struts

Posted on 2006-12-01
11
1,168 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:jaggernat
Comment Utility
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
Comment Utility
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
Comment Utility
>>>>>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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

12 Experts available now in Live!

Get 1:1 Help Now