尚筹网用户信息维护模块-更新用户

更新当前登录用户的信息

目标

修改现有的Admin的数据,不修改密码

思路

首先要回显数据,即在数据库中查询当前用户的数据
点击按钮------------------->AdminHandler.getAdminById(Inter admin);
点击修改-------------------->AdminHandler.update(Admin admin)-------------->AdminService.update()------------->AdminMapper.updateByPrimaryKeySelective(admin)
与增的不同处就是回显表单

代码

回显表单部分

修改admin-page部分
修改前

<button type="button" class="btn btn-primary btn-xs">
														<i class=" glyphicon glyphicon-pencil"></i>
													</button>

修改后

	<a href="admin/to/edit/page.html?adminId=${admin.id }&pageNum=${requestScope.pageInfo.pageNum }&keyword=${param.keyword }" class="btn btn-primary btn-xs"  ><i class=" glyphicon glyphicon-pencil"></i></a>			

AdminHandler.java

	@RequestMapping("/admin/to/edit/page.html")
	public String toEditPage(
			@RequestParam("adminId") Integer adminId,
			Model model
			) {
		
		// 1.根据adminId查询Admin对象
		Admin admin = adminService.getAdminById(adminId);
		// 2.将Admin对象存入模型
		model.addAttribute("admin",admin);
				return "admin-edit";
		
	}

AdminService.java

	Admin getAdminById(Integer adminId);

AdminServiceImpl.java

@Override
	public Admin getAdminById(Integer adminId) {
	
		
		return adminMapper.selectByPrimaryKey(adminId);
	
	}

增加admin-edit.js

 <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
				<ol class="breadcrumb">
				  <li><a href="admin/to/main/page.html">首页</a></li>
				  <li><a href="admin/get/page.html">数据列表</a></li>
				  <li class="active">更新</li>
				</ol>
			<div class="panel panel-default">
              <div class="panel-heading">表单数据<div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-question-sign"></i></div></div>
			  <div class="panel-body">
				<form action="admin/update.html" method="post" role="form">
				<input type="hidden" name="id" value="${requestScope.admin.id }"/>
				<input type="hidden" name="pageNum" value="${param.pageNum }"/>
				<input type="hidden" name="keyword" value="${param.keyword }"/>
				<p>${requestScope.exception.message }</p>
				  <div class="form-group">
					<label for="exampleInputPassword1">登陆账号</label>
					<input 
					name="loginAcct"
					value="${requestScope.admin.loginAcct }"
					type="text" class="form-control" id="exampleInputPassword1" placeholder="请输入登陆账号">
				  </div>
				
				  <div class="form-group">
					<label for="exampleInputPassword1">用户昵称</label>
					<input 
					name="userName"
					value="${requestScope.admin.userName }"
					type="text" class="form-control" id="exampleInputPassword1" placeholder="请输入用户昵称">
				  </div>
				  <div class="form-group">
					<label for="exampleInputEmail1">邮箱地址</label>
					<input 
					name="email"
				    value="${requestScope.admin.email }"
					type="email" class="form-control" id="exampleInputEmail1" placeholder="请输入邮箱地址">
					<p class="help-block label label-warning">请输入合法的邮箱地址, 格式为: xxxx@xxxx.com</p>
				  </div>
				  <button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-edit"></i> 更新</button>
				</form>
			  </div>
			</div>
        </div>

配置view-contronller视图
spring-web-mvc.xml中

<mvc:view-controller path="/admin/to/edit/page.html" view-name="admin-edit"/>

修改用户信息部分代码

admin-edit.js

<form action="admin/update.html" method="post" role="form">

AdminHandler.java

	@RequestMapping("/admin/update.html")
	public String update(
			Admin admin,
			@RequestParam("pageNum") Integer pageNum,
			@RequestParam("keyword") String  keyword
			) {
		adminService.update(admin);
		return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
		
	}
	

AdminService.java

	void update(Admin admin);

AdminServiceImpl.java

public void update(Admin admin) {
		//有选择的更新,对于null值的字段不更新
		try {
			adminMapper.updateByPrimaryKeySelective(admin);
		} catch (Exception e) {
			e.printStackTrace();

			logger.info("异常全类名="+e.getClass().getName());
			
			if(e instanceof DuplicateKeyException) {
				throw new LoginAcctAlreadyInUseForUpdateException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
			}
		}
		
	}

此时抛出修改用户名唯一异常,与新增用户名时相似,但新增时异常跳转页面在新增的页面,所以
新建异常映射类

异常处理器中声明对应的处理方法
LoginAcctAlreadyInUseForUpdateException

public class LoginAcctAlreadyInUseForUpdateException extends RuntimeException{
	private static final long serialVersionUID = 1L;

	public LoginAcctAlreadyInUseForUpdateException() {
		super();
		
	}

	public LoginAcctAlreadyInUseForUpdateException(String message, Throwable cause, boolean enableSuppression,
			boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
		
	}

	public LoginAcctAlreadyInUseForUpdateException(String message, Throwable cause) {
		super(message, cause);
		
	}

	public LoginAcctAlreadyInUseForUpdateException(String message) {
		super(message);
		
	}

	public LoginAcctAlreadyInUseForUpdateException(Throwable cause) {
		super(cause);
	
	}

	


}

CrowdExceptionResolver.java

	@ExceptionHandler(value = LoginAcctAlreadyInUseForUpdateException.class)
	public ModelAndView resolveLoginAcctAlreadyInUseForUpdateException(
			LoginAcctAlreadyInUseException exception,
			HttpServletRequest request,
			HttpServletResponse response
			) throws IOException {
		
		String viewName = "system-error";
		
		return commonResolve(viewName, exception, request, response);
	}

最终效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值