package com.nutz.module;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import com.nutz.bean.Student;
@IocBean
@At("/student")
@Ok("json:full")
@Fail("http:500")
public class StudentModule {
@Inject
protected Dao dao;
@At
public int count(){
return dao.count(Student.class);
}
@At
@Ok(">>:/")
public void logout(HttpSession session){
session.invalidate();
}
@At
@Filters
public Object login(@Param("studentName")String name,@Param("password")String password,HttpSession session) {
Student student=dao.fetch(Student.class, Cnd.where("studentName","=",name).and("password", "=",password));
if(student==null){
return false;
}else{
session.setAttribute("me", student.getId());
return true;
}
}
protected String checkStudent(Student student,boolean create){
if(student==null){
return "空对象";
}
if(create){
if(Strings.isBlank(student.getStudentName())||Strings.isBlank(student.getPassword()))
return "用户名/密码不能为空";
}else{
if(Strings.isBlank(student.getPassword()))
return "密码不能为空";
}
String password=student.getPassword().trim();
if(6>password.length()||password.length()>12){
return "密码长度6~12位";
}
student.setPassword(password);
if(create){
int count=dao.count(Student.class, Cnd.where("name", "=", student.getStudentName()));
if(count !=0){
return "学生已存在";
}
}else{
if(student.getId()<1){
return "学生Id非法";
}
}
if(student.getStudentName() !=null)
student.setStudentName(student.getStudentName().trim());
return null;
}
@At
public Object add(@Param("..")Student student){
NutMap re=new NutMap();
String msg=checkStudent(student,true);
if(msg !=null){
return re.setv("ok", false).setv("msg", msg);
}
student=dao.insert(student);
return re.setv("ok", true).setv("data", student);
}
@At
public Object update(@Param("..")Student student){
NutMap re=new NutMap();
String msg=checkStudent(student,false);
if(msg !=null){
return re.setv("ok", false).setv("msg", msg);
}
student.setStudentName(null);
dao.updateIgnoreNull(student);
re.setv("ok", true);
return student;
}
@At
public Object delete(@Param("id")int id, @Attr("me")int me) {
if (me == id) {
return new NutMap().setv("ok", false).setv("msg", "不能删除当前学生!!");
}
dao.delete(Student.class, id);
return new NutMap().setv("ok", true);
}
@At
public List query(@Param("name")String name,HttpServletRequest request){
Cnd cnd=Strings.isBlank(name)? null: Cnd.where("name","like","%"+name+"%");
QueryResult qr=new QueryResult();
qr.setList(dao.query(Student.class, cnd));
return qr.getList();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>界面</title>
<link rel="stylesheet" type="text/css" href="./easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="./easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="./easyui/demo/demo.css">
<script type="text/javascript" src="./easyui/jquery.min.js"></script>
<script type="text/javascript" src="./easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
var base = '<%=request.getAttribute("base")%>';
$(function(){
//ip与用户配置表
$('#ds').datagrid({
url:base +"/student/query",
pagination:false,//显示底部分页栏
singleSelect:true,
method: 'GET',
columns:[[
{field:'id',title:'Id',width:50,align:'center',editor:'text'},
{field:'studentName',title:'用户名',width:100,align:'center',editor:'text'},
{field:'password',title:'密码',width:100,align:'center',editor:'text'},
{field:'address',title:'地址',width:200,align:'center',editor:'text'},
{field:'_opt',title:'操作',width:100,align:'center',formatter:function(value,rec){
var btn = '<input type="button" onclick="update()" value="保存"/>';
return btn;
}}
]],
toolbar : [ {
text : '添加',
iconCls : 'icon-add',
handler : function(){
$('#ds').datagrid("appendRow",{id:'',studentName:'',password:'',address:''});
}
}, '-', {
text : '删除',
iconCls : 'icon-remove',
handler : function(){
var _sR = $('#ds').datagrid("getSelected");
var q = confirm("确认删除吗?");
if(q){
$('#ds').datagrid('acceptChanges');
var _sr = $('#ds').datagrid("getSelected");
var _delURL = base +"/student/delete";
var _data = JSON.stringify(_sr);
$.ajax({
type: 'POST',
url: _delURL,
data:_data,
dataType:"json",
contentType:"application/json",
success: function(msg){
alert("删除成功");
$('#ds').datagrid("reload");
}
});
}else{
$('#ds').datagrid('acceptChanges');
}
}
} ],
onClickCell : function(index,field){
$('#ds').datagrid('acceptChanges');
$('#ds').datagrid('beginEdit', index);
},
onLoadSuccess:function(data){
rows = $('#ds').datagrid("getRows").length;
}
});
});
function update(){
$('#ds').datagrid('acceptChanges');
var _selectedRow = $('#ds').datagrid("getSelected");
var _updURL = base +"/student/update";
var _addURL = base +"/student/add";
var _data = JSON.stringify(_selectedRow);
var newrows = $('#ds').datagrid("getRows").length;
if(newrows== rows){
$.ajax({
type:'POST',
url: _updURL,
data: _data,
dataType:"json",
contentType:"application/json",
success: function(msg){
$('#ds').datagrid("reload");
alert("修改成功");
}
});
}else{
$.ajax({
type: 'POST',
url: _addURL,
data: _data,
dataType:"json",
contentType:"application/json",
success: function(msg){
$('#ds').datagrid("reload");
alert("添加成功");
}
});
}
};
</script>
</head>
<body>
<table id="ds" title="" style="width:602px"></table>
</body>
</html>
灌水
求大神解决问题 为什么我的数据插入进去 显示成功了 但是数据没进入数据库 只是表面上成功了
标签:
无
0 回复
添加回复
该帖子已被锁定,不能回复.