Snowy Coder Girl
Snowy Coder Girl

Reputation: 5518

Struts Map References in JSP

My question is more complex than below, but I've tried to simplify things out.

I have something like the following for my form:

public class MyForm extends ActionForm {
    private Map<Long, String> myValues;

    public MyForm() {
        setMyValues(new HashMap<Long, String>());
    }

    public Map<Long, String> getMyValues() {
        return myValues;
    }

    public void setMyValues(Map<Long, String> myValues) {
        this.myValues = myValues;
    }

    public Object getMyValue(String key) {
        return getMyValues().get(key);
    }

    public void setMyValue(String key, Object value) {
        getMyValues().put(Long.valueOf(key), value.toString());
    }
}

If I try this in my JSP page for the form, all of the lines are value except the last one:

<c:forEach items="${MyForm.myValues}" var="mapEntry">
    <c:out value="${mapEntry.value}" />
    <c:out value="${mapEntry.key}" />
    <c:out value="${MyForm.myValue(mapEntry.key)}" />
</c:forEach>

The last line causes the page to break with "The function myValue must be used with a prefix when a default namespace is not specified". Does anyone know the correct way to access the map value for a specific key? As I said, it's more complex than the above. I need something similar to that last line to work.

UPDATE

Note that the following does work, where # is any integer:

<textarea name="myValue(#)"><textarea>

Upvotes: 0

Views: 1279

Answers (1)

Dave Newton
Dave Newton

Reputation: 160211

Immediate value:

<c:out value="${MyForm.myValues[1]}" />

Key from form value:

<c:out value="${MyForm.myValues[MyForm.anId]}" />

Key from request attribute:

<c:out value="${MyForm.myValues[wat]}"/>

Why use <c:out>? We have JSP EL.

${MyForm.myValues[wat]}

Upvotes: 2

Related Questions