Bruce
Bruce

Reputation: 3528

How to handle complexity in jsps

This may be more of a rant than a question that can actually be answered. I'm not sure yet. I suspect it may be a situation that lots of people encounter though so hopefully it's a useful question.

We have jsps that present information that can be in about 40 different statuses. Let's say it's the status of a complicated financial product. So we want to output messages that are dependent on the status, for example status 10 might be "Your product is pending activation" and status 15 might be "Your product is active" and so on.

So it would seem that we could have a simple lookup that returns the appropriate messages depending on the status and it all works neatly.

But in reality, the status alone is not enough to determine the right message. If it's status 10 (pending activation) but it's within 1 week of the activation then we want to color the message red. If it's status 10 (pending activation) but there's an external reason delaying it, we want a message that provides a link to an explanation page. And so on.

So the status by itsef doesn't capture all the information needed to determine and create the message. There are lots of other extraneous pieces of information that are also required.

The way the legacy code works, all these pieces of information are loaded into the jsp, and then the jsp makes determinations based on this non-status information.

Now that we are redoing the site, I'm faced with the situation that we need this same status logic in multiple different places, but outputting different wrapping html.

In short, it's a big mess.

I think the ideal answer would be to refactor things so that all the decisions are taken out of the jsps. Ugh, I'm not sure what the correct way to handle it is. There's a lot of stringy complexity and I'm not sure where to start.

I'm not sure I've given enough detail to even make the problem clear, but if anyone has any advice, I'd be very grateful..

EDIT: Thanks for all your answers! The code is so tangly that I've sort of refactored it a bit by pulling common code into include files, so that I can include those files where they're needed. I'll definitely look at encapsulating things further with functions as you suggest. Will circle back round to your answers soon. Thanks again.

Upvotes: 3

Views: 134

Answers (3)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

I believe your existing JSPs are using a lot of <% // scriptlets %> to achieve at the final status message. I suggest you write a custom tag that factors all of that Java logic out of the JSP.

Handling message content

Implement the tag so that it takes anything that's required to generate the message (like your Product object itself) as one of its attributes.

WEB-INF/status.tld (Tag library descriptor)

<taglib ... >
  <uri>WEB-INF/status.tld</uri>
  <tag>
    <name>statusMesg</name>
    <tag-class>com.foo.StatusMesgTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>product</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>com.foo.beans.Product</type>
    </attribute>
  </tag>
</taglib>

Implement the Tag class with the Java code that's currently present in your scriptlets.

pubic class StatusMesgTag extends SimpleTagSupport {

  private Product product;

  public void setProduct(Product product) {
    this.product = product;
  }

  public void doTag() throws JspException, IOException {
    StringBuilder statusMesg = new StringBuilder();

    // process Product info to generate mesg

    getJspContext().getOut().println(statusMesg);
  }
}

Then invoke it in your JSP (with the Product object mapped as "product" in any one of your page, request or session scopes) as follows

<custom:statusMesg product="${product}" />

after adding the taglib directive to your JSP

<%@ taglib prefix="custom" uri"WEB-INF/status.tld" %>

Handling message rendering (view)

The part where you need to decide the colour the message should be rendered in should be handled with CSS stylesheets. You could have your custom tag determine the message priority and then return your message with an appropriate CSS style class like

<span class="priorityHigh">Needs activation within 1 week</span>

Your CSS would map different priorities to different colours like

.priorityHigh {color: red};
.priorityNormal {color: black};
.priorityLow {color: green};

Upvotes: 3

Ryan Quinn
Ryan Quinn

Reputation: 1205

Can you use a custom jstl tag with a couple parameters?

Upvotes: 0

fmodos
fmodos

Reputation: 4568

Create one Util class with a method to receive by parameter the status, date, etc... So the return of the method will be an object with properties for all the info that the jsp will need to render it. Something like this:

class MyStatusUtil{

    public static StatusInfo getStatusInfo(int status, Date date, etc...){
      StatusInfo info = new StatusInfo();
      if(status==10){
         info.setStatusName("....");
      }

      .....
     if(date==....){
       info.setColor("...");

     }

     if(somethinElse){
       info.setComment("....");
     }
     return info;
    }

}

StatusInfo class:

class StatusInfo{
    private String statusName;
    private String color;
    private String comment;

}

jsp:

<% StatusInfo info = MyStatusUtil.getStatusInfo(....) 
  //now render the html based on the StatusInfo data.
%>

Upvotes: 0

Related Questions