接一篇文章,今天上午实现了添加数据。以下是Jsp。里面主要是看newUser()和saveUser().注意这函数里的url,newUser()里面去掉url属性。还要注意的一个问题
这里面,<a href="#" >的#要改为javvascript:void(0); 这样才不会出现新建用户时找不到页面的情况。
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>jQuery EasyUI CRUD Demo
id='+row.id; } } function saveUser(){ $('#fm').form('submit',{ url:url, onSubmit: function(){ return $(this).form('validate'); }, success: function(result){ var result = eval('('+result+')'); if (result.success){ $.messager.show({ title:'Info', msg:result.msg, showType:'fade', style:{ right:'', bottom:'' } }); $('#dlg').dialog('close'); // close the dialog $('#dg').datagrid('reload'); // reload the user data } else { $.messager.show({ title: 'Error', msg: result.msg }); } } }); } function removeUser(){ var row = $('#dg').datagrid('getSelected'); if (row){ $.messager.confirm('Confirm','Are you sure you want to remove this user?
',function(r){ if (r){ $.post('remove_user.php',{id:row.id},function(result){ if (result.success){ $('#dg').datagrid('reload'); // reload the user data } else { $.messager.show({ // show error message title: 'Error', msg: result.msg }); } },'json'); } }); } } </script> </head> <body> <h2>Basic CRUD Application</h2> <div class="demo-info" style="margin-bottom:10px"> <div class="demo-tip icon-tip"> </div> <div>Click the buttons on datagrid toolbar to do crud actions.</div> </div> <table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:250px" url="user/listUsers" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true"> <thead> <tr> <th field="userId" width="50">UserId</th> <th field="userName" width="50">UserName</th> <th field=passWord width="50">PassWord</th> <th field="enable" width="50">Enable</th> </tr> </thead> </table> <div id="toolbar"> <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" plain="true" οnclick="newUser()">New User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" οnclick="editUser()">Edit User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" οnclick="removeUser()">Remove User</a> </div> <div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px" closed="true" buttons="#dlg-buttons"> <div class="ftitle">User Information</div> <form id="fm" method="post" novalidate> <div class="fitem"> <label>UserId:</label> <input name="UserId" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>UserName:</label> <input name="UserName" class="easyui-validatebox" required="true"> </div> <div class="fitem"> <label>PassWord:</label> <input name="PassWord"> </div> <div class="fitem"> <label>Enable:</label> <input name="Enable" class="easyui-validatebox" > </div> </form> </div> <div id="dlg-buttons"> <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-ok" οnclick="saveUser()">Save</a> <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" οnclick="javascript:$('#dlg').dialog('close')">Cancel</a> </div> </body> </html>
UserController:package com.yang.bishe.controller;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.yang.bishe.entity.Json;import com.yang.bishe.entity.User;import com.yang.bishe.service.interfaces.IUserService;@Controller@RequestMapping("/user")public class UserController extends BaseController { @Autowired private IUserService userService; @RequestMapping("/list") public ModelAndView goList(){ return new ModelAndView("user/list"); } @RequestMapping("/listUsers") public String listUser(HttpServletRequest request, HttpServletResponse response) throws Exception { // return "/views/index"; String hql="from User"; Listusers=userService.find(hql); // String result=userService.find(hql); writeJson(users,response); return null; } @RequestMapping("/addUser") public String addUser(User user,HttpServletRequest request, HttpServletResponse response) throws Exception{ Json json = new Json();//用于向前端发送消息 if(userService.getById(user.getUserId())!=null){ json.setMsg("新建用户失败,用户已存在!
"); }else{ userService.save(user); json.setMsg("新建用户成功!"); json.setSuccess(true); } writeJson(json,response); return null; } }
writeJson(json,response)这里是把消息传给前端页面。在script里面的函数里success:funciont(result);的result就存有controller里的json消息。