This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Bad practise?  JavaBean where setX() actually should be addX()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Bad practise?  JavaBean where setX() actually should be addX()?" Watch "Bad practise?  JavaBean where setX() actually should be addX()?" New topic
Author

Bad practise? JavaBean where setX() actually should be addX()?

Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
I've written a little JavaBean I used in JSP for alternating colors in navigation menus and table rows etc:



The setItem() method doesn't fit it's description and is only there so i can interact with it via JSP's setPoperty tag. Would this be considered a bad practise? Also, the toString() method returns a different value each time.... is that probably not a good idea?

I use it like this:

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The setItem() method doesn't fit it's description and is only there so i can interact with it via JSP's setPoperty tag. Would this be considered a bad practise? Also, the toString() method returns a different value each time.... is that probably not a good idea?


Yes to both. Use setItem to set the collection itself and replace addItem with an indexed getter and setter setItem(int index, Value value) to set/get an entity. In the toString method you might want to iterate over all the items and concatenate them into a single string. Just my thoughts. Why do you want this cyclic iterator ?
[ September 08, 2007: Message edited by: John Meyers ]

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
Both points taken on board and agreed with I'll rewrite the bean. I think I need to implement a getNext() method because I do need some way of making it iterate cyclically.

The reason it's needed is for alternating colors (but more than just two colors). I wanted to avoid too much complex bitwise logic inside the JSP itself.

Rather than the typical boolean negation or modulus operations I can just keep using ${color.next} and let the bean deal with that logic.
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
I updated it to use an indexed setter system like so:



It's implemented like this:



I have dynamic menu options which are displayed and they are colorful buttons on a dark background. I wanted to avoid the crazy logic determining what color to use in the JSP

 
Don't get me started about those stupid light bulbs.
 
subject: Bad practise? JavaBean where setX() actually should be addX()?
 
Similar Threads
Listing and Action Form Validation
JSTL & EL, not resolving my ${identifier} syntax?
Java Bean in JSP
JSP Displays Source
Generate Radio Buttons