• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

design question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! my mistake. Sorry I didn't read the question properly.
 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Struts?
 
ed copony
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, i am not.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok -- cool. nice way of putting it.
thanks for your time and cents.
ed
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic