Struts 2 encapsulates form data into list and map sets

1. Encapsulate form data into Map collection
1. Create MapAction class

import cn.entity.User;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

public class MapAction extends ActionSupport {
    private Map<String, User> map;

    public Map<String, User> getMap() {
        return map;

    public void setMap(Map<String, User> map) { = map;

    public String execute(){

        return NONE;

2. Create a map. The JSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <form action="${pageContext.request.contextPath}/map.action" method="post">
        username:<input name="map[0].username" type="text"><br>
        password:<input name="map[0].password" type="password"><br>
        username:<input name="map[1].username" type="text"><br>
        password:<input name="map[1].password" type="password"><br>
        <input type="submit" name="Submit" value="提交">

Add the following statement

to the struts.xml file

    <package name="myPackage" extends="struts-default" namespace="/">
<!--        <action name="data3" class=""></action>-->
<!--        <action name="list" class=""></action>-->
        <action name="map" class=""></action>

** flow: ** browser in parsing the form data will find the getMap method in the MapAction entity class to get the Map class object, and then in the User entity class to find setUsername() and other set methods to encapsulate the data in the entity class object

To sum up: encapsulate the data List set contract Map the same

Read More: