Easy tech stuff!

Generate dynamic id’s for struts html tags

Posted: September 23rd, 2008 | Author: Nischal Shetty | Filed under: Struts | Tags: , , , , | 7 Comments »

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 :)

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png

7 Comments on “Generate dynamic id’s for struts html tags”

  1. 1 Neel said at 3:46 pm on February 13th, 2009:

    Thanks a lot

  2. 2 Nischal Shetty said at 12:11 am on February 14th, 2009:

    @Neel
    Glad you found this article useful :)

  3. 3 Ashu said at 6:15 pm on July 4th, 2009:

    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. 4 Ian Walter said at 4:16 pm on November 20th, 2009:

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

  5. 5 Nischal Shetty said at 4:19 pm on November 20th, 2009:

    @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. 6 Ashok said at 11:26 pm on April 5th, 2010:

    Thanks much for the solution,it saved my day

  7. 7 James said at 12:34 pm on June 11th, 2010:

    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?


Leave a Reply