package com.hhit.tempmodel;
public class TesterTempModel {
private String computernum;
private String photo;
private String name;
private String state;
private String stunum;
public TesterTempModel() {
super();
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getComputernum() {
return computernum;
}
public void setComputernum(String computernum) {
this.computernum = computernum;
}
public String getStunum() {
return stunum;
}
public void setStunum(String stunum) {
this.stunum = stunum;
}
}
在Action中定义变量private List<TesterTempModel[]> testers,并且添加get,set方法。
在jsp中:
<s:iterator value="testers" var="val">
<tr><!-- onmouseover="SetNewColor(this);" onmouseout="SetOldColor(this);" -->
<s:iterator value="val" var="v">
<td align="center">${v.computernum}</td>
<td align="center" >
<s:if test="#v.photo!=''">
<img id="img_photo" alt="头像" src="studentImgs/${v.photo}" width="84px" height="84px" onclick="" />
</s:if>
<s:else>
<img id="img_photo" alt="未上传头像" src="${pageContext.request.contextPath}/style/images/default.gif" width="84px" height="84px" onclick="" />
</s:else>
</td>
<td align="center" >${v.name }</td>
<s:if test="#v.state=='未考'">
<td align="center" id="${v.stunum }" style="color: red;">未考</td>
</s:if>
<s:elseif test="#v.state=='在考'">
<td align="center" id="${v.stunum }" style="color: yellow;">在考</td>
</s:elseif>
<s:else>
<td align="center" id="${v.stunum }" style="color: green;">交卷</td>
</s:else>
<td align="center" ></td>
</s:iterator>1、使用<s:iterator>标签遍历List<Arry[]>,二维数组与List<List<>>与其相同
<s:iterator value="testers" var="val">
<tr><!-- onmouseover="SetNewColor(this);" onmouseout="SetOldColor(this);" -->
<s:iterator value="val" var="v">
<td align="center">${v.computernum}</td>
其中testers是后台action中需要传到jsp的变量,其类型为List<TesterTempModel[]>,首先使用第一个<s:iterator>进行第一层遍历,其中的var为TesterTempModel[;然后再用<s:iterator>遍历,其value=var。这样可以实现2层遍历,依此类推,3层、4层.......
2、使用<s:if><s:elseif>进行条件判断
<s:if test="#v.photo!=''">
当进行条件判断是,需要用#,若使用${}则会出现错误。也还需要注意<s:if><s:elseif><else>标签之间是没有嵌套的,使用逻辑与java相同。
3、取值时,如${v.computernum}与test="#v.photo!=''",属性的首字母必须小写。
本文介绍如何在Struts框架中使用List传递数据至JSP页面,并展示如何通过S-Taglib标签进行数据遍历及条件判断。具体涵盖了如何定义模型类、设置Action中的List传递、以及JSP页面上数据的展示方式。
959

被折叠的 条评论
为什么被折叠?



