Hi folks, In my webapplications there are many html page with the similar look and structure. I am trying to factor out them. So I created utility functions to print out html code to the jspWriter. But I am not able to incorporate struts tags into these functions because they will just be printed without being rendered through the struts engine. So if I print <struts:radio .../> to jspWriter it will come out like <struts:radio.../> without any change. On the other hand if I don't incorporate struts tags into my utility functions and use just plain html code, I don't get to take the full advantage of struts tags. How do I get around this problem? thanks
Jonathan, You are correct that you can't have tags within tags. Don't know if this is the best way, but I factor the common code into a .jspf (JSP fragment) and jsp:include it in.
The latest version of JSP allows you to use this approach for taglibs as well, so it will get less awkward when we upgrade to this.
Jeane, thank you for the suggestion. However I am trying to do something different, maybe an example will elaborate. I have a utility function printTable1 that I want to use to print different tables because the tables in my jsps share a lot of common structures. Are you suggesting that I might create custom taglibs to do it with the latest jsp?
Jonathan, I still think that should go in a jspf. Once it's there, you can work on converting some of the java to struts logic tags or JSTL.
jonathan Greens
Ranch Hand
Joined: Apr 07, 2004
Posts: 139
posted
0
Jeane, even in a jspf, can I have nested tags like this? <html:<bean:write name="type"/> property=""/> sorry can you elaborate a little? what's your approach on factoring the code so that it can accept parameters and act as a template. thank you!!
With the obvious exception of custom tag implementation classes, there would have to be very compelling reasons to render HTML using Java code. As such, I would seriously question the appropriateness of what you are doing. HTML markup should be in JSPs, not in Java code.
I usually use Tiles to factor out common markup elements. If you are not using Tiles, use jsp:includes. Any kind of iteration or dynamic rendering such as rendering rows of data in tables would be accomplished with JSTL.
Originally posted by jonathan Greens: Jeane, thank you for the suggestion. However I am trying to do something different, maybe an example will elaborate. I have a utility function printTable1 that I want to use to print different tables because the tables in my jsps share a lot of common structures. Are you suggesting that I might create custom taglibs to do it with the latest jsp?
Hi,
I'm not sure if this is going to help or not, but give it a try,
the method is a utility method. So you can create a new tag library using this, and in your Tag class you can call this method. I'm writing a small example to do that below:
//SomeUtility.class // a static Method like your method
//TagClass in doStatrtTag call SomeUtility.aMethod()
//a tld file <tag> <name>test</name> <tagclass>TagClas</..> some attributes </tag>
in jsp you can use this like <prefix:test />
I'm using this type of approach for creating some fields on my jsp. If you need to display some static text you can use the same thing. Just write different methods for each section you want to display on the page.
sorry for bad english.
jonathan Greens
Ranch Hand
Joined: Apr 07, 2004
Posts: 139
posted
0
thank you! I will try the custom tag library approach... if this wont' work, someone please let me know before i die trying
I would only take the taglib approach if the html tables were to display read-only data.
Your example requires editable fields, so I would use static includes. I would choose static includes over tiles because it might get difficult trying to reference the proper ActionForm through the tile, since it might have a different name on different pages. With static includes, your Struts tags should not need a name attribute.
A good workman is known by his tools.
jonathan Greens
Ranch Hand
Joined: Apr 07, 2004
Posts: 139
posted
0
thanks you!
But static include can not allow me to include parameter types other than String, right? So if I have a table of content(2d Vector) that I want to pass it, i have to break it down individually as include parameters? So is it better off using tiles because put allow me to input arbitray types?
jonathan Greens
Ranch Hand
Joined: Apr 07, 2004
Posts: 139
posted
0
Junilu, You are absolutely right! I should not use java code. I will use either jsp:include or tiles. thank you!!
But static include can not allow me to include parameter types other than String, right? So if I have a table of content(2d Vector) that I want to pass it, i have to break it down individually as include parameters? So is it better off using tiles because put allow me to input arbitray types?
With static includes, you would write it exactly the way it would be if you just put the tags directly into the pages themselves. That's what static includes do - they insert the included .jsp before anything gets rendered and then renders the whole thing together.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: automation and code factoring with struts