[英]How to get an item from the String[] attribute in JSTL/JSP tag
In plain JSP I can get first item by EL ${form.items[0]}
, but in a JSP tag the same expression throws the following exception:在普通的 JSP 中,我可以通过 EL
${form.items[0]}
获得第一项,但在 JSP 标记中,相同的表达式会引发以下异常:
javax.el.PropertyNotFoundException: Could not find property 0 in class java.lang.String
javax.el.PropertyNotFoundException:在 class java.lang.String 中找不到属性 0
The value of ${form.items}
is [Ljava.lang.String;@315e5b60
. ${form.items}
的值为[Ljava.lang.String;@315e5b60
。
JSP tag code is: JSP 标签代码为:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="items" required="true" %>
${items[0]}
JSP code is: JSP 代码为:
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:input items="${form.items}"></t:input>
Maybe I forgot type of the attribute or something else?也许我忘记了属性的类型或其他? Why is the way to access values different in JSP and JSP tag?
为什么在 JSP 和 JSP 标签中访问值的方式不同?
You need to specify the expeded type of the custom tag attribute.您需要指定自定义标签属性的加速类型。 By default, it's
java.lang.String
, and the JSP container coerces the attribute to a string before passing it to your tag.默认情况下,它是
java.lang.String
,并且 JSP 容器将属性强制转换为字符串,然后再将其传递给您的标签。 It thus calls toString on your String array.因此,它会在您的 String 数组上调用 toString。
<%@ attribute name="items" required="true" type="java.lang.String[]" %>
or或者
<%@ attribute name="items" required="true" type="[Ljava.lang.String" %>
should do the trick.应该做的伎俩。 If neither does, using
如果两者都没有,则使用
<%@ attribute name="items" required="true" type="java.lang.Object" %>
should, but it's less clear.应该,但不太清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.