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消息。

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何编写单元测试用例

    如何编写单元测试用例 一、单元测试的概念  单元通俗的说就是指一个实现简单功能的函数。单元测试就是只用一组特定的输入(测试用例)测试函数是否功能正常,并且返回了正确的输出。  测试的覆盖种类  1.语句覆盖:语句覆盖就是设计若干个测试用例,运行被测试程序,使得每一条可执行语句至少执行一次。  2.判定覆盖(也叫分支覆盖):设计若干个测试用例,运行所测程序,使程序中每个判断的取真分支和取假分支至少执行一次。  3.条件…

    2022年6月16日
    43
  • 5G信道编码之争

    5G信道编码之争2019年华为技术再次突围,中国有一次掀起5G热潮。时间回到2016年,让我们去看看当年精彩的5G信道编码之争。什么是信道编码?在移动通信中,由于存在干扰和衰落,信号在传输过程中会出现差错。所以需要对数字信号采用纠、检错编码技术,以增强数据在信道中传输时抗干扰的能力,提高系统的可靠性。对要在信道中传送的数字信号进行的纠、检错编码就是信道编码。信道编码是为了降低误码率和提高数字通信的可靠性而采取的编码。信道编码是如何检出和校正接收比特流中的差错呢?通过加入一些冗余比特,把几个比特上携带的信息扩散到

    2022年5月2日
    51
  • Python爬虫实战——搭建自己的IP代理池[通俗易懂]

    Python爬虫实战——搭建自己的IP代理池[通俗易懂]如今爬虫越来越多,一些网站网站加强反爬措施,其中最为常见的就是限制IP,对于爬虫爱好者来说,能有一个属于自己的IP代理池,在爬虫的道路上会减少很多麻烦环境参数工具详情服务器Ubuntu编辑器Pycharm第三方库requests、bs4、redis搭建背景之前用Scrapy写了个抓取新闻网站的项目,今天突然发现有一个网站的内容爬不下来…

    2022年5月11日
    52
  • eclipse代码自动补全[通俗易懂]

    eclipse代码自动补全[通俗易懂]1、点击菜单栏,打开Eclipse->Window->Perferences2、找到Java下的 Editor下的 ContentAssist,点击它3、找到第二个“AutoactivationtriggersforJava:”选项,在其后的文本框中会看到一个“.”存在。这表示:只有输入“.”之后才会有代码提示和自动补全,把该文本框中的“.”换成“abcdefghijklmnopqrstuvwxyz.”即可。…

    2022年5月31日
    33
  • wifi数据包解析_详细解析WiFi模块的基础知识「建议收藏」

    wifi数据包解析_详细解析WiFi模块的基础知识「建议收藏」1、WiFi模块的SDK是什么?SDK是WiFi模块的软件开发工具包(全称:SoftwareDevelopmentKit)的英文简称,一般都是一些软件工程师为特定的软件包、软件框架,硬件平台,操作系统等建立应用软件时的开发工具的集合。2、WiFi模块的驱动是什么?驱动程序即添加到操作系统中的一小块代码,其中包含有关硬件设备的信息。有了此信息,计算机就可以与设备进行通信。驱动程序是硬件厂…

    2022年7月21日
    25
  • mybatis-plus id主键生成的坑

    mybatis-plus id主键生成的坑mybatis-plusid主键生成的坑简要说明错误解决方案一1.修改id字段类型2.调整数据库id字段类型解决方案二添加注解其他`type`类型介绍简要说明由于mybatis-plus会自动插入一个id到实体对象,不管你封装与否,所以有时候导致一些意外的情况发生默认是生成一个长数字字符串(编码不同可能结尾带有字母)错误estedexceptionisorg.apac…

    2022年6月17日
    82

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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