NutzCN Logo
问答 nutz的module的类型,需要json来进行传数据!
发布于 2616天前 作者 qq_da40428e 2101 次浏览 复制 上一个帖子 下一个帖子
标签:

@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);
}

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("添加成功");

         }

       });
}
31 回复

用 "插入代码"按钮重新贴一次

大神 啥意思?上面是module 下面是javascript中的内容 怎么转换成json格式

因为javascript里面收不到数据 可能是module里面的格式对应不上 ,我想转成json能够收取

看到 回复 按钮旁边还有个按钮不?

@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);
 }
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("添加成功");

         }

       });
}

加上 @Ok("json:full")

谢谢大佬 我试试

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:{locked:'password',ignoreNull:true}")
@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
	@Ok("json:full")
    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>

大神 为什么我的数据插入进去 显示成功了 但是数据没进入数据库 只是表面上成功了

那个@ok加在上面也不行 我刚试了 还是没有数据

后台日志贴来看看

在这个帖子继续贴,不要再重复发帖了

log4j: 2017-09-17 18:07:34,265 [http-bio-8080-exec-3] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [POST] path=/student/add : StudentModule.add(StudentModule.java:95)
log4j: 2017-09-17 18:07:34,267 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.NutIoc - Get 'studentModule'<class com.nutz.module.StudentModule>
log4j: 2017-09-17 18:07:34,267 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.NutIoc - 	 >> Load definition name=studentModule
log4j: 2017-09-17 18:07:34,267 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.loader.combo.ComboIocLoader - Found IocObject(studentModule) in AnnotationIocLoader(packages=[com.nutz])
log4j: 2017-09-17 18:07:34,267 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.NutIoc - 	 >> Make...'studentModule'<class com.nutz.module.StudentModule>
log4j: 2017-09-17 18:07:34,269 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.aop.impl.DefaultMirrorFactory - Load class com.nutz.module.StudentModule without AOP
log4j: 2017-09-17 18:07:34,269 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.ScopeContext - Save object 'studentModule' to [app] 
log4j: 2017-09-17 18:07:34,274 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<interface org.nutz.dao.Dao>
log4j: 2017-09-17 18:07:34,286 [http-bio-8080-exec-3] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [GET] path=/student/query : StudentModule.query(StudentModule.java:129)
log4j: 2017-09-17 18:07:34,286 [http-bio-8080-exec-3] DEBUG org.nutz.ioc.impl.NutIoc - Get 'studentModule'<class com.nutz.module.StudentModule>
log4j: 2017-09-17 18:07:34,289 [http-bio-8080-exec-3] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT * FROM student 

查询都做出来了,新增修改删除都是前段显示成功,但是后端没运行

js端, 打印一下服务器返回的内容, 用console.log之类的方法

是我的jsp端出错了吗?

在哪里加上去?不怎么懂

没用过Chrome的F12?? 可以看服务器响应的内容哦

既然不能证明是页面问题还是服务器问题, 那就取求证一下嘛

通过Chrome的开发者工具(F12)的networks tab, 可以看到服务器的响应内容, 从而判断是哪里出问题了

用了 但是里面的数据没有报错

是空白的,但是没看见add的方法

打开后,再操作页面才会有

我就是使用了按钮 也是没有报错

您有qq吗 加下我行吗?

你加qq群吧 68428921

添加回复
请先登陆
回到顶部