Trick
Trick

Reputation: 3859

How to access dynamic attributes in JSP (Struts2)

In Action I re set sent parameters.

    for(Enumeration<String> enumParams = request.getParameterNames(); enumParams.hasMoreElements();) {
        String name = enumParams.nextElement();
        String value = request.getParameter(name);
        request.setAttribute(name, value);
    }

On the JSP I would like to access the request attribute values.

<s:iterator value="variables">
    <input type="text" 
        id="<s:property value="sign"/>"
        name="<s:property value="sign"/>"  
        value="<s:property value="%{#attr['sign']}"/>" />
</s:iterator>

(variables are objects with field sign, etc.)

Currently I get with <s:property value="%{#attr['sign']}"/> only the sign of the variable, not the value. It does not evaulate 'sign'.

Generated HTML:

<input id="A" name="A" value="A" type="text">

So if hard-code sign like this <s:property value="%{#attr['A']}"/>, I get the correct value...

Any clues? Please.

Upvotes: 0

Views: 10124

Answers (1)

Trick
Trick

Reputation: 3859

With a little trick I got it:

<s:iterator value="variables">
    <s:set var="mySign" value="%{sign}" name="mySign" scope="request"></s:set>
    <jsp:useBean id="mySign" class="java.lang.String" scope="request" ></jsp:useBean>           
    <input type="text" 
        id="<s:property value="sign"/>"
        name="<s:property value="sign"/>"  
        value="<%=request.getAttribute(mySign)%>">
</s:iterator>

Upvotes: 3

Related Questions