Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep logic out of a JSP? [duplicate]

Tags:

java

jsp

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?

like image 236
bmw0128 Avatar asked Apr 18 '13 19:04

bmw0128


1 Answers

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().

like image 88
Nathaniel Ford Avatar answered Sep 22 '22 18:09

Nathaniel Ford