Struts2的三种数据封装方法

第一种方法:属性封装(action提供属性对应的set方法)


这种方式需要手动封装数据,但是如果属性很多,提供很多的set方法,导致Action类易读性变差。

页面代码:
< body >
< h1 > Struts2 的数据封装 </ h1 >
< h3 > 数据封装的方式一:提供属性的 set 方法的方式 </ h3 >
< form action = " ${ pageContext.request.contextPath } /employee1Action.action" method = "post" >
    姓名 : < input type = "text" name = "name" />< br />
    年龄 : < input type = "text" name = "age" />< br />
    性别 : < input type = "text" name = "sex" />< br />
    工资 : < input type = "text" name = "salary" />< br />
    < input type = "submit" value = " 提交 " />
</ form >
</body>

action代码:
/**
  * 数据封装的方式一:提供 set 方法的方式
  */
public class Employee1Action extends ActionSupport{
    private String name ;
    private Integer age ;
    private String sex ;
    private Double salary ;
    // 传递值(并完成数据类型的转换)
    public void setName(String name ) {
        this . name = name ;
    }
    public void setAge(Integer age ) {
        this . age = age ;
    }
    public void setSex(String sex ) {
        this . sex = sex ;
    }
    public void setSalary(Double salary ) {
        this . salary = salary ;
    }
    @Override
    public String execute() throws Exception {
        System. out .println( " 员工姓名 :" + name );
        System. out .println( " 员工年龄 :" + age );
        System. out .println( " 员工性别 :" + sex );
        System. out .println( " 员工工资 :" + salary );
        // 手动封装数据:
        Employee employee = new Employee();
        employee .setName( name );
        employee .setAge( age );
        employee .setSex( sex );
        employee .setSalary( salary );
        // 保存
        return NONE ;
    }
}


第二种方法:属性封装(页面表达式ONGL封装)


页面代码:
< h3 > 数据封装的方式二:页面提供 OGNL 表达式的写法 </ h3 >
< form action = " ${ pageContext.request.contextPath } /employee2Action.action" method = "post" >
    姓名 : < input type = "text" name = "employee.name" />< br />
    年龄 : < input type = "text" name = "employee.age" />< br />
    性别 : < input type = "text" name = "employee.sex" />< br />
    工资 : < input type = "text" name = "employee.salary" />< br />
    < input type = "submit" value = " 提交 " />
</ form >

action代码:
/**
  * 数据封装的方式二 : 页面提供表达式的方式
  */
public class Employee2Action extends ActionSupport{
    // 提供成员的属性 employee ,必须提供属性的 get set 方法
    private Employee employee ;
   
    public Employee getEmployee() {
        return employee ;
    }
    public void setEmployee(Employee employee ) {
        this . employee = employee ;
    }
 
    @Override
    public String execute() throws Exception {
        System. out .println( employee );
        return NONE ;
    }
}

第三种方法:模型封装(action实现ModelDriven接口)  推荐使用


页面代码:
< h3 > 数据封装的方式三:采用模型驱动的方式 </ h3 >
< form action = " ${ pageContext.request.contextPath } /employee3Action.action" method = "post" >
    姓名 : < input type = "text" name = "name" />< br />
    年龄 : < input type = "text" name = "age" />< br />
    性别 : < input type = "text" name = "sex" />< br />
    工资 : < input type = "text" name = "salary" />< br />
    生日 : < input type = "text" name = "birthday" />< br />
    < input type = "submit" value = " 提交 " />
</ form >

action代码:
/**
  * 数据封装的方式三:采用模型驱动的方式
  */
public class Employee3Action extends ActionSupport implements ModelDriven<Employee>{
    // 模型驱动使用的对象:必须手动构建对象的实例。
    private Employee employee = new Employee();
    @Override
    public Employee getModel() {
        return employee ;
    }   
    @Override
    public String execute() throws Exception {
        System. out .println( employee );
        return NONE ;
    }
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值