简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM