struts2 二

一、访问servletAPI方式

Action类中

        ActionContext context = ActionContext.getContext();
        String parameter = (String)context.get("cust_name"); //三个域中查找
        Map<String, Object> application = context.getApplication();  //application域
        Map<String, Object> session = context.getSession();//session域
        Map<String, Object> parameters = context.getParameters();//request域

二、结果跳转方式

type属性说明
dispatcher转发
redirect重定向
chain跳转到Action后仍接受上一页面的值,比如 request
redirectAction重定向到Action丢失页面传递的信息

三、数据的封装

1.属性驱动

页面提供表达式方式
Action类中

    private Customer customer;
    //必须实现对象的get方法
    public Customer getCustomer() {
        return customer;
    }

jsp页面中

<form action="${pageContext.request.contextPath }/customer_add">
<input type="text" name="customer.name">

2.模型驱动

jsp和以前的一样 不用改动

Action类的书写
实现ModelDriven 实现接口 构建对象

public class DemoAction extends ActionSupport implements ModelDriven<Customer>{
    //手动构建对象
    private Customer customer=new Customer();
    @Override
    public Customer getModel() {
        // TODO Auto-generated method stub
        return customer;
    }
}

三、封装到集合

1.封装到List集合

编写页面

<form action="${pageContext.request.contextPath }/customer_add">
<input type="text" name="list[0].name">
<input type="text" name="list[0].age">
<input type="text" name="list[0].birthday">
<input type="text" name="list[1].name">
<input type="text" name="list[1].age">
<input type="text" name="list[1].birthday">
<input type="submit" value="提交">
</form>

Action类的编写

public class DemoAction extends ActionSupport {
    private List<Customer> list;

    public List<Customer> getList() {
        return list;
    }

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

利用下标来区分….

2.封装到Map集合

页面编写:

<form action="${pageContext.request.contextPath }/customer_add">
<input type="text" name="map['one'].name">
<input type="text" name="map['one'].age">
<input type="text" name="map['one'].birthday">
<input type="text" name="map['two'].name">
<input type="text" name="map['two'].age">
<input type="text" name="map['two'].birthday">
<input type="submit" value="提交">
</form>

编写Action:

public class DemoAction extends ActionSupport {
    private Map<String,Customer> map;
    public Map<String, Customer> getMap() {
        return map;
    }
    public void setMap(Map<String, Customer> map) {
        this.map = map;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值