struts2获取值封装到List和Map

一、封装数据到List

1、实现步骤
  • 在action声明List
  • 生成List变量的set和get方法
  • 在表单输入项写表达式
2、实现代码

Userbean.java

package work.zhangdoudou.Bean;

public class User {
    private String username;
    private String password;
    private String type;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }   
}

ListAction.java

package work.zhangdoudou.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;

public class ListAction extends ActionSupport{
    private List list;
    
    
    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(list);
        return NONE;
    }
}

struts.xml





    
    
    
        
            /success.jsp
           
    

web.xml



  struts2_ListAndMap
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
    
    
        
    
    
    
  
  
  
    

Login

username:

password:

type:


username:

password:

type:

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'success.jsp' starting page
    
    
    
        
    
    
    

  
  
  
    
${list[0].username } ${list[0].password } ${list[0].type }
${list[1].username } ${list[1].password } ${list[1].type }
3、运行效果
image.png
image.png
image.png

一、封装数据到Map

1、实现步骤
  • 在action声明Map
  • 生成Map变量的set和get方法
  • 在表单输入项写表达式
2、实现代码

Userbean.java

package work.zhangdoudou.Bean;

public class User {
    private String username;
    private String password;
    private String type;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }   
}

MapAction.java

package work.zhangdoudou.action;

import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Bean.User;

public class MapAction extends ActionSupport{
    private Map map;
    
    public Map getMap() {
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    
    @Override
    public String execute() throws Exception {
        System.out.println(map);
        return NONE;
    }
}

struts.xml





    
    
    
        
            /success.jsp
        
    

web.xml



  struts2_ListAndMap
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
    
    
        
    
    
    
  
  
  
    

Login

username:

password:

type:


username:

password:

type:

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'success.jsp' starting page
    
    
    
        
    
    
    

  
  
  
    
${map['one'].username } ${map['one'].password } ${map['one'].type }
${map['two'].username } ${map['two'].password } ${map['two'].type }
三、运行效果
image.png

image.png
image.png

你可能感兴趣的:(struts2获取值封装到List和Map)