How do I pass a page scoped object to a custom JSP tag using Struts 2?
题
I am using Struts 2 and I need to pass a page scoped variable to a custom JSP tag.
If I use OGNL syntax %{#option}
I get the following error:
org.apache.jasper.JasperException: PWC6338: Cannot convert "%{#option}" for the attribute option of the bean com.leaseplanis.iq.model.bo.options.OptionDetail
Where as if I use EL syntax ${option}
I the object is null
.
My custom tag is located at */WEB-INF/tags/option_price_textfield.tag* in my web application:
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ tag body-content="empty"%>
<%@ attribute name="option" type="com.leaseplanis.iq.model.bo.options.OptionDetail" required="true" rtexprvalue="true"%>
<s:textfield
id="%{optionId(#option, 'price')}"
name="%{optionField(#option, 'price')}"
value="%{optionGrossPrice(#option).getString()}"
cssClass="input price"
size="8"
maxlength="11"
onchange="%{optionPriceUpdate(#option)}" />
The custom tag is used in the following JSP page:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="iq" tagdir="/WEB-INF/tags"%>
.
.
<s:iterator var="option" value="variousOptionList">
.
.
<s:if test="viewConfigService.displayPriceColumn()">
<td class="price">
<s:if test="isPriceEditable(#option)">
<iq:option_price_textfield option="%{#option}"/>
</s:if>
<s:else>
<s:property value="optionGrossPrice(#option).getString()"/>
</s:else>
<s:property default=" " escape="false" value="#option.optionCur"/>
</td>
</s:if>
.
.
</s:iterator>
解决方案
OGNL allows access to scoped attributes like option
via #attr
. Change the option
accesses to use that and you should be all set:
<s:textfield
id="%{optionId(#attr.option, 'price')}"
name="%{optionField(#attr.option, 'price')}"
value="%{optionGrossPrice(#attr.option).getString()}"
cssClass="input price"
size="8"
maxlength="11"
onchange="%{optionPriceUpdate(#attr.option)}" />
不隶属于 StackOverflow