struts2标签使用

项目中用到的几个strut2标签
1,s:checkbox
该标签对应html中标签,当时在使用struts2时,使用struts标签是非常方便的。
若有需求:
在action中,将checkbox的数据源放入值栈

List<Device>smallDevices = new ArrayList<>();
.........
ActionContext.getContext().put("smallDevices", smallDevices);


其中device bean大致为:省去set/get方法
public class Device {
    private Integer id;
    private String name;
    private String addr;
    private String deviceNo;
    private Integer areaId;
}

在jsp页面使用如下struts标签:

< s:iterator value=”smallDevices”>
< s:checkbox name=”ids” value=”id” fieldValue=”%{id}”/>
< /s:iterator>


value属性等价于html标签中的checked属性
    若device存在于迭代的数据源list中,则会选中该device对象。
fieldValue属性等价于html标签中的value属性
    表示若选中该项,则回传给action的device的id属性。

若选中该checkbox,在action使用数组接收:
private Integer[] ids ;
public Integer[] getIds() {
    return ids;
}
public void setIds(Integer[] ids) {
    this.ids = ids;
}

ids数组中存放的就是页面传回的fieldValue属性,即被选中device的id值。


2,< s:select>标签
下拉选择标签也是使用的比较多的标签之一。

在action中准备好下拉数据源:

ActionContext.getContext().put("deviceTypes", deviceTypes);
在jsp页面中使用< s:select>标签下拉选择:


< s:select name="deviceTypeID" list="#deviceTypes" listKey="id" listValue="name+'('+manufacture+')' "headerKey="" headerValue="===请选择===" /> 


其中:
list属性为action中放入值栈的数据源。
listkey为选中之后回传action的对象的属性,这里为选中对象的id值。
listvalue为需要显示下拉的对象的属性,这里为对象的name+manufacture组成的字符串。
headervalue为默认显示的值。
name属性为传回action的接收变量名。

**若要回显下拉框**
action中准备数据源listname属性值
private Integer deviceTypeID;
public Integer getDeviceTypeID() {
    return deviceTypeID;
}

public void setDeviceTypeID(Integer deviceTypeID) {
    this.deviceTypeID = deviceTypeID;
}

………

ActionContext.getContext().put("deviceTypes", deviceTypes);
deviceTypeID = device.getDeviceType().getId();

在jsp页面中会自动回显选中deviceTypeID对应的对象的name+manufacture属性组合的字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值