Action获取数据——对象方式、模型驱动、集合方式

1、以对象方式获取表单数据

(1)创建一个User的JavaBean:

public class User {
    private String password;
    private Integer userage;
    private String username;
    public Integer getUserage() {
        return userage;
    }

    public void setUserage(Integer userage) {
        this.userage = userage;
    }

    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;
    }
    @Override
    public String toString() {
        return "User{" +
                "password='" + password + '\'' +
                ", userage=" + userage +
                ", username='" + username + '\'' +
                '}';
    }
}

(2)创建一个表单用于收集用户的信息:

    
            
用户名:
密码:
年龄:

(3)struts.xml配置文件:


    <package name="hello" namespace="/h" extends="struts-default">
        class="pers.zhb.hello.Action" method="execute">
            /form.jsp
        
    package>

(4)书写Action类,获取表单提交的数据:

public class Action extends ActionSupport{
    private User user;
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
        public String execute(){
            System.out.println("用户的信息为:"+user.toString());
            System.out.println("用户名为:"+user.getUsername());
            System.out.println("密码为:"+user.getPassword());
            System.out.println("年龄为:"+user.getUserage());
            return "success";
        }
}

(5)测试结果:

表单数据:

Action获取数据——对象方式、模型驱动、集合方式_第1张图片

 

Action获取到的数据:

Action获取数据——对象方式、模型驱动、集合方式_第2张图片

 

 

2、模型驱动的方式提交数据

(1)创建User的JavaBean

(2)创建表单:

   
            
用户名:
密码:
年龄:

与以对象的方式提交相比name属性有变化。

(3)配置struts.xml配置文件

(4)书写Action类:

public class Action extends ActionSupport implements ModelDriven {
    private User user=new User();
    public String execute(){
        System.out.println("用户的信息为:"+user.toString());
        System.out.println("用户的用户名为:"+user.getUsername());
        System.out.println("用户的密码为:"+user.getPassword());
        System.out.println("用户的年龄为:"+user.getUserage());
        return null;
    }
    @Override
    public User getModel() {
        return user;
    }
}

这里需要实现一个接口。

(5)测试:

向表单输入数据:

Action获取数据——对象方式、模型驱动、集合方式_第3张图片

 

 控制台打印Action获取的数据:

Action获取数据——对象方式、模型驱动、集合方式_第4张图片

 

 缺点:模型驱动的方式每一个Action只能获取到一个对象。

 

3、List集合方式

(1)表单:



(2)Action类:

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

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

    public String execute(){
        System.out.println(list);
        return null;
    }
}

(3)测试:

 

 

 

 可以指定数据在list集合中的位置。

 

4、Map集合方式

(1)表单:

   

表单必须指定提交的键,该键以提交的数据成为键值对。

(2)Action:

public class Action extends ActionSupport{
    private Map map;
    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public String execute(){
        System.out.println(map);
        return null;
    }
}

(3)测试:

 

 

 

你可能感兴趣的:(Action获取数据——对象方式、模型驱动、集合方式)