开发者

How would you get a returned value from a JSTL tag back into an EL statement?

Basically I have a custom tag that handles querying a java object for me.

<c:set var="profit">
    <ct:get-profit transaction="${transaction}"/>
</c:set>

Now the problem is that I want to use that value (which is a float in an if statement, which I do as so:

 <c:when test="${profit > 0}">

When I do that though I end up getting the following error.

javax.el.ELException: Cannot convert -141.75 of type class java.lang.String to class       java.lang.Long

I ha开发者_JAVA百科ve no idea how I can make this work. I was under the impression that JSTL's would handle casting for you, is that incorrect? Either way, how would you go about making this work? Thanks


Can you try doing 0.00 instead of 0? <c:when test="${profit > 0.00}"> .

The reason you have to it is because 0 is getting treated as Long by the EL parser. However, "0.00" is getting treated as a float.


Everything which you set in the body of <c:set> is implicitly converted to String by Object#toString(). You'd like to use its value attribute instead which keeps the type unchanged.

I'd suggest to replace the <ct:get-profit> tag by an EL function. Since this tag doesn't seem to render any markup, you could do it as good (and better) with an EL function.

<c:set var="profit" value="${ct:getProfit(transaction)}" />

in combination with

public static float getProfit(Transaction transaction) {
    // Implement.
}

For a more detailed example how to configure such a function, check the bottom of this answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜