Bean:
package com.wenj.beans;
import java.util.Date;
public class Msg {
public static final int MSG_STAUS_IS_NEW = 0;
public static final int MSG_STAUS_HAVE_READ = 1;
private Integer id;
private String content;
private Integer sender;
private Integer getter;
private Date date;
private Integer status; //0 for new, 1 for hava read
public Msg(){ }
public Integer getId() {
return id;
}
@SuppressWarnings(value="all")
private void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getSender() {
return sender;
}
public void setSender(Integer sender) {
this.sender = sender;
}
public Integer getGetter() {
return getter;
}
public void setGetter(Integer getter) {
this.getter = getter;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
Action:
package com.wenj.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.wenj.beans.Msg;
import com.wenj.beans.User;
import com.wenj.dao.factory.MsgDAOFactory;
import com.wenj.dao.factory.UserDAOFactory;
public class NewMsgAction extends ActionSupport implements SessionAware{
private Map<String, Object> session;
@Override
public String execute() throws Exception {
String username = (String)session.get("username");
if(null != username && !"".equals(username)){
User user = UserDAOFactory.createUserDAO().findUserByUsername(username);
if(null != user){
List<Msg> msgs = MsgDAOFactory.createMsgDAO().findMsgsByStatus(user, Msg.MSG_STAUS_IS_NEW);
Map<Msg, String> map = new HashMap<Msg, String>();
if(null != msgs){
for(Msg msg : msgs){
User sender = UserDAOFactory.createUserDAO().findUserById(msg.getSender());
map.put(msg, sender.getName());
}
}
session.put("map", map);
return SUCCESS;
}
}
return ERROR;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
Action配置:
<action name="*_*" class="com.wenj.action.{1}{2}Action">
<result type="redirect">/{1}_{2}.jsp</result>
<result name="error">/include.jsp</result>
</action>
用户交互界面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="include.jsp" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>MessageBox Index</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="resource/page.css">
<script src="resource/jquery.js"></script>
</head>
<body>
<div id="div0">
<div>
<h2>欢迎你, <a href="User_Info.action"><s:property value="#session.username"/></a></h2>
</div>
<div id="div1">
<li><a href="All_Msg.action">留言板</a></li>
<li><a href="New_Msg.action">最新留言</a></li>
<li><a href="Send_Msg.action">留言</a></li>
</div>
<div id="div2">
<h3>最新留言</h3>
<div id="divspan">
<table border="1" align="left">
<tr>
<td>信息内容</td>
<td>发送人</td>
<td>发送时间</td>
</tr>
<s:iterator id="map1" value="#session.map">
<tr>
<td><s:property value="#map1.key.content"/></td>
<td><s:property value="#map1.value"/></td>
<td><s:property value="#map1.key.date"/></td>
</tr>
</s:iterator>
</table>
</div>
</div>
</div>
</body>
</html>
jsp中遍历map的语句为:
<s:iterator id="map1" value="#session.map">
<tr>
<td><s:property value="#map1.key.content"/></td>
<td><s:property value="#map1.value"/></td>
<td><s:property value="#map1.key.date"/></td>
</tr>
</s:iterator>
先从session取出map, 再用iterator标签来遍历map,
用 “#+id”表示其中之一键值对, 如: #map1
用 “#+id.key”表示引用其键, 如: #map1.key
用 “#+id.value”表示引用其值, 如: #map1.value