这个回复过程其实是2部,1.预处理:包括列出与ID相关的留言,预设回复的默认标题以及对应的回复id (replyId);2.回复留言,添加记录。
a. PreReplyAction.java
public class PreReplyAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "bbs.read";
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
ThreadForm f = (ThreadForm) form;
String id = (String) request.getParameter("id");
String sql;
QueryRunner qr = DbHelper.getQueryRunner();
List list = null;
sql = "select * from guestbook where id = " + id;
try {
list = (List) qr.query(sql, new BeanListHandler(Post.class));
} catch (SQLException ex) {
Logger.getLogger(PreReplyAction.class.getName()).log(Level.SEVERE, null, ex);
}
Post post = null;
post = (Post)list.get(0);
List replies = null;
sql = "select * from guestbook where replyId =" + id + " order by id";
try {
replies = (List) qr.query(sql, new BeanListHandler(Post.class));
} catch (SQLException ex) {
Logger.getLogger(PreReplyAction.class.getName()).log(Level.SEVERE, null, ex);
}
Topic topic = new Topic(post,replies);
f.setTopic(topic);
f.setSubject("Re :" + post.getSubject());
f.setReplyId(post.getId());
return mapping.findForward(SUCCESS);
}
}
b. ReplyAction.java
1 public class ReplyAction extends org.apache.struts.action.Action {
2
3 /* forward name="success" path="" */
4 private static final String SUCCESS = "bbs.reply";
5
6 /**
7 * This is the action called from the Struts framework.
8 * @param mapping The ActionMapping used to select this instance.
9 * @param form The optional ActionForm bean for this request.
10 * @param request The HTTP Request we are processing.
11 * @param response The HTTP Response we are processing.
12 * @throws java.lang.Exception
13 * @return
14 */
15 @Override
16 public ActionForward execute(ActionMapping mapping, ActionForm form,
17 HttpServletRequest request, HttpServletResponse response)
18 throws Exception {
19 ThreadForm f = (ThreadForm)form;
20 String sql = "insert into guestbook (name,subject,email,url,content,iconId,password,font,replyId,date,lastReplyTime) " +
21 "values(?,?,?,?,?,?,?,?,?,now(),now())";
22 String content = f.getContent();
23 content = content.replaceAll(" ", " ");
24 content = content.replaceAll("\n","<br>");
25 String params[] = {f.getName(),f.getSubject(),f.getEmail(),f.getUrl(),content,new Integer(f.getIconId()).toString(),f.getPassword(),f.getFont(),new Integer(f.getReplyId()).toString()};
26
27 QueryRunner qr = DbHelper.getQueryRunner();
28 try {
29 qr.update(sql, params);
30 } catch (SQLException ex) {
31 Logger.getLogger(ReplyAction.class.getName()).log(Level.SEVERE, null, ex);
32 }
33 sql = "update guestbook set lastReplyTime= now() where id = " + f.getReplyId();
34 try {
35 qr.update(sql);
36 } catch (SQLException ex) {
37 Logger.getLogger(ReplyAction.class.getName()).log(Level.SEVERE, null, ex);
38 }
39 return mapping.findForward(SUCCESS);
40 }
41 }
这个基本和添加留言的一直,只是在insert回复记录以后,更新主题留言的最近的回复时间,可以保证浏览留言的时候,该主题能在最前端。