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

design question

ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
this is (probably) a "there is no right answer" question about design:
when designing a site that will have a standard header, is it considered better form to have a Header object that will get instantiated on every page just so you can call a method that does some processing/database hits and then writes the header html, or is it better to have a class with a bunch of static methods that handle this kind of thing (dynamic headers, footers, breadcrumbs, etc.). btw, i'm using jsp.
or maybe there's another way altogether...
thanks!
ed
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Here's a solution
1) Create a JavaBean that handles the database.
2) Create a file header.jsp that uses the bean and displays the info.
3) In all other files include the header.jsp at the required place.
use the include directive <%@ include file='header.jsp' %> or
use the include action <jsp:include page='header.jsp' />
ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
that sounds good, but what if the header builds a breadcrumb that is based on a node id on the page that does the including? is there a way to get the bean some information it needs to build the correct header?
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
If implementing breadcrumb like feature is the problem, here is a taglib that you may want to try.
http://www.servletsuite.com/servlets/navtag.htm
I haven't tried it myself though.
Correct me if I am wrong, but this is what i understood from your message. Each page has a node id. You want the bean to know about this id before the header is generated. To do that, you can set the nodeId value into the bean using jsp:setProperty or scriptlet before you include the header jsp. When the header page is executed, it can use the value of the nodeId already present in the bean.

ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
yes -- you understand correctly. i already have this implemented in the following form:
- jsp page instantiates a Header object with the page's nodeId set as a property.
- the header object in turn instantiates a Breadcrumb object, which also receives the nodeId. the constructor of the Breadcrumb object hits the database and builds the breadcrumb based on the nodeId's place in a hierarchy.
- the jsp page calls the Header class's method writeHeaderHtml(), which returns the correct html for the header (and in the process, calls a method of the breadcrumb class to get the correct breadcrumb)
soooo, my original question was, is it better to have Header and Breadcrumb objects when all they are used for is returning html -- or would it be a better idea to have a general class that contains a handful of class methods that handle writing these dynamic page elements to each jsp template?
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Oops! my mistake. Sorry I didn't read the question properly.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Are you using Struts?


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
no, i am not.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
soooo, my original question was, is it better to have Header and Breadcrumb objects when all they are used for is returning html -- or would it be a better idea to have a general class that contains a handful of class methods that handle writing these dynamic page elements to each jsp template?

well struts at jakarta.apache.org defines an MVC framework and provides tag libraries that I'm just learning about at the moment and which seem to have huge potential for making alot of things alot easier.
I guess it depends on whether your header and breadcrumbs are dynamic or not. How is the HTML for them written? In system.println("<HTML>")? As a custom tag?
Adam
ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
the breadcrumb and header are dynamic, based on the page's nodeId. after doing the database hits for the breadcrumb, the html gets dynamically built and then stored in a string.
that string is returned by calls to the get method for the string variable. i'm new to java, coming from a scripting background (tcl, php) where i'm used to having libraries for all of my functions to handle the dynamic parts of the highly reused elements of the site. it seems like a hack to me to use an object for something (header, breadcrumb) when all you are ever interested in is returning the html for a nav element. this is what got me thinking, maybe i should just use one class, perhaps call it siteElements, that has all class methods to handle the returning of dynamic html -- sort of functioning like a library.
after seeing your post, i read up on struts, but i'm not quite ready yet to embark on another new thing before i'm a little more sound on the new thing i'm already tackling.
thanks,
ed
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
I know what you mean about new stuff. When I first looked at struts, I was blown away - they were talking about beans, xml, servlets and custom tags all at once. Not to mention the SQL and the HTML.
As far as the idea of an object and what to use it for, it is analogous to libraries I think but I've never used PHP. I always look at objects from an object-oriented design point-of-view. Objects encapsulate things in a real world kind of way and don't have anything to do with anything else unless they provide a distinct method for that interaction. They also hide unnecessary bits that only concern the object itself. Like dog.bark(), or in your case header.getHTML(nodeID)
What seems non-OO to me in the way you are thinking is that "it just provides HTML" - well half of your whole project is about providing HTML (and the other half about processing the results). It's a bit like saying, "well, dog just interacts with its environment, so maybe I should put it in a catch-all class"
Just my 2 cents
Adam
ed copony
Greenhorn

Joined: Nov 29, 2001
Posts: 10
ok -- cool. nice way of putting it.
thanks for your time and cents.
ed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: design question