1

I just started to use JSTL for my project, but sorry to say it's really confusing to me.

I originally used Number.java

package com.mycompany
public class Number {
  private int total;
  public static int add (int x, int y) {
    return total;
}

And in showNumber.jsp i could just use

<%@page import= "com.mycompany.Number" %>

and inline use <%= Number.add(5,6) %>

How can I rewrite this part in JSTL? Is that also possible to import the class Number.java? I tried so many different things, e.g. <c:out value="${Number}.add(5,6)" />, but still cannot find a solution. Thanks.


Edited: I use @Victor's approach, and it does work. In my case, I need to reuse other's variable from spring framework, say NumberTwo.java and totalTwo as private variable inside. And added "100" to this totalTwo.

For the src where i need to use it is <spring:param name="secondNumber" value ="${NumberTwo.totalTwo}" />.

However, intutively i used (int) pageContext.getAttribute("NumberTwo.totalTwo"), it always returned me null.

The other workaround is first <c:set var="result" value="${NumberTwo.totalTwo}" /> then <% String result = (String) pageContext.getAttribute("result"); %> and then <%= Number.add(result, 100) %>

Richard
  • 159
  • 1
  • 3
  • 11

3 Answers3

1

Unfortunately it's not possible to arbitrarily call methods with JSTL, the function capabilities of JSTL are very limited: http://docs.oracle.com/javaee/5/tutorial/doc/bnalg.html . But it's still possible to use your Number class. Here the workaround:

<%@page import= "com.mycompany.Number" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    pageContext.setAttribute("addResult", Number.add(7, 8));
%>
<html>
<body>
  JSP 1.x: Result is: <c:out value="${addResult}" /><br/>
  JSP 2.x: Result is: ${addResult}
</body>
</html>

With pageContext.setAttribute() the method result is stored in the page context, and the JSTL tags can access values (attributes) stored in that context.

Note: the second output line "Result is: ${result}" works only with JSP 2 afaik.

t0r0X
  • 4,212
  • 1
  • 38
  • 34
  • Thank you so much. This does help a lot. One of my related problem is, I might need to reuse this `${addResult}` in the scriptlet. So for example, `Number.add("${addResult}, "7")`. Based on this workaround, I wonder how to use the things in JSTL back to scriptlet. Thanks. – Richard Mar 03 '12 at 02:45
  • I guess i can use `(String) pageContext.getAttribute("addResult") `to reuse this variable (maybe not a standard way). However, the new problem is, when i worked with other people, i could not use `pageContext` to retrieve their data via `getAttribute`. The result always show me "null". If this is a right approach, how could I know where to `getArribute`? – Richard Mar 03 '12 at 04:48
  • I find a way to work around: The source ==> for `foo.java` and `bar` (the private variable) in ``. If I want `${foo.bar}` and try `(String) pageContext.getAttribute("foo.bar")`, it always returns me `null`. The solution ==> first `` then `<% String result = (String) pageContext.getAttribute("result"); %>` before i reuse `result` in my scriptlet. This approach works, but just cumbersome to `` before `.getAttribute` – Richard Mar 03 '12 at 05:04
0

You can use the 'usebean' tag as in the following example:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- UseBean.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<html><body>
<jsp:directive.page import="CacheBean"/>
<jsp:useBean id="b" class="CacheBean"/>
<jsp:setProperty name="b" property="text" value="Hello world!"/>
Property from my Bean: 
<jsp:getProperty name="b" property="text"/>
<br/>
Info from my Bean: 
<jsp:expression>b.getInfo()</jsp:expression>
</body></html>
</jsp:root>

Where:

/**
 * CacheBean.java
 * Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
 */
public class CacheBean {
  private String text = "null";
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public String getInfo() {
    return "My JavaBean - Version 1.00"
  }
}

CREDIT TO: http://www.herongyang.com/jsp/usebean.html

kostasv
  • 81
  • 1
  • 3
  • Thanks, but i might still need to interact with others' EF variable like `${xxxx}`. I hope i can know how i can reuse such variable in the jsp:usebean. – Richard Mar 03 '12 at 05:43
0

Please look at EL functions in BalusC's answer at
Hidden features of JSP/Servlet
also look at "Using Custom Methods in EL" at
http://www.roseindia.net/jstl/jstl-el.shtml
look at Functions at
http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html

Community
  • 1
  • 1
rickz
  • 4,324
  • 2
  • 19
  • 30
  • This seems also to work, but i hope i can see a way to get rid of maintaining another tag library descriptor (.tld) file. – Richard Mar 03 '12 at 05:23