monssef
monssef

Reputation: 1004

How to inject the value of a Struts <s:property /> tag into another tag

I want to take the value of an attribute in the action class by using Struts2 property tag <s:property value="id"/> and give it to another tag, something like this (wrong code, just to give the idea):

<t:tag id="<s:property value="id"/>"/>

How can I do this?

Note: <t: is mapped to a custom taglibrary.

Upvotes: 1

Views: 1495

Answers (2)

Keith Jonas
Keith Jonas

Reputation: 1

Try something like this:

You have a Class like this

public class YourAction
{ 
    public static final String ROLES_ALLOWED = "Admin,Customer Service";

    // ...

    @Override
    public String getRolesAllowed()
    {
        return ROLES_ALLOWED;
    }
}

Your JSP will have a property rolesAllowed that is assigned like this:

<s:set var="rolesAllowed">
    <s:property value="%{@com.package.YourAction@ROLES_ALLOWED}"/>
</s:set>

Now you can use this property in another tag using the hash # symbol

<s:if test="%{#session.user.isAuthorized(#rolesAllowed)}">

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50203

You can't nest (server-side) tags like that;

If you don't know exactly what you are doing, I'd suggest to stick with the existing taglibraries, standing on the shoulders of the giants; if instead you are inheriting it, and can't drop it, then try with JSP EL syntax:

<t:tag id="${id}"/>

(Objects in the Value Stack are made available to JSP EL by the Struts2 request wrapper)

Upvotes: 3

Related Questions