Solved

Dynamic form processing in struts

Posted on 2006-12-01
11
1,200 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project in eclipse 11 60
dynamic reloading of jsp in jetty 2 121
Unhandled exception type Exception 18 42
jsp login check 12 42
Are client relationship the only driver of a successful MSP? While important, client relationships are only one component. Learn how else MSPs can broaden their horizon and differentiate themselves.
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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