Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

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

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic