list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
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>发送页面</title>
<script type="text/javascript" src="static/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$(".changeusers").change(function(){
var uid=$("#uid").val();
window.location.href="message/getMessagesByUser.do?uid="+uid;
});
$(".btn").click(function(){
//自定义标签
/* var mid=$(this).attr("btn");
window.location.href="message/delete.do?mid="+mid;
*/
/* 方法2:
$('.btn')获取的是一组标签
var mid=$(this).parent().prev().prev().prev().prev().text();
alert(mid); */
var mid=$(this).parent().parent().find("td:eq(0)").text();
alert(mid);
window.location.href="message/delete.do?mid="+mid;
});
});
</script>
</head>
<body>
<center>
<h1>站内信查看页面</h1>
<select class="changeusers" id="uid" >
<option>---请选择---</option>
<option value="0">所有用户</option>
<c:forEach items="${users}" var="u">
<option value="${u.uid}">${u.uname}</option>
</c:forEach>
</select>
<br><br><br>
<table border="2" bgcolor="brown">
<tr>
<td>消息ID</td>
<td>消息内容</td>
<td>时间</td>
<td>接受人</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="m">
<tr>
<td >${m.mid}</td>
<td>${m.content}</td>
<td><f:formatDate value='${m.createtime}' pattern="yyyy-MM-dd hh:mm:ss"/></td>
<td>${m.user.uname}</td>
<td><button class="btn" btn="${m.mid }" >删除</button></td>
</tr>
</c:forEach>
<tr>
</tr>
</table>
</center>
</body>
</html>
send.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>发送页面</title>
<script type="text/javascript" src="static/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
alert()
$("#btn").click(function(){
var content=$("#msg").val();
if($("input[type=checkbox]:checked").length==0){
alert("请勾选择用户");
return;
}
// 不用ajax时,可以用超链接去跳转
/* var uids=new Array();
$(":checkbox[name='uids']:checked").each(function(){
uids.push($(this).val());
});
alert(uids);
alert(content); */
$(":checkbox[name='uids']:checked").each(function(){
var uid=$(this).val();
$.ajax({
url:"message/insert.do",
data:{"content":content,"uid":uid},
dataType:"json",
success:function(data){
alert("发送成功!!");
//清空文本域
$("#msg").val("");
//清空多选框的值
$("input:checked").prop("checked",false);
}
});
});
});
});
</script>
</head>
<body>
<center>
<h1>站内信发送页面</h1>
<table border="2" bgcolor="brown">
<tr >
<td>请选择联系人</td>
<td>
<c:forEach items="${users}" var="u">
<input type="checkbox" name="uids" value="${u.uid}"/>
${u.uname}
</c:forEach>
</td>
</tr>
<tr>
<td>请输入消息内容</td>
<td>
<textarea id="msg" rows="4" cols="60"></textarea>
</td>
</tr>
</table>
<input type="button" id="btn" value="发送" >
</center>
</body>
</html>
MessageMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.frank.dao.MessageMapper">
<insert id="insert" parameterType="message">
insert into message (content,createtime,u_id) values (#{content},#{createtime},#{user.uid})
</insert>
<resultMap type="message" id="messagemap">
<id column="mid" property="mid"/>
<result column="content" property="content"/>
<result column="createtime" property="createtime"/>
<association property="user" javaType="user">
<id column="uid" property="uid"/>
<result column="uname" property="uname"/>
<result column="upass" property="upass"/>
<result column="ugender" property="ugender"/>
</association>
</resultMap>
<select id="getMessagesByUser" parameterType="user" resultMap="messagemap">
select m.*,u.uname from message m,user u where m.u_id=u.uid
<if test="uid!=0">
and m.u_id=#{uid}
</if>
</select>
<delete id="delete" parameterType="int">
delete from message where mid=#{mid}
</delete>
</mapper>
MessageMapper接口
public interface MessageMapper {
public void insert(Message msg);
public void delete(int mid);
public List<Message> getMessagesByUser(User user);
}
MessageController类
@Controller
@RequestMapping("message")
public class MessageController {
@Autowired
private MessageService service;
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("insert")
public String insert(Message m,String content,int uid){
m.setContent(content);
m.setCreatetime(new Date());
m.setUser(new User(uid));
service.insert(m);
return "ok";
};
@RequestMapping("delete")
public String delete(int mid){
service.delete(mid);
return "redirect:select.do";
}
//public List<Message> getMessagesByUser(User user);
@RequestMapping("select")
public String getMessagesByUser(Model model,HttpSession session) {
List<User> users = userService.selectUsers();
session.setAttribute("users", users);
List<Message> list= service.getMessagesByUser(new User());
model.addAttribute("list", list);
return "list";
}
@RequestMapping("getMessagesByUser")
public String getMessagesByUsers(int uid,Model model) {
User user = new User();
System.out.println("-------:"+uid);
user.setUid(uid);
List<Message> list = service.getMessagesByUser(user);
for (Message message : list) {
System.out.println(message);
}
model.addAttribute("list",list);
return "list";
}
}