📄 sendmessage.java
字号:
package com.yhbbs.message.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.yhbbs.message.bean.MessageIm;
import com.yhbbs.message.biz.MessageBiz;
import com.yhbbs.message.form.MessageForm;
import com.yhbbs.message.itface.Message;
import com.yhbbs.user.biz.UserBiz;
import com.yhbbs.user.itface.bean.UserSession;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.ReqUtils;
import com.yhbbs.utils.StringUtils;
/**
* <p>Title:发送短消息Action</p>
* <li> 发送短消息模块<br>
* <li> 删除短消息模块:第一次删除短消息的用户实际上是更新了isdel为自己的Id,第二个删除短消息的用户才是彻底删除<br>
* <li> 回复短消息模块<br>
* <br><b>WebSite: www.yyhweb.com</b>
* <br><b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YHBBS-2.0
*/
public class SendMessage extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
ActionMessages errors = new ActionMessages();
// 判断当前用户是否存在
UserSession curUser = (UserSession) request.getSession(true).getAttribute(Constants.bbsuser);
if(curUser==null){
errors.add("message.no.user",new ActionMessage("message.no.user"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
int userId = curUser.getUserId();
String str = ReqUtils.getString(request,"a");
/** 给非好友发送短消息 */
int toUserId = ReqUtils.getInt(request,"toId");
if(toUserId>0){
String userName = UserBiz.getUserName(toUserId);
if(userName!=null && userName.length()>0){
/** 设置令牌 */
this.saveToken(request);
request.setAttribute("toUserName", userName);
return mapping.findForward("Success");
}else{
errors.add("message.send.nouser",new ActionMessage("message.send.nouser"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
}
/**
* 如果是删除操作要有以下判断(这里讲原理,具体操作在数据库里已经有实现)
* 首先判断是否有发件者是否有保存,没有保存直接删除 有保存进行一下操作
* 首先取得isdel>0?表示已经被人删除过:没有被人删除过
* 如果isdel>0,isdel!=curuserId?可以彻底删除:不可能
* 如果isdel==0?更新isdel=curuserId
*/
if(str!=null && str.equals("delete")){
int dmsgId = ReqUtils.getInt(request, "dmsgId");
int i = 0; // 标示是从发件还是收件箱删除 0:发 1:收 2:彻底
Message delMsg = MessageBiz.getMessage(dmsgId);
if(delMsg==null){
errors.add("message.delete.noexist",new ActionMessage("message.delete.noexist"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
int isdel = delMsg.getIsdel();
String issave = delMsg.getSave();
if(issave.equals("0")){
i = 2;
}else if(isdel>=1)
i = 2;
else if(userId==delMsg.getFromId())
i = 0;
else
i = 1;
MessageBiz.delMessage(dmsgId,i);
return mapping.findForward("Delete");
}
/** 回复时要设置令牌 */
if(str!=null && str.equals("replay")){
this.saveToken(request);
int rmsgId = ReqUtils.getInt(request, "rmsgId");
Message msg = MessageBiz.getMessage(rmsgId);
if(msg==null){
errors.add("message.replay.noexist",new ActionMessage("message.replay.noexist"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
String content = msg.getContent();
msg.setContent(StringUtils.unEncode(content));
request.setAttribute("replaymsg", msg);
return mapping.findForward("Success");
}
/**
* 发送取得消息验证用户是否存在、令牌检查
* 发送完毕后跳转到收件箱
* 第一次进入该页面时要设置令牌
*/
MessageForm mform = (MessageForm)form;
String action= mform.getAction();
if(action.equals("post")){
String title = mform.getTitle();
String friend = mform.getFriend();
String content = mform.getContent();
String save= mform.getSave();
int tid = 0;
int fid = curUser.getUserId();
String fname = curUser.getUsername();
if(friend!=null && friend.length()>1){
tid = UserBiz.getUserId(friend);
if(tid<1){
errors.add("message.send.nouser",new ActionMessage("message.send.nouser"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
if(tid==userId){
errors.add("message.send.tomyself",new ActionMessage("message.send.tomyself"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
}
if(this.isTokenValid(request,true)){ //令牌检查
resetToken(request);
}else{
this.saveToken(request);
errors.add("message.token.error",new ActionMessage("message.token.error"));
saveErrors(request,errors);
return mapping.findForward("Failure");
}
Message msg = new MessageIm();
msg.setTitle(title);
msg.setContent(content);
msg.setFromId(fid);
msg.setFromUser(fname);
msg.setToId(tid);
msg.setToUser(friend);
msg.setSave(save);
MessageBiz.sendMessage(msg);
return mapping.findForward("Delete");
}else{
this.saveToken(request);
}
return mapping.findForward("Success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -