JSTL, Beans, and method calls

I'm working on a JSP where I need to call methods on object that come from a Bean. The previous version of the page does not use JSTL and it works properly. My new version has a set up like this:

<jsp:useBean id="pageBean" scope="request" type="com.epicentric.page.website.PageBean" />
<c:set var="pageDividers" value="<%= pageBean.getPageDividers() %>" />
<c:set var="numColumns" value="${pageDividers.size()}" />

The variable pageDividers is a List object.

I'm encountering this issue: when I ask for pageDivider's size, an exception is thrown. I know this is a simple JTSL error -- what am I doing wrong?

The error message is:

The function size must be used with a prefix when a default namespace is not specified

How do I correctly access or call the methods of my pageDividers object?


Asked by: Julian993 | Posted: 23-01-2022






Answer 1

When using the dot operator for property access in JSTL, ${pageDividers.size} (no () needed) results in a call to a method named getSize().
Since java.util.List offers a method called size() (rather than getSize()) you won't be able to access the list length by using that code.


In order to access to a list size, JSTL offers the fn:length function, used like

${fn:length(pageDividers)}

Note that in order to use the fn namespace, you should declare it as follows

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

In addition, the same function can be used with any collection type, and with Strings too.

Answered by: Tess782 | Posted: 24-02-2022



Answer 2

To access the property of a bean using EL you simply name the property (not invoke the method). So lets say you have a method called getSize() in the bean then

${pageDividers.size}

Notice no ().

EDIT:Sorry...made an error in the original post.

Answered by: Chelsea401 | Posted: 24-02-2022



Similar questions

javabeans - Getting Java beans that were set in a try method

I am setting a java bean from within a try method. A text file is being read and the text that is read is used to set the java bean. public class mainDisplay extends JPanel{ private imageDisplay id; public mainDisplay() { String path; while (1==1) { try { FileInputStream roadMap = new FileInputStream("C:\\Users\\Public\\Desktop\\write.txt"); //path to the text fi...


When should I use the java 5 method cast of Class?

Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like to know if casting this way has any advantages over trTuDocPackTypdBd.update((TrTuDocPackTypeDto)packDto); I've asked the developer responsible and he said he used it because it was new (which doesn't seem like a partic...


java - How can I call a method in an object from outside the JVM?

I have a really simple Java class that effectively decorates a Map with input validation, with the obvious void set() and String get() methods. I'd like to be able to effectively call those methods and handle return values and exceptions from outside the JVM, but still on the same machine Update: the caller I have in mind is not another JVM; thanks @Dave Ray My implementation considerations...


java - How to show if a method may return null

After posting this question and reading that one I realized that it is very important to know if a metho...


swing - Java Paint Method Doesn't Paint?

I'm working on a simple little swing component, and I'm tearing out my hair trying to figure out why my paint method isn't working. The idea behind this component is that it's a small JPanel with a label. The background (behind the label) is supposed to be white, with a colored rectangle on the left-hand side indicating the ratio of two measurements: "actual" and "expected". If you had a bunch of these comp...


class - Why can't java find my method?

I am trying to wrap my mind around something in java. When I pass an object to another class' method, can I not just call any methods inherent to that object class? What is the reason code such as the example below does not compile? Thank you, class a { public static void myMethod(Object myObj) { myObj.testing(); } } class b { public void testing() { System.out.println ("TEST...


java - Can I mock a super class method call?

Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymock or jmock (and I think it is not possible). There is a (relative) clean solution, to create a delegate with the super class method logic and then set expectations on it, but I don't know why and when use that solution ¿any ideas/examples?...


Java Main Method, Good Coding Style

Closed. This question is opinion-based. It is not c...


print method in java

I want to ask you about the print vector array , the following one: Vector[] routingTable = new Vector[connectivity.length]; I tried this method , but it doesn't work with me and it gives me protocol.Route@c17164 when I printed in the main, here is the code, so can you tell me why it doesn't print the correct value ? public String printRT(int hop) { String ...


How can I split up paint swing method in java?

I'm developing a fair sized hospital simulation game in java. Right now, my pain method is starting to look a little big, and I need a way to split it up into different sections... I have an idea, but I'm not sure if this is the best way. It starts by painting the grass, then the hospital building, then any buildings, then people, then any building previews when building. The grass and hospital building will not change, so...


c# - Method name from line number

Given a line number of a particular class source code (Java/C#) - is there an easy way to get the name of the method it falls within? (If it falls within one) (Presumably using an Abstract Syntax Tree) (This would be useful in limiting the output of checkstyle to just the method touched). I'm assuming you'd have to use an Abstract Syntax Tree to do Line#->MethodName.






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top