aspose file tools*
The moose likes Struts and the fly likes automation and code factoring with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "automation and code factoring with struts " Watch "automation and code factoring with struts " New topic
Author

automation and code factoring with struts

jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31085
    
163

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
jonathan Greens
Ranch Hand

Joined: Apr 07, 2004
Posts: 139
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?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31085
    
163

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
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!!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Jonathan,

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
sanat
Ranch Hand

Joined: Dec 08, 2004
Posts: 111

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
thank you! I will try the custom tag library approach... if this wont' work, someone please let me know before i die trying
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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
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
Junilu,
You are absolutely right! I should not use java code. I will use either jsp:include or tiles.
thank you!!
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by jonathan Greens:
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?


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://aspose.com/file-tools
 
subject: automation and code factoring with struts