Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 670
Zack The Human Avatar asked Oct 22 '08 20:10

Zack The Human


People also ask

How do you call a method in JSTL?

Simply create an object of the class using <jsp:useBean> and call the method using JavaServer Pages Standard Tag Library or Expression Language that is more easy to use and less error prone. Read more about Implicit Objects that might help you.

Can I call method from JSP?

The JSP 2.0 EL lets you call a Java class's public static method using the following syntax: ${prefix:methodName(param1, param2, ...)}

What is the difference between JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

What are JSTL functions?

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionality common to many JSP applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.


2 Answers

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.

like image 67
abahgat Avatar answered Oct 21 '22 08:10

abahgat


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.

like image 41
Vincent Ramdhanie Avatar answered Oct 21 '22 07:10

Vincent Ramdhanie