jsp页面java代码<%%>中获取struts2值栈中的值

实现效果:User中有一个字段updateTime存的是毫秒值,在jsp页面上要转换为
2016-03-31 12:00格式
在Action中
private List<User> users;

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

User类
public class User {
	private Integer id;
	private String name;
	//注册时间utc毫秒数
	private Long updateTime;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Long updateTime) {
		this.updateTime = updateTime;
	}
}

在jsp中
<tr class="first">
  <td class="title">id</td>
  <td class="title">姓名</td>
  <td class="title">注册时间</td>
</tr>
<s:iterator value="users">
<tr>
  <td class="cb">
    <s:property value="id"/>
  </td>
  <td class="cb">
    <s:property value="name"/>
  </td>
  <td class="cb">
    <s:set value="updateTime" var="time"/>
    <%
      //时间毫秒数
      Long timeMillis = (Long) request.getAttribute("time");
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      Date date =new Date(timeMillis);
      String dateTime = sdf.format(date);
      request.setAttribute("dateTime", dateTime);
    %>
    <s:property value="#request.dateTime"/>
  </td>
</tr>
</s:iterator>

已经知道更好的解决办法:在实体类中创建一个getter然后在getter中做想要的转换,
在页面上只需使用这个getter对应的bean名称即可。

猜你喜欢

转载自zzzhang.iteye.com/blog/2288159
今日推荐