Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

how to dynamically generate struts2 form tags and populate with action

 
Ranch Hand
Posts: 47
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am working on a Canteen Software project based on Struts2 / jsp / hibernate / MYSQL.

I have a requirement that in a jsp page i have to add a row of struts2 input fields dynamically when user clicks on Add Button.
Along with the row 3 buttons are also added Save, Cancel, Edit

All this is working allright using javascript.

my problem is that since these set of rows are loaded dynamically as per user choice, hence they have no relation to action properties.

how do i populate these to action properties ?

Waiting for your advises

Regards
Vijay Kumar




 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't understand what you want to do, can you share screen shot as per your requirement.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vijay dadhwal wrote:Dear All,

I am working on a Canteen Software project based on Struts2 / jsp / hibernate / MYSQL.

I have a requirement that in a jsp page i have to add a row of struts2 input fields dynamically when user clicks on Add Button.
Along with the row 3 buttons are also added Save, Cancel, Edit

All this is working allright using javascript.

my problem is that since these set of rows are loaded dynamically as per user choice, hence they have no relation to action properties.

how do i populate these to action properties ?

Waiting for your advises

Regards
Vijay Kumar







if your input fields have same name, then you can get these using array.
 
vijay dadhwal
Ranch Hand
Posts: 47
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have to add students details in a jsp as per attached pictures.


The problem i am facing is that the row of input fields (performance.standard & performance.marks) are generated dynamically on the Add Button Click (using javascript), hence thereby there can be any no of rows of such input fields. Though i have decalred performance & student object in the action properties but action performance property setter is never fired after action submit. Only other properties which are hardcorded in jsp page using struts2 form tags are fired in the action.


how do i populate the dynamically added performance input elements row object in the jsp to action.
cosequently the target jsp output.jsp does not display performance object properties

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Ajax Test</title>
<style type="text/css">
.errorMessage {color:red;}
</style>
<script language="javascript">


function cost_addRow(tableid,button,value)
{
button.disabled = "true";
var table = document.getElementById(tableid);
var rowcount = table.rows.length;
var colcount = table.rows[0].cells.length;
colcount+=1; // provision for 1 more column for save buttons
var row = table.insertRow(rowcount);
for (var i=0;i<colcount;i++)
{
var newcell = row.insertCell(i);
var id=tableid+rowcount+i;
switch (i)
{
case 0:
newcell.innerHTML='<s:textfield id="'+id+'" name="perform.standard" value="'+value+'" size="20" theme="simple"/>';
break;
case 1:
newcell.innerHTML='<s:textfield id="'+id+'" name="perform.marks" value="'+value+'" size="10" theme="simple"/>';
break;
case 2:
newcell.innerHTML='<input id="'+id+'" type="button" value="Save">';
newcell.childNodes[0].onclick=new Function("saveRow('"+tableid+"','"+rowcount+"','"+colcount+"')");
break;
}
}

}

function saveRow(tableid,row_no,total_cells)
{
var table = document.getElementById(tableid);

for (var i=0;i<total_cells;i++)
{
var id = tableid+row_no+i;
var element = document.getElementById(id);
element.disabled="true";
}

var button = document.getElementById("performance_button");
button.disabled = "";
}
></script>

</head>
<body>
<table >
<s:form action="output">
<tr ><td>
<s:textfield name="stud.name" label="student name" size="20" />
<s:select label="student sex code" name="stud.sex" list="{'Male', 'Female'}" />
</td></tr>
<tr><td>
<table id="performance" >
<tr>
<td>Standard</td>
<td>Marks</td>
</tr>
</table>
</td></tr>
<tr><td> </td><td><input id="performance_button" type="button" value="Add" onclick="cost_addRow('performance',this,'' )" /></td></tr>
<tr><td> </td></tr>

<tr><td> </td>
<td>
<s:submit value="Save" theme="simple"/>
</td></tr>
</s:form>
</table>

</body>
</html>

[code]

Action Code
[code]
package test;
import com.opensymphony.xwork2.*;

public class studentaction extends ActionSupport
{
student stud;
performance perform;

public studentaction()
{
System.out.println("new action instance");
}

public student getStud() {
return stud;
}
public void setStud(student stud) {
System.out.println("stud setter");
this.stud = stud;
}
public performance getPerform() {

return perform;
}
public void setPerform(performance perform)
{
this.perform = perform;
System.out.println("performance setter");
}
public String collect()
{


return SUCCESS;
}

}

[code]

student class

[code]
package test;

import java.util.ArrayList;
import java.util.List;

public class student
{
String name;
String sex;
List<performance> performance = new ArrayList<performance>();

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<performance> getPerformance() {
return performance;
}
public void setPerformance(List<performance> performance) {
this.performance = performance;
}

}

[code]

performance class
[code]
package test;

public class performance
{
String standard;
int marks;

public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public int getMarks() {

return marks;
}
public void setMarks(int marks)
{
this.marks = marks;
}


}

[code]

output jsp
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<%@ taglib uri="/struts-tags" prefix="s"%>

1<s:property value="stud.name"/>
2<s:property value="stud.sex"/>
3<s:property value ="perform.standard"/>
4<s:property value ="perform.marks"/>

[code]
Picture1.png
[Thumbnail for Picture1.png]
Picture2.png
[Thumbnail for Picture2.png]
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic