Generate dynamic id’s for struts html tags

Do you use struts tags to generate html content? If yes, then sooner or later you’ll come across a scenario where you would generate html elements with the same name but would need different id’s for each element. I faced a similar predicament recently. I had no other option but to use scriptlets to generate id’s for the html elements. However, I soon found out using scriptlets within the struts html tags isn’t really straight forward.

Here’s the code I tried in my first attempt:

<%int i=0;%>

//iteration logic here

<html:text property=”example” styleid=”example<%=i%>”>

Well, if you write the code as shown above, the html code generated would be :

<input type=”text” name=”example” id = “example<%=i%>”>

and not

<input type=”text” name=”example” id=”example0″>

To get the expected result, i.e. for the scriptlet to work inside the struts html tag, write as below:

<html:text property=”example” styleid=’<%=”example”+i%>’

That’s it. If any of you ever find out a way to generate different id’s for html elements with the same name using strtus tags exclusively then do let me know :)

Tags: , , , ,


  1. Thanks a lot

  2. @Neel
    Glad you found this article useful :)

  3. The following code will work

    <%for( i=1;i

    <html:text property=” />
    <html:text property=” />
    <html:text property=” />
    <html:text property=” />
    <html:text property=” />
    <html:text property=” />
    <html:text property=” />

  4. Oh my god this problem was driving me crazy, thank you so much for posting this.

  5. @Ian

    Ah! Glad you found it useful. Its been really long since I updated this site. I guess I’ll have to start tech blogging again… :)

  6. Thanks much for the solution,it saved my day

  7. I can generate text boxes dynamically ok.. List is coming from query in DB. I am not sure how to write code in submit button in action form which will save back result to database.

    What would be code in action form to read all values?

  8. How to get the value for this property in Action class

  9. @Kalees ideally, you need to use a string array in your struts form to map fields which have the same name but multiple entries in html.

    If you directly want to retrieve the value of such fields, try using request.getParameterValues(“paramNameHere”) in your action class

  10. Works fine.

    Thanks a lot.

    Keep Posting…

  11. Don’t know Struts, but how about:

  12. Thanks man .. You really did good job by posting this… God bless my friend.. From this i really solved big problem … which i faced from long time… Thank you so mach…

  13. Thank you very much. I was looking for this solution.

  14. You saved my hair!! (whats left)

  15. Can you please post an example where in you generate dynamic elements and how do you get those in Struts Action Form???

    The way you have done is
    You have created text boxes with names
    example0, example1, example2 and so on..

    For this I need to know how many dynamic elements will be there at the time of writing the code, so that I can write getter setter in Action Form. Hence this approach is not going to work in case of struts.

  16. Oops, I guess I was wrong and you seem to be right enuf.

    because your property is same for all the html textboxes they should come in the example array in action form.

  17. Well, your code has a small mistake

    <html:text property=”example” styleid=”example”>

    You didn’t closed the tag in the example.

    It should be
    <html:text property=”example” styleid=”example”/>

  18. :(

    Another problem…

    Your code gets converted to

    <input type="text" name="example" value="[Ljava.lang.String;@8e" id="example”>

  19. <input type="text" name="example" value="[Ljava.lang.String;@8e" id="example”>


  20. Please remove my comments, your code is correct.

  21. thanks much!

  22. is it good but i am applying to my project.
    i am generating unique code for for each and every row for that i apply “for loop” the fornt end it’s showing that many times i was specified (i=15 for ex) how to avoid this one. let me know please

  23. I am creating a number of dropdown lists dynamically using java script in struts. I would like to be able to trigger an event when the selected dropdown list item changes properly .when click on ” add row ” button it trigger that event it will adding a row again i am selecting a value from list its over riding that resultant value how do i achieve this one let me know plz……

  24. thank you sooooo much …. This solved my issue,……

  25. Thanks man!!! a lot!

  26. Thanks dude.

    After lot’s of searching your code worked.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>