博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 网站用户在线和客服聊天
阅读量:6443 次
发布时间:2019-06-23

本文共 11522 字,大约阅读时间需要 38 分钟。

注:本文来源于《》

这是应用到项目中的一个例子。

实现原理是将信息存储到Application域里面。然后使用Struts2 Action 用json格式的数据进行前后台交互。

截图:

前台用户界面:

20130521081322386

后台客服界面:

20130521081348963

编辑框采用的Ueditor 可以发送图片表情文字格式、涂鸦。。。。等等!功能十分的强大!

代码部分:

20130521081505375

用到了两个Action,分别为用户和管理员服务。

Struts.xml

Java 类:

package com.shop.chat.admin;import java.util.ArrayList;import java.util.List;import com.shop.bean.Admin;/** * 存放当前已经登陆的管理员列表 * @author Pan * */public class AdminList {	private List
list=new ArrayList
(); public AdminList(){ } /** * 增加Admin对象 * @param admin */ public void save(Admin admin){ if(list.indexOf(admin)==-1){ list.add(admin); } } /** * 得到所有Admin * @return */ public List
getList(){ return list; }}

package com.shop.chat.admin;import java.util.List;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import com.shop.bean.Admin;/** * admin列表服务 * @author Pan * */public class AdminListServler {	private AdminList list;	public AdminListServler(HttpSession session){		ServletContext context=session.getServletContext();		list=(AdminList)context.getAttribute("adminlist");		if(list==null){			list=new AdminList();			context.setAttribute("adminlist", list);		}	}	/**	 * add an admin to the list	 * @param admin	 */	public void save(Admin admin){		list.save(admin);	}	/**	 * get all admin object	 * @return arraylist	 */	public List
getList(){ return list.getList(); }}
package com.shop.chat.admin;import java.sql.Timestamp;public class AdminSimplify {	private Integer id;	private String adminName;	private String adminPass;	private String nickName;	private String head;	private Timestamp addTime;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getAdminName() {		return adminName;	}	public void setAdminName(String adminName) {		this.adminName = adminName;	}	public String getAdminPass() {		return adminPass;	}	public void setAdminPass(String adminPass) {		this.adminPass = adminPass;	}	public String getNickName() {		return nickName;	}	public void setNickName(String nickName) {		this.nickName = nickName;	}	public String getHead() {		return head;	}	public void setHead(String head) {		this.head = head;	}	public Timestamp getAddTime() {		return addTime;	}	public void setAddTime(Timestamp addTime) {		this.addTime = addTime;	}}
package com.shop.chat;import java.util.ArrayList;import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.shop.bean.Admin;import com.shop.chat.admin.AdminListServler;import com.shop.chat.admin.AdminSimplify;public class AdminListAction extends ActionSupport {	private List
list; public List
getList() { return list; } /** * 得到当前登陆的管理员 * @return */ public String execute(){ list=new ArrayList
(); AdminListServler aServler=new AdminListServler(ServletActionContext.getRequest().getSession()); for (Admin admin : aServler.getList()) { AdminSimplify as=new AdminSimplify(); as.setAddTime(admin.getAddTime()); as.setAdminName(admin.getAdminName()); as.setAdminPass(admin.getAdminPass()); as.setHead(admin.getHead()); as.setId(admin.getId()); as.setNickName(admin.getNickName()); list.add(as); } return SUCCESS; }}
package com.shop.chat;import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.shop.bean.Admin;public class AdminMessageAction extends ActionSupport {	private String to;		//接受用户	private String result;	//返回消息	private String content;	//消息内容	private String nickName;	//昵称	public void setNickName(String nickName) {		this.nickName = nickName;	}	public String getNickName() {		return nickName;	}	public void setContent(String content) {		this.content = content;	}	private List
list; public List
getList() { return list; } public void setResult(String result) { this.result = result; } public String getResult() { return result; } public void setTo(String to) { this.to = to; } public String execute(){ Admin admin=(Admin)ServletActionContext.getRequest().getSession().getAttribute("admin"); if(admin==null){ setResult("你还没有登录!"); return SUCCESS; } MessageServer msServer=new MessageServer(ServletActionContext.getRequest().getSession(),admin.getId()+"",admin.getNickName()); list=msServer.getMessage(); return SUCCESS; } /** * 发送消息 * @return */ public String send(){ Admin admin=(Admin)ServletActionContext.getRequest().getSession().getAttribute("admin"); if(admin==null){ setResult("没有登录"); return SUCCESS; } MessageServer msServer=new MessageServer(ServletActionContext.getRequest().getSession(), admin.getId()+"",admin.getNickName()); try { msServer.send(to, content); setResult("发送成功!"); setNickName(admin.getNickName()); } catch (Exception e) { // TODO Auto-generated catch block result=e.getMessage(); } return SUCCESS; }}
package com.shop.chat;/** * 信息容器 * @author Pan * */public class Message {	private String form; // 来自用户	private String to; // 接受用户	private String message; // 信息	private String nickName;	//昵称	public void setNickName(String nickName) {		this.nickName = nickName;	}	public String getNickName() {		return nickName;	}	public String getForm() {		return form;	}	public void setForm(String form) {		this.form = form;	}	public String getTo() {		return to;	}	public void setTo(String to) {		this.to = to;	}	public String getMessage() {		return message;	}	public void setMessage(String message) {		this.message = message;	}}
package com.shop.chat;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.shop.bean.User;import com.sun.org.apache.bcel.internal.generic.NEW;public class MessageAction extends ActionSupport {	private String to;			//传送到指定的用户	private String result;		//返回信息	private String content;		//内容	private String nickName;	//昵称	private List
list; //消息集合 public void setNickName(String nickName) { this.nickName = nickName; } public String getNickName() { return nickName; } public void setResult(String result) { this.result = result; } public void setContent(String content) { this.content = content; } public List
getList() { return list; } public String getResult() { return result; } public void setTo(String to) { this.to = to; } /** * 获取消息 */ public String execute(){ HttpServletRequest request=ServletActionContext.getRequest(); User user=(User)request.getSession().getAttribute("user"); MessageServer msServer=new MessageServer(request.getSession(), user.getId()+"",user.getNickName()); this.list=msServer.getMessage(); return SUCCESS; } /** * 发送消息 * @return */ public String send(){ HttpServletRequest request=ServletActionContext.getRequest(); User user=(User)request.getSession().getAttribute("user"); if(user==null){ result="你还没有进行登录!"; return SUCCESS; } MessageServer mServer=new MessageServer(request.getSession(),user.getId()+"",user.getNickName()); mServer.getMessage(); try { mServer.send(to, content); System.out.println("to:"+to); setNickName(user.getNickName()); setResult("发送成功"); } catch (Exception e) { setResult("发送失败!"); result=e.getMessage(); } return SUCCESS; }}
package com.shop.chat;import java.util.ArrayList;import java.util.List;/** * 信息集合 这个对象储存到Application中 * @author Pan * */public class MessageList {	private List
list=new ArrayList
(); public MessageList(){ } /** * 添加信息 * @param message */ public void add(Message message){ list.add(message); } /** * 寻找信息 通过接受的用户 * @param uid * @return */ public List
getByTo(String uid){ List
tmp=new ArrayList
(); System.out.println("list的大小:"+list.size()); for(Message message:list){ if(message.getTo().equals(uid)){ //添加到返回对象 tmp.add(message); } } //然后移除信息 以免信息重复 list.removeAll(tmp); return tmp; }}
package com.shop.chat;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;public class MessageServer {	private MessageList mList;	private ServletContext context;	private String uid;	private String nickname;	public MessageServer(HttpSession session,String uid,String nickname){		this.context=session.getServletContext();		Object object=context.getAttribute("mlist");		if(object==null){			mList=new MessageList();			context.setAttribute("mlist", mList);		}else {			this.mList=(MessageList)object;		}		this.uid=uid;		this.nickname=nickname;	}	/**	 * 发送数据	 * @throws Exception	 */	public void send(String to,String content) throws Exception{		if(uid==null){			throw new Exception("用户名id无效");		}		Message message=new Message();		message.setForm(uid);		message.setTo(to);		message.setMessage(content);		message.setNickName(this.nickname);		mList.add(message);	}	/**	 * 接受信息	 * @return	 */	public List
getMessage(){ //返回当前用户的信息 System.out.println("uid是:"+uid); return mList.getByTo(uid); }}
package com.shop.chat.admin;import java.util.ArrayList;import java.util.List;import com.shop.bean.Admin;/** * 存放当前已经登陆的管理员列表 * @author Pan * */public class AdminList {	private List
list=new ArrayList
(); public AdminList(){ } /** * 增加Admin对象 * @param admin */ public void save(Admin admin){ if(list.indexOf(admin)==-1){ list.add(admin); } } /** * 得到所有Admin * @return */ public List
getList(){ return list; }}
package com.shop.chat.admin;import java.util.List;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import com.shop.bean.Admin;/** * admin列表服务 * @author Pan * */public class AdminListServler {	private AdminList list;	public AdminListServler(HttpSession session){		ServletContext context=session.getServletContext();		list=(AdminList)context.getAttribute("adminlist");		if(list==null){			list=new AdminList();			context.setAttribute("adminlist", list);		}	}	/**	 * add an admin to the list	 * @param admin	 */	public void save(Admin admin){		list.save(admin);	}	/**	 * get all admin object	 * @return arraylist	 */	public List
getList(){ return list.getList(); }}
package com.shop.chat.admin;import java.sql.Timestamp;public class AdminSimplify {	private Integer id;	private String adminName;	private String adminPass;	private String nickName;	private String head;	private Timestamp addTime;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getAdminName() {		return adminName;	}	public void setAdminName(String adminName) {		this.adminName = adminName;	}	public String getAdminPass() {		return adminPass;	}	public void setAdminPass(String adminPass) {		this.adminPass = adminPass;	}	public String getNickName() {		return nickName;	}	public void setNickName(String nickName) {		this.nickName = nickName;	}	public String getHead() {		return head;	}	public void setHead(String head) {		this.head = head;	}	public Timestamp getAddTime() {		return addTime;	}	public void setAddTime(Timestamp addTime) {		this.addTime = addTime;	}}

转载于:https://www.cnblogs.com/ios9/p/10136292.html

你可能感兴趣的文章
axis实例包
查看>>
归并排序 MergeSort
查看>>
Javascript的this用法
查看>>
Fiddler下Firefox提示“您的连接并不安全”的解决办法
查看>>
mint 安装emacs 24.3源码安装
查看>>
性能细节1
查看>>
Echarts图表
查看>>
Java线程面试题 Top 50
查看>>
解决mysql图形管理器乱码问题
查看>>
mysql :error while loading shared libraries: libaio.so.1: cannot open shared object file:
查看>>
我的友情链接
查看>>
我与51CTO的战争之还有人不依不饶
查看>>
使用FUSE挂载HDFS流程及错误集锦
查看>>
spring学习笔记(一)
查看>>
cocoaPods管理本地库
查看>>
程序员看电影之战狼2
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
pandas
查看>>
脚本编写练习题
查看>>