Bad practise? JavaBean where setX() actually should be addX()?
Chris Corbyn
Ranch Hand
Joined: Jan 14, 2007
Posts: 114
posted
0
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?
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 ]
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
posted
0
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