Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

java - How to pass a Map<ObjectA, List<ObjectB>> to action in Struts 2

I have a event object, inside there is a Map<ObjectA, List<ObjectB>>, the ObjectA is the label, and the list<ObjectB> are table rows. With following code, I can display the tables correctly, but when I submit the form to Action class, the map is null inside the event.

JSP CODE:

<s:iterator value="event.planMap" var="map" >
    <h4>Plan Type: <s:property value='key' /></h4>
    <table id="plan">
    <s:iterator value="value" status="stat" var="detail" >
        <tr>
            <td><input type="text" id="name" name="event.planMap['%{#map.key}'][%{#stat.index}].name" value="<s:property value='name'/>"/></td>
            <td><input type="text" id="text" name="event.planMap['%{#map.key}'][%{#stat.index}].text" value="<s:property value='text'/>"/></td>
            <td><input type="text" id="contact" name="event.planMap['%{#map.key}'][%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
        </tr>
        </s:iterator>
    </table>
</s:iterator>

@Andrea & Roman, So I modified the code. displaying the table is correct, but I got error and it went to Result input. If I remove the planMap, the action goes to success. So at least I know the error are the planMap. The modified code is:

The Event definition:

public Event {
    private Map<Object_A, Object_B> planMap;
    public Map<Object_A, Object_B> getPlanMap {
           return this.planMap;
    }

    public void setPlanMap(Map<Object_A, Object_B> planMap) {
           this.planMap = planMap;
    }
}

The Object_B definition:

public Object_B {
    private List<Object_C> details;
    
    public List<Object_C> getDetials() {
           return this.details;
    }
    public void setDetails(List<Object_C> details) {
           this.details = details;
    }
}

The JSP code is:

<s:iterator value="event.planMap" status="mStat"  >
    <h4>Plan Type: <s:property value='key' /></h4>
    <table id="plan">
    <s:iterator value="value.details" status="stat">
    <tr>
        <td><input type="text" id="name" name="event.planMap['% {#mStat.index}'].details[%{#stat.index}].name" value="<s:property value='name'/>"/></td>
        <td><input type="text" id="text" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].text" value="<s:property value='text'/>"/></td>
        <td><input type="text" id="contact" name="event.planMap['%{#mStat.index}'].details[%{#stat.index}].contact" value="<s:property value='contact'/>"/></td>
    </tr>
    </s:iterator>
</table>
</s:iterator>
      
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Struts can access indexed properties of the action bean, such as List, Map, etc., but it's required that the element class should be a Java Bean like Map<ObjectA, ObjectC>, where ObjectC wraps List<ObjectB>.

Then use Struts type conversion to populate the indexed property like in this answer How to get updated list values from JSP in my action.

Indexing a collection by a property of that collection

It is also possible to obtain a unique element of a collection by passing the value of a given property of that element. By default, the property of the element of the collection is determined in Class-conversion.properties using KeyProperty_xxx=yyy, where xxx is the property of the bean Class that returns the collection and yyy is the property of the collection element that we want to index on.

For an example, see the following two classes:

MyAction.java

/**
 * @return a Collection of Foo objects
 */
public Collection getFooCollection()
{
    return foo;
}

Foo.java

/**
 * @return a unique identifier
 */
public Long getId()
{
    return id;
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...