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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis

    小疯手把手带你整合SpringMVC+Spring+MyBatis三大框架,俗称SSM,用它完全代替传统的SSH框架,把它们最优雅的一面发挥出来。整合配置结束后,会有一个应用实例“图书管理系统”带给大家,希望能快速上手这个框架!

    2022年4月16日
    29
  • 高德地图实现多点标注功能

    高德地图实现多点标注功能

    2022年5月11日
    71
  • linux查看udp端口占用命令,详解Linux查看端口占用「建议收藏」

    linux查看udp端口占用命令,详解Linux查看端口占用「建议收藏」在使用计算机的过程中,有时会碰到端口被占用的情况,这时候需要查看端口占用情况进行问题排查。在linux系统中,端口占用的情况也时有发生,一般情况下可以使用lsof和netstat两个命令来查看端口占用情况。下面我们就分别介绍Linux查看端口占用用到的lsof和netstat命令:1、lsof命令lsof(listopenfiles)命令功能:列出当前系统打开文件。在linux系统中,一切皆文…

    2022年7月27日
    5
  • 51单片机之逻辑运算指令ANL、ORL、XRL等

    51单片机之逻辑运算指令ANL、ORL、XRL等文章目录前言一、简单操作指令CLR、CPL、SWAPCLRACPLASWAPA二、位移指令RL、RR、RLC、RRCRLARRARLCARRCA三、逻辑“与”指令ANLANLA,XXXANLdirect,XXX四、逻辑“或”指令ORLORLA,XXXORLdirect,XXX五、逻辑“异或”指令XRLORLA,XXXXRLdirect,XXX前言…

    2022年7月26日
    20
  • 软件管理和电脑管家打不开怎么办_电脑管家下载软件连接错误

    软件管理和电脑管家打不开怎么办_电脑管家下载软件连接错误错误:应用程序无法启动,因为应用程序的并行配置不正确。请参阅应用程序事件日志,或使用命令行sxstrace.exe工具”问题的处理方法。方法一:开始-运行(输入services.msc)-确定或回车,打开:服务(本地);我们在服务(本地)窗口找到:WindowsModulesInstaller服务,查看是否被禁用;3…如果WindowsModulesInstaller服务被禁用,我们必须把它更改为启用-手动,重启计算机,再安装应用程序。转载至https://blo

    2022年8月13日
    7
  • 优秀的有趣的博客

    优秀的有趣的博客昨晚和几个老同学小聚,聊得很开心。忘了到哪儿聊起一些牛人的博客,走得时候华仔还一直说要我一定记得把博客链接email给他。索性写个资源帖,记录一些我经常浏览的博客,并在此向所有提供,分享优秀资源的博主们致敬!也期待大家能留言推荐其他优秀的博客~大牛:刘未鹏http://mindhacks.cn/绝对的绝对的大牛,在大一时读到他的《我在南大的七年》,从此成了我和我身边很多朋友的必…

    2025年8月28日
    5

发表回复

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

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