How to translate JSP servlet which calls an object

2019-08-13 10:06发布

问题:

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) %>

回答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.



回答2:

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



回答3:

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



标签: java jsp jstl