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