Struts2获取表单数据详解编程语言

Struts2 提供三种数据封装的方式
1. Action 本身作为model对象,通过成员setter方法进行封装
2. 创建独立model对象,页面通过ognl表达式封装
3. 实现ModelDriven接口,对请求数据进行封装

准备jsp文件:
form.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
  </head> 
 
  <body> 
 
   <form action="${pageContext.request.contextPath}/formAction.action" method="post"> 
        用户名:<input type="text" name="username" /> <br/> 
        密码:<input type="text" name="password" /> <br/> 
        <input type="submit" /> 
    </form> 
 
  </body> 
 
</html> 

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
 
  </head> 
 
  <body> 
   Hello Struts2 
  </body> 
</html> 

struts.xml文件的action 配置

<action name="formAction" class="com.my.action.FormAction"> 
            <result name="success">/success.jsp</result> 
        </action>
  1. Action 本身作为model对象,通过成员setter方法进行封装
    Action类的编写:
package com.my.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class FormAction extends ActionSupport{ 
    //必须提供相应的setter方法 
    private String username; 
    private String password; 
 
    public void setUsername(String username) { 
        this.username = username; 
    } 
    public void setPassword(String password) { 
        this.password = password; 
    } 
 
 
    @Override 
    public String execute() throws Exception { 
 
        System.out.println( username + " ###" + password); 
 
        return SUCCESS; 
    } 
 
 
} 

运行结果:
这里写图片描述

  1. 创建独立model对象,页面通过ognl表达式封装
    修改form.jsp,通过ognl表达式,给javabean进行赋值,
    action提供 setUser()进行封装 ,通过getUser() 获得对象,并给此对象对应的属性赋值
用户名:<input type="text" name="user.username" /> <br/> 
密码:<input type="text" name="user.password" /> <br/>

FormAction.java

package com.my.action; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class FormAction extends ActionSupport{ 
 
    private User user;//必须提供getter setter方法 
 
    public User getUser() { 
        return user; 
    } 
 
    public void setUser(User user) { 
        this.user = user; 
    } 
 
    @Override 
    public String execute() throws Exception { 
 
        System.out.println(user); 
 
        return SUCCESS; 
    } 
 
} 

运行结果:
这里写图片描述

  1. 使用ModelDriven接口,对请求数据进行封装
    action 实现 ModelDriven
package com.my.action; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
 
public class FormAction extends ActionSupport implements ModelDriven<User>{ 
 
    private User user = new User(); //必须实例化,如果对象为null,表单中的数据将不能赋值 
 
    @Override 
    public User getModel() { 
        return user; 
    } 
 
    @Override 
    public String execute() throws Exception { 
 
        System.out.println(user); 
 
        return SUCCESS; 
    } 
 
} 

运行结果:
这里写图片描述

4.获取list数据
修改表单form.jsp:

    用户名 :<input type="text" name="userList[0].username" /> <br/> 
        密码 :<input type="text" name="userList[0].password" /> <br/> 
        用户名 :<input type="text" name="userList[1].username" /> <br/> 
        密码 :<input type="text" name="userList[1].password" /> <br/>

FormAction.java

package com.my.action; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class FormAction extends ActionSupport { 
 
    private List<User> userList = new ArrayList<User>(); 
 
    public List<User> getUserList() { 
        return userList; 
    } 
 
    public void setUserList(List<User> userList) { 
        this.userList = userList; 
    } 
 
    @Override 
    public String execute() throws Exception { 
 
        for (User user : userList) { 
            System.out.println(user); 
        } 
 
        return SUCCESS; 
    } 
 
 
 
} 

运行结果:
这里写图片描述

5.获取map数据
修改表单form.jsp:

用户名 :<input type="text" name="userMap['u1'].username" /> <br/> 
        密码 :<input type="text" name="userMap['u1'].password" /> <br/> 
        用户名 :<input type="text" name="userMap['u2'].username" /> <br/> 
        密码 :<input type="text" name="userMap['u2'].password" /> <br/>

FormAction.java

package com.my.action; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Map.Entry; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class FormAction extends ActionSupport { 
 
    private Map<String, User> userMap = new HashMap<String, User>(); 
 
    public Map<String, User> getUserMap() { 
        return userMap; 
    } 
 
    public void setUserMap(Map<String, User> userMap) { 
        this.userMap = userMap; 
    } 
 
    @Override 
    public String execute() throws Exception { 
        for (Entry<String, User> entry : userMap.entrySet()) { 
            System.out.println(entry.getKey() + "##" + entry.getValue()); 
        } 
 
        return SUCCESS; 
    } 
 
 
} 

运行结果:
这里写图片描述

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/12078.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论