aspose file tools*
The moose likes JSP and the fly likes Generating fields dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Generating fields dynamically" Watch "Generating fields dynamically" New topic
Author

Generating fields dynamically

Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi,

I have a requirement where I need to generate a web page in that number of fields is not fixed,so I have to generate them dynamically using a plus button to add extra fields by users. Can somebody please assist me how can I go on this one and how to collect the values entered in those fields in java and persist them to DB as I can't fix number of table also.

Thanks,
Vighnesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

What have you thought of so far? How might you accomplish this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi,

Thanks for the reply.

I thought to use simple jsp tags or struts 2 tags but didn't come to know how can I do this. I hope you got my requirement correctly, my requirement is that "I need to have 3 fixed fields as a group and need to provide a plus button. on click of that plus button it should generate 3 more fields and so on" can you tell me how can I accomplish this?

Thanks,
Vighnesh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

At a high level, 2 means come to mind:

1) Using just JSP: When the button is clicked, the page is submitted so that the JSP can be re-ecexcuted putting the desired number of fields on the page. Use <c:if> for making descisions, and <c:forEacj> for looping.

2) Using JavaScript: When the button is clicked, script creates the new fields on the fly without any need to go back t the server.

The first method is terribly old-fashioned.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi,

Please see the attachment which shows my requirement. how to do this?

Thanks,
Vighnesh


[Thumbnail for requirement.jpg]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

I gave you two approaches. If you are looking for someone to write the code for you, you're in the wrong place.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi,

Without using javascript can't I do this other than the one way you provided? Because I can't use javascript as per my requirement. Is there any way to do this in struts 2? If yes can you help some sites where I can get examples or the way to implement this?

Thanks
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55

Hi,

Doing this in javascript is simple such as

<html>
<head>
<script language="Javascript" type="text/javascript">

var counter = 1;
var limit = 3;
function addInput(divName){
if (counter == limit) {
alert("Reached adding limit" + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry " + (counter + 1) + "
<input type='text' name='myInputs[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}

</script>
</head>


<body>
<form method="POST">
<div id="dynamicInput">
Entry 1
<input type="text" name="myInputs[]">
</div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
</body>
</html>

But even I want to know how the same can be achieved in struts2.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Without JavaScript to issue Ajax request, you are limited to refreshing the page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating fields dynamically