struts2怎样遍历list显示在前台页面
action是这样
private List<Person> list;
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
req.setAttribute("list", list);
if(list==null)
return "error";
else
return "success";
前台jsp
<s:iterator id="stat" value="list" status="st">
<s:property value="username[0]"/>
<s:property value="password[#st.index][0]"/>
</s:iterator>
该怎么改呢 Struts 遍历 Iterator JSP
[解决办法]
你的代码有点看不懂啊,req?request? list怎么来的。是request传值的?
我给下我的
public class ShowMissionAction {
private List<ExpirementMission> missions=new ArrayList<ExpirementMission>();
public List<ExpirementMission> getMissions() {
return missions;
}
public void setMissions(List<ExpirementMission> missions) {
this.missions = missions;
}
public String showDistributionMission(){
System.out.println("-- show distribution missions now --");
Map<String, Object> session = ActionContext.getContext().getSession();
analysist =(Employee) session.get("analysist");
session.put("name", analysist.getName());
missions=analysistMissionService.showDistributionMissions(analysist);
System.out.println(analysist.getName()+","+missions);
session.put("missions_state", "distribution");
if(missions.isEmpty()){
session.put("haveMission", "NO");
}else{
session.put("haveMission", "YES");
}
return "SUCCESS";
}
}
jsp 代码:
<s:iterator value="missions" status="i">
<tr>
<td><s:property value="batchNumber" /></td>
<td><s:property value="sort" /></td>
<td><s:property value="serialNumber" /></td>
<td><s:property value="batchNumber" /></td>
<td><s:property value="expriement" /></td>
</tr>
</s:iterator>
附:
<s:iterator id="stat" value="list" status="st"> 中id是web,html标签元素
至于req.setAttribute("list", list); 多余了,在struts2中,只需有list bean 且有setter/getter方法,struts标签能自动获取,因为值放在了ActionContext中,struts2特有的一块内存区域
[解决办法]
附加 missions的bean:
package com.until.temporay;
import java.sql.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.until.support.Employee;
@Entity
public class ExpirementMission {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String expriement;
private String state;
private String sort;
private String serialNumber;
private String batchNumber;
private Date Sdate;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "employee_id")
private Employee mtemployee;
@ManyToOne(cascade = CascadeType.ALL)
private ExpirementEvent expirementEvent;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExpriement() {
return expriement;
}
public void setExpriement(String expriement) {
this.expriement = expriement;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getBatchNumber() {
return batchNumber;
}
public void setBatchNumber(String batchNumber) {
this.batchNumber = batchNumber;
}
public Date getSdate() {
return Sdate;
}
public void setSdate(Date sdate) {
Sdate = sdate;
}
public ExpirementEvent getExpirementEvent() {
return expirementEvent;
}
public void setExpirementEvent(ExpirementEvent expirementEvent) {
this.expirementEvent = expirementEvent;
}
public Employee getMtemployee() {
return mtemployee;
}
public void setMtemployee(Employee mtemployee) {
this.mtemployee = mtemployee;
}
}
@XXxXX什么的别管。那个是我做了hibernate的@注释。。楼主可以忽略,具体看setter/getter方法
[解决办法]