JSTL,Bean和方法调用 [英] JSTL, Beans, and method calls

查看:145
本文介绍了JSTL,Bean和方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个JSP,我需要调用一个来自一个Bean的对象的方法。以前版本的页面不使用JSTL,它可以正常工作。我的新版本设置如下:

 < jsp:useBean id =pageBeanscope =requesttype =com.epicentric.page.website.PageBean/> 
< c:set var =pageDividersvalue =<%= pageBean.getPageDividers()%> />
< c:set var =numColumnsvalue =$ {pageDividers.size()}/>

变量 pageDividers 是一个列表对象。



我遇到这个问题:当我要求 pageDivider 的大小,抛出异常。我知道这是一个简单的JTSL错误 - 我做错了什么?



错误信息是:


当未指定默认命名空间时,必须使用前缀的函数大小


正确访问或调用我的 pageDividers 对象的方法

解决方案

在JSTL中使用点运算符进行属性访问,需要调用 $ {pageDividers.size} (no () getSize()

由于java.util.List提供了一个名为 size() (而不是 getSize()),您将无法使用该代码访问列表长度。






为了访问列表大小,JSTL提供 fn:length 函数,如

  $ {fn:length(pageDividers)} 

请注意,按顺序t o使用 fn 命名空间,您应该声明如下

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

此外,同样的功能可以用于任何集合类型,也可以用于Strings。 p>

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?

解决方案

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.

这篇关于JSTL,Bean和方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆