用户添加技术点:#
性别下拉列表框(固定值的下拉列表框)
角色下拉列表框(来自数据库的角色列表)
学生演示上述功能
关键代码
新增菜单项
{id: "splitter", text: "用户管理", pid: "layouts",url:"/admin/user/goUser"},
controller跳转到用户管理页
@RequestMapping("/goUser")
public String goMenu() {
return "userlist";
}
userlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用户管理</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="<%=request.getContextPath() %>/css/demo.css" rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath() %>/js/boot.js" type="text/javascript"></script>
</head>
<body>
<h1>用户管理</h1>
<div style="padding-top:5px;padding-bottom:5px;">
<input type="button" value="Add" onclick="addRow()"/>
<input type="button" value="Remove" onclick="removeRow()" />
<input type="button" value="Save" onclick="saveData()" style="margin-left:100px;"/>
</div>
<div id="datagrid1" class="mini-datagrid" style="width:820px;height:250px;"
url="<%=request.getContextPath() %>/admin/user/adminlist" idField="id"
multiSelect="true" allowResize="true"
>
<div property="columns">
<div type="checkcolumn"></div>
<div field="id" width="120" headerAlign="center" allowSort="true">用户编号</div>
<div field="name" width="100" allowSort="true">用户名称</div>
<div field="sex" width="100" allowSort="true" renderer="onGenderRenderer" align="center" headerAlign="center">性别</div>
<div field="email" width="100" allowSort="true">邮箱</div>
<div field="birthday" width="100" allowSort="true" dateFormat="yyyy-MM-dd">生日</div>
<div field="roleid" width="100" allowSort="true">角色编号</div>
</div>
</div>
<fieldset style="width:800px;border:solid 1px #aaa;margin-top:8px;position:relative;">
<legend>员工详细信息</legend>
<div id="editForm1" style="padding:5px;">
<input class="mini-hidden" name="id"/>
<table style="width:100%;">
<tr>
<td style="width:120px;" >用户编号:</td>
<td style="width:150px;"><input id="id" name="id" class="mini-textbox" readonly="readonly"/></td>
<td style="width:120px;">用户名称:</td>
<td style="width:150px;"><input id="name" name="name" class="mini-textbox" /></td>
<td style="width:120px;">性别:</td>
<td style="width:150px;">
<input id="sex" name="sex" class="mini-combobox" data="Genders"/>
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input id="email" name="email" class="mini-textbox" /></td>
<td>生日:</td>
<td><input id="birthday" name="birthday" class="mini-datepicker" minValue="0" maxValue="200" value="25" /></td>
<td>角色编号:</td>
<td>
<input id="roleid" name="roleid" showNullItem="true" class="mini-combobox"
url="<%=request.getContextPath()%>/admin/power/getRole"
emptyText="请选择..."
nullItemText="请选择..."
textField="rolename" valueField="roleid"/>
</td>
</tr>
</table>
</div>
</fieldset>
<script type="text/javascript">
var Genders = [{ id: 1, text: '男' }, { id: 2, text: '女'}];
mini.parse();
var grid = mini.get("datagrid1");
grid.load();
//绑定表单
var db = new mini.DataBinding();
db.bindForm("editForm1", grid);
///////////////////////////////////////////////////////
function onGenderRenderer(e) {
for (var i = 0, l = Genders.length; i < l; i++) {
var g = Genders[i];
if (g.id == e.value) return g.text;
}
return "";
}
//////////////////////////////////////////////////////
function addRow() {
var newRow = { name: "New Row" };
//添加一行
grid.addRow(newRow, 0);
//取消选中旧的数据行
grid.deselectAll();
//选中新增加的数据行
grid.select(newRow);
}
function removeRow() {
//得到选中的行
var rows = grid.getSelecteds();
if (rows.length > 0) {
if (confirm("确定删除选中记录?")) {
//定义一个数组
var ids = [];
for (var i = 0, l = rows.length; i < l; i++) {
var r = rows[i];
//循环rows,将id两侧加上单引号后放入数组
ids.push("'"+r.id+"'");
}
//用逗号分隔数组中的数 '1','2','3'
var id = ids.join(',');
grid.loading("操作中,请稍后......");
$.ajax({
url: "<%=request.getContextPath()%>/admin/user/del?id=" + id,
success: function (text) {
grid.reload();
},
error: function () {
}
});
}
} else {
alert("请选中一条记录");
}
}
function saveData() {
//获取变动的数据
var data = grid.getChanges();
var json = mini.encode(data);//反序列成json数据
grid.loading("保存中,请稍后......");
//json数组
alert(json);
$.ajax({
headers: {//公有配置,可以封装复用
'Accept': 'application/json',
'Content-Type': 'application/json'
},
url: "<%=request.getContextPath()%>/admin/user/adduser",//method是操作标识。save update del
data: json,
type: "post",
success: function (result) {
//alert(result.code);
grid.reload();
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
</script>
<div class="description">
<h3>Description</h3>
</div>
</body>
</html>
UserController.java
package com.neuedu.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.module.service.IUserService;
import com.neuedu.pojo.ResultMsg;
import com.neuedu.pojo.UserList;
@Controller
@RequestMapping("/admin/user")
public class UserController {
@Autowired
IUserService UserServiceImpl;
@RequestMapping("/goUser")
public String goMenu() {
return "userlist";
}
@RequestMapping("/adminlist")
@ResponseBody
public Map adminlist(int pageIndex,int pageSize,String key,String sortField,String sortOrder) {
// 第一步,读取满足条件的记录数
int total = UserServiceImpl.getUserCount();
List<UserList> list = null;
// 如果total>0,执行第二步,查询满足条件的记录数
if (total > 0) {
list = UserServiceImpl.getUserAll();
}
Map map = new HashMap();
map.put("total", total);
map.put("data", list);
return map;
}
@RequestMapping("/adduser")
@ResponseBody
public ResultMsg adduser(@RequestBody UserList[] adduser,String method) {
int i=0;
ResultMsg msg=new ResultMsg();
String oper=adduser[0].get_state();
if(oper.equals("added")){
i=UserServiceImpl.addAdminUser(adduser);
}else if(oper.equals("modified")){
i=UserServiceImpl.updateUser(adduser);
}
if(i==adduser.length){
msg.setCode(0);
msg.setMsg("success");
}else{
msg.setCode(-1);
msg.setMsg("failure");
}
return msg;
}
@ResponseBody
@RequestMapping("/del")
public ResultMsg del(String id){
int i=0;
ResultMsg msg=new ResultMsg();
i=UserServiceImpl.delUser(id);
if(i>0){
msg.setCode(0);
msg.setMsg("success");
}else{
msg.setCode(-1);
msg.setMsg("failure");
}
return msg;
}
}
IUserService.java
public interface IUserService {
public int addAdminUser(UserList[] adduser);
public int getUserCount();
public List<UserList> getUserAll();
public int updateUser(UserList[] adduser);
public int delUser(String id);
}
UserServiceImpl.java
@Override
public int getUserCount() {
// TODO Auto-generated method stub
return userMapper.getUserCount();
}
@Override
public List<UserList> getUserAll() {
return userMapper.getUserAll();
}
@Override
public int addAdminUser(UserList[] adduser) {
int i=0;
for(UserList u:adduser){
String id=UuidGenerateUtil.getUUID();
u.setId(id);
int count=userMapper.addAdminUser(u);
i+=count;
}
return i;
}
@Override
public int updateUser(UserList[] adduser) {
int i=0;
for(UserList u:adduser){
int count=userMapper.updateUser(u);
i+=count;
}
return i;
}
@Override
public int delUser(String id) {
// TODO Auto-generated method stub
return userMapper.delUser(id);
}