项目中用到的几个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中准备数据源list和name属性值
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属性组合的字符串。