tags:

views:

174

answers:

3

Hi,

I know you can use the <jsp:useBean> tag to instantiate objects within JSPs without resorting to scriptlet code. However I'd like to instantiate an Integer who value is the result of an EL expression, something like:

<jsp:useBean id="total" class="java.lang.Integer">
    <jsp:setProperty name="amount" value="${param1 + param2}"/>
</jsp:useBean>

Of course this won't work because Integer objects don't have a property named 'amount', the only way their value can be set is via a constructor parameter (i.e. Integer objects are immutable). Is there any way to instantiate such an object and set it's value without using scriptlet code?

Thanks, Don

A: 

If you have a bean, can you just update the bean with param1 and 2? Create a method, setAmount(param1, param2), and set it before you use getAmount(), which is what the bean is going to call.

James McMahon
+1  A: 

Primitive wrappers also have no default constructor so you can't even initialize one that way.

I'm not sure that EL is supposed to be used in that way. It is more of a template language. It isn't clear what advantage what you are trying to do has over something like:

<%
  Integer total = new Integer(param1 + param2);
%>

And then just using <%= total %> where you need the value to be output. You could also do:

<%
  pageContext.setAttribute("total", new Integer(param1 + param2));
%>

if you want the value to be in the page scope like useBean will do.

carson
+1  A: 

<c:set var="amount" value="${param1 + param2}" scope="page" />

Adeel Ansari