Solved

Dynamic form processing in struts

Posted on 2006-12-01
11
1,191 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Google always has something new and amazing up its sleeve, and the most current thing that they have been working on is another step in the evolution of Google Search, from machine learning to its brilliant successor, deep learning.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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