We are using Struts Tiles 1.x in a legacy web application. In a.jsp
, we do:
<tiles:put name="bean" beanName="change"></tiles:put>
Later, b.jsp
references a.jsp
. In b.jsp
, we currently have a scriptlet, in which we do:
<% DomainObject domObject = (DomainObject) TilesHelper.getAttribute("bean", pageContext); %>
This is what getAttribute(String,PageContext)
looks like:
public class TilesHelper {
public static Object getAttribute(String name, PageContext pageContext) throws JspException {
ComponentContext compContext = (ComponentContext) pageContext.getAttribute(
ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
if (compContext == null) {
throw new JspException("Error - tag.getAttribute : component context is not defined. Check tag syntax");
}
return compContext.getAttribute(name);
}
This works, but I don't like scriptlets. Scriptlets are ugly. I don't want any scriptlets in this JSP. I want to use a tag to access the object passed in request scope using. I've tried
<bean:define id="domObject" name="bean" scope="request"/>
This fails with error can't find bean bean in scope request.
How do I access the object passed in the <tiles:put>
call in a tag like <bean:define>
or <jsp:useBean>
?
Thanks
In case this helps anyone later: after much eye-straining forum browsing and rereads of the documentation, I was able to decipher that the correct tag to use is
<tiles:useAttribute id="beanId" name="beanNameHere" classname="theBeanClass" scope="request"/>
.
This declares a Java variable in scope request which can then be accessed like any other bean, in my case using EL, for example:
<c:if test="${beanId.class.simpleName == 'someInterestingClass'}">
<c:out value="${beanId.someInterestingProperty}"/>
</c:if>