6

I need to build a table in a JSP. I have an arraylist with a bunch of beans, and the beans were made from a resultset, just from the rows returned from a DB call.

Depending on the data, I want to show different things. An example would be, if the name in the bean starts with 'a', highlight the name, if it starts with 'b', make the name red but not highlighted (i think that covers my question/situation).

If I do not have logic in the JSP, how would I control this?

bmw0128
  • 13,470
  • 24
  • 68
  • 116

4 Answers4

6

One way to do this is to write a function that lives inside the bean class, or perhaps more properly inside a wrapper for the bean class:

public class BeanFormatter {

  private Bean bean;

  public BeanFormatter(Bean myDataBean) {
    this.bean = myDataBean;
  }

  public String getFormattedHTML() {
    //put your logic here. Return the necessary HTML based on the bean.
  }
}

It's possible that what you want to return is not HTML in format of a String, but a div name or other css class to wrap the data in. But you could just write another method such as getDisplayCSSClass().

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
0

You could create a "translator" object that has one (or more) methods that take a bean, and return formatted HTML.

ffxtian
  • 559
  • 2
  • 5
0

In case that you need to have some special behavior on view, and you want to keep clean JSPs you should consider creating of new tag. You should know that there is few different kinds of tags. And because your tag is responsible for creating visual component i would suggest usage of tag files.

Check this tutorial to get base idea how it is working.

IgorMadjeric
  • 389
  • 2
  • 3
0

Actually, what do you mean under the word logic? Thing that you described relates to display logic, it's permissible to put such kind of logic into jsp files. Most of JSTL tags is designed for this job. In case you mean business logic, java classes(ejb, spring beans or simple classes) is the only place for it in good design(even not in servlets/controllers which is better for control logic)

maks
  • 5,911
  • 17
  • 79
  • 123