尚筹网:新增用户

给t_admin表的login_acct字段添加唯一约束

ALTER TABLE `t_admin`
ADD UNIQUE INDEX (`login_acct`)

流程分析

在这里插入图片描述

跳转到新增表单页面

把新增按钮改成超链接

/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/admin-page.jsp

修改前

<button type="button" class="btn btn-primary"
							style="float: right;" onclick="window.location.href='add.html'">
							<i class="glyphicon glyphicon-plus"></i> 新增
						</button>

修改后

<a href="admin/to/add/page.html" class="btn btn-primary" style="float: right;"><i class="glyphicon glyphicon-plus"></i> 新增</a>

view-controller配置

/atcrowdfunding-admin-1-webui/src/main/resources/spring-web-mvc.xml

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

创建admin-add.jsp

/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/admin-add.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="UTF-8">
<%@ include file="/WEB-INF/include-head.jsp"%>
<body>

	<%@ include file="/WEB-INF/include-nav.jsp"%>
	<div class="container-fluid">
		<div class="row">
			<%@ include file="/WEB-INF/include-sidebar.jsp"%>
			<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/query/for/search.html">数据列表</a></li>
					<li class="active">新增</li>
				</ol>
				<div class="panel panel-default">
					<div class="panel-heading">
						表单数据
					</div>
					<div class="panel-body">
						<form action="admin/save.html" method="post" role="form">
							<div class="form-group">
								<label for="exampleInputPassword1">登录账号</label>
								<input
									type="text" 
									name="loginAcct"
									class="form-control" 
									id="exampleInputPassword1"
									placeholder="请输入登陆账号"/>
							</div>
							<div class="form-group">
								<label for="exampleInputPassword1">登录密码</label>
								<input
									type="text" 
									name="userPswd"
									class="form-control" 
									id="exampleInputPassword1"
									placeholder="请输入登录密码"/>
							</div>
							<div class="form-group">
								<label for="exampleInputPassword1">用户昵称</label>
								<input
									type="text" 
									name="userName"
									class="form-control" 
									id="exampleInputPassword1"
									placeholder="请输入用户昵称"/>
							</div>
							<div class="form-group">
								<label for="exampleInputEmail1">邮箱地址</label>
								<input 
									type="email"
									name="email"
									class="form-control" 
									id="exampleInputEmail1"
									placeholder="请输入邮箱地址"/>
							</div>
							<button type="submit" class="btn btn-success">
								<i class="glyphicon glyphicon-plus"></i> 新增
							</button>
							<button type="reset" class="btn btn-danger">
								<i class="glyphicon glyphicon-refresh"></i> 重置
							</button>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>

</body>
</html>

执行保存操作

handler方法

/atcrowdfunding-admin-2-component/src/main/java/com/atguigu/crowd/funding/handler/AdminHandler.java

// 使用Admin实体类对象封装表单提交的请求参数,具体每一个请求参数会通过对应的setXxx()方法注入实体类
@RequestMapping("/admin/save")
public String saveAdmin(Admin admin) {
	
	adminService.saveAdmin(admin);
	
	return "redirect:/admin/query/for/search.html";
}

service方法


	@Override
	public void saveAdmin(Admin admin) {
		
		// 对密码进行加密
		String userPswd = admin.getUserPswd();
		userPswd = CrowdFundingUtils.md5(userPswd);
		admin.setUserPswd(userPswd);
		
		// 执行保存
		adminMapper.insert(admin);
		
	}

转换抛出的异常

	// 使用Admin实体类对象封装表单提交的请求参数,具体每一个请求参数会通过对应的setXxx()方法注入实体类
	@RequestMapping("/admin/save")
	public String saveAdmin(Admin admin) {
		
		try {
			adminService.saveAdmin(admin);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof DuplicateKeyException) {
				throw new RuntimeException(CrowdFundingConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
			}
		}
		
		return "redirect:/admin/query/for/search.html";
	}
	

操作完成之后立即看到新增的记录

方案一

跳转到分页页面时前往最后一页

return "redirect:/admin/query/for/search.html?pageNum="+Integer.MAX_VALUE;

方案二

分页页面显示数据时根据id降序排列

<select id="selectAdminListByKeyword" resultMap="BaseResultMap">
	select
	<include refid="Base_Column_List" />
	from t_admin
	WHERE
		login_acct LIKE CONCAT("%", #{keyword}, "%")
	OR user_name LIKE CONCAT("%", #{keyword}, "%")
	OR email LIKE CONCAT("%", #{keyword}, "%")
	order by id desc
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值