开发者

Accessing JavaBean Methods using EL from JSP

开发者 https://www.devze.com 2023-02-17 13:28 出处:网络
I\'m migrating an old application from JSP 1.1 to JSP 2.1, and trying to do everything without scriptlets. I have a JavaBean that I create, populate and insert into the page scope via a CustomTag.This

I'm migrating an old application from JSP 1.1 to JSP 2.1, and trying to do everything without scriptlets. I have a JavaBean that I create, populate and insert into the page scope via a CustomTag. This JavaBean exposes some methods that transform data, generate HTML snippets, etc, based on it's instance variables.

When I need to access a property in the JavaBean I use:

${mybean.property}

But since JSP 2.1/EL 2.1 don't support calling methods on your beans (this requires JSP 2.2/EL 2.2), I'm trying to determine the best way to expose such utility methods to my JSP pages without resorting to scriptlets.

Here is an example of the methods on the JavaBean that I need to access:

public String getThumbColor() {
   String tbgcolor = "#FF0000";
   if (this.getJavaBuildTotal() > 0 && 
        this.getJavaBuildBroke() == 0 && 
        this.getCeeBuildBroke() == 0 && 
        this.getJavaBuildInvalid() == 0 && 
        !this.hasBuildException()) {
      tbgcolor = "#00FF00";
   }
   else if (this.getJavaBuildTotal() == 0 && this.getCeeBuildTotal() == 0) {
      tbgcolor = "#f7f814";
   }
   return tbgcolor;
}

It feels like converting this (and the 10 or so other methods like it) entirely to JSTL in the JSP would be muddying up my JSP page.

Since I'm already doing a large refactoring, I don't mind drastic changes; yet I can't think of anyway to eliminate my need of certain methods that use conditionals for deciding on return v开发者_运维问答alues, generate small HTML snippets, etc.


Have a look at JSTL. It offers at least a basic set of core tags to control the flow in the page. Use this instead of if/else/switch whatever in scriptlets. It also offers several utility methods in the functions taglib. Basic String operations such as substring and so on are offered by this. You could also homegrow custom EL functions yourself which can call public static methods with arguments.

See also:

  • How to avoid Java code in JSP files?
  • Hidden features of JSP/Servlet (contains an EL function example)

Update as per the information in the comment

They make decisions based on the instance variables in the class and respond accordingly

You could add getters for those variables and use this in <c:if> or whatever.

<c:if test="${bean.property == 'somevalue' && bean.otherproperty != 'foo'}">

You could even wrap this in a boolean getter in the bean if those values are for example constants.

public boolean isCondition() {
    return "somevalue".equals(property) && !"foo".equals(otherproperty);
}

which you use as follows:

<c:if test="${bean.condition}">

There are lot of ways depending on the concrete functional requirement, which is still vague in your question.

See also:

  • Java EE 5 tutorial - Operators in EL

Update 2: as per the new example in your question, presentation specifics should go in the view (JSP). The bgcolor is part of the presentation. I'd suggest to replace this by an enum.

public enum Status {
    OK, FAIL, NONE;
}

public Status getStatus() {
   if (this.getJavaBuildTotal() > 0 && 
        this.getJavaBuildBroke() == 0 && 
        this.getCeeBuildBroke() == 0 && 
        this.getJavaBuildInvalid() == 0 && 
        !this.hasBuildException()) {
      return OK;
   }
   else if (this.getJavaBuildTotal() == 0 && this.getCeeBuildTotal() == 0) {
      return NONE;
   }
   return FAIL;
}

And declare the color in the view

<c:set var="bgcolor" value="${(build.status == 'OK') ? '#00ff00' : (build.status == 'FAIL') ? '#ff0000' : '#f7f814'}" />
...
<tr bgcolor="${bgcolor}">

Or, better, make a CSS class of it

<tr class="build ${build.status}">

with in a style(sheet)

tr.build.OK { background: #00ff00; }
tr.build.FAIL { background: #ff0000; }
tr.build.NONE { background: #f7f814; }


Depending on your application server / framework, etc you might be able to just override the default EL implementation for a EL2.2 one (I have successfully used Tomcat 7 EL 2.2 on a Tomcat 6.0.20 / JSF 2.0 web application). Other than that you can create a <call-method object="..." method=""><param=".."/>...</call-method> tag and substitute the scriptlets.

Anyway if you don't mind drastic changes you should eliminate these calls from your JSPs and go for a MVC architecture. You would have:

  • Controller components (typically servlets / filters) that receive client requests, decode them and invoke the appropiate Actions that update the application Model.

  • View components (typically JSP pages) that are forwarded the request once the Actions have finisihed, so they only need to read the updated Model and dump it into the response to the client.

Most popular Java web frameworks support this architecture (Struts, JSF, Spring MVC, ...)

0

精彩评论

暂无评论...
验证码 换一张
取 消