SpringMVC+easyUI CRUD 添加数据C

SpringMVC+easyUI CRUD 添加数据C

大家好,又见面了,我是全栈君。

接一篇文章,今天上午实现了添加数据。以下是Jsp。里面主要是看newUser()和saveUser().注意这函数里的url,newUser()里面去掉url属性。还要注意的一个问题

<div id="toolbar">
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">Remove User</a>
	</div>

这里面,<a href=”#”  >的#要改为javvascript:void(0);  这样才不会出现新建用户时找不到页面的情况。

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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">
	<base href="<%=basePath%>">
	<meta name="description" content="easyui help you build your web page easily!">
	<title>jQuery EasyUI CRUD Demo</title>
		<link rel="stylesheet" type="text/css" href="css/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="css/easyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="css/easyui/demo.css">
	<script type="text/javascript" src="js/jquery-easyui-1.4.2/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
	<style type="text/css">
		#fm{
			margin:0;
			padding:10px 30px;
		}
		.ftitle{
			font-size:14px;
			font-weight:bold;
			color:#666;
			padding:5px 0;
			margin-bottom:10px;
			border-bottom:1px solid #ccc;
		}
		.fitem{
			margin-bottom:5px;
		}
		.fitem label{
			display:inline-block;
			width:80px;
		}
	</style>
	<script type="text/javascript">
		var url;
		function newUser(){
			$('#dlg').dialog('open').dialog('setTitle','New User');
			$('#fm').form('clear');
			url = 'user/addUser';
		}
		function editUser(){
			var row = $('#dg').datagrid('getSelected');
			if (row){
				$('#dlg').dialog('open').dialog('setTitle','Edit User');
				$('#fm').form('load',row);
				url = 'update_user.php?

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" onclick="newUser()">New User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="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" onclick="saveUser()">Save</a> <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" onclick="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";
		List<User>users=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消息。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115805.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 数组与List的相互转换

    数组与List的相互转换1.List转换为数组直接调用ArrayList中的toArray方法就可以实现。List接口中,toArray有两个重载的方法:Object[]toArray();T[]toArray(T[]a);可见toArray可以用无入参的方式调用,返回一个Object数组;也可以用指定返回类型的方式调用,返回一个指定类型的数组。@Testpublicvoidtest()

  • 有关onpropertychange事件

    有关onpropertychange事件<divstyle="border:1pxsolid#fc0;height:24px;width:300px;"id="target">&l

  • ES6数组新方法[通俗易懂]

    ES6数组新方法[通俗易懂]ES6数组新方法目录ES6数组新方法1.`forEach()`和`map()`2.`filter()`3.`reduce()`4.`some()`5.`every()`6.`Array.from()`7.`Array.of()`8.`copyWithin()`9.`find()`和`findIndex()`10.`fill()`11.`entries()`,`keys()`和`values()`12.`includes()`13.`flat()`,`flatMap()`

  • HostMyBytes

    HostMyBytes 推荐配置:Hostmybytes KVMVPS512MBRAM(SS+BBR加速几乎不占用内存,三四个小站没问题).BudgetVPSHostingPackages:Locationsavailablein:LosAngelesandLondonUK!128MB RAMVPS- $6/year 768MB RAMVPS- …CLICKL…

  • vue常见错误:Invalid prop: type check failed for prop “data“. Expected Array, got Object

    vue常见错误:Invalid prop: type check failed for prop “data“. Expected Array, got Object错误截图错误分析这个错误的意思是说:无效的命名数据:“数据”类型检查失败。期望数组,得到对象,那么我们这个时候很明白了,是类型不对,但是是哪一行的呢?打开错误信息下面的详情,这个时候找到后缀是自己页面的.vue文件,看看是哪一行,就知道问题在哪了!下面的是我的:warn @ vue.esm.js?efeb:610assertProp @ vue.esm.js?efeb:1691vali…

  • Hibernate与MyBatis详解「建议收藏」

    Hibernate与MyBatis详解「建议收藏」Hibernate&amp;amp;nbsp;是当前最流行的O/Rmapping框架,它出身于sf.net,现在已经成为Jboss的一部分。&amp;amp;nbsp;Mybatis&amp;amp;nbsp;是另外一种优秀的O/Rmapping框架。目前属于apache的一个子项目。MyBatis&amp;amp;nbsp;参考资料官网:http://www.mybatis.org/core/zh/index.html&amp;amp;nbsp;&a

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号