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) {
        this.map = map;
    }

    @Override
    public String execute(){

        System.out.println(map);
        return NONE;
    }
}

2. Create a map. The JSP

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <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>
        <br><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="提交">
    </form>
</body>
</html>

Add the following statement

to the struts.xml file

<struts>
    <package name="myPackage" extends="struts-default" namespace="/">
<!--        <action name="data3" class="cn.data.DateDemo2Action"></action>-->
<!--        <action name="list" class="cn.data.ListAction"></action>-->
        <action name="map" class="cn.data.MapAction"></action>
    </package>
</struts>

** 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: