springmvc 以不同的跳转方式实现增删改查

优势:可以只用一个URL
不同功能对应的方式:
1.用户信息列表 get
2. 添加用户 add post
3.删除 delete delete
4.修改update put

列表

Controller层

@RequestMapping(value="/manage",method= {RequestMethod.GET})
	public String List(Map<String,Object> map) {
		map.put("userList",userService.getSUList());
		return "user/list";

增加

Controller层

	@RequestMapping(value = "/manage", method= {RequestMethod.POST})
	public String add(UserInfo userInfo) {
		System.out.println("===============>" + userInfo);
		userService.add(userInfo);
		return "redirect:manage";

在jsp中直接使用表单提交即可

修改

Controller层

@RequestMapping(value = "/manage", method= {RequestMethod.PUT})
	public String editUser(UserInfo userInfo) {
		
		System.out.println("=======editUser==========>" + userInfo);
		userService.update(userInfo);
		return "redirect:manage";
	}

在jsp的表单中应加入<input type="hidden" name="_method" value="put"> 才能以put方式提交
表单<form action="${pageContext.request.contextPath }/userInfo/manage" method="post">依旧写为post 注意:在表单将post改为put无用
表单的常用提交方式:1) GET 浏览器将提交表单中的字段信息放置在请求头中,不适合大数据量的传输,
适合根据关键字作查询。
2) POST
浏览器将提交表单中的字段信息放置在请求体中,适合大数据量的传输,
如:向服务器端上传文件等。
注意:
1) 若直接在地址栏中输入URL按回车,按GET方式提交.
2) 若属性method不指定任何值或者不加属性method默认按GET方式提交.
3) 只有在method指定为POST时才按POST方式提交

删除

Controller层

@RequestMapping(value = "/manage", method= {RequestMethod.DELETE})
	public String deleteUser(int userId) {
		userService.delect(userId);
		return "redirect:manage";
	}

在list.jsp页面中需要使用javascript

<c:forEach items="${userList }" var="user">
		<tr>
				<td>${user.userId } </td>
				<td>${user.userName } </td>
				<td>${user.passwd } </td>
				<td>${user.salt } </td>
				<td>${user.createTime } </td>
				<td><a href="javascript:deleteUserInfo(${user.userId })">删除</a></td>
				<td><a href="/springboot.mvc/views/user/update.jsp?userId=${user.userId}">修改</a></td>
		</tr>
		
		</c:forEach>
	
	
	</table>
	
	
	</div>
	<from id="deleteUserFrom" action="${pageContext.request.contextPath }/userInfo/manage" method="post">
		<input type="hidden" name="_method" value="delete">
		<input type="hidden" id="userId" name="userId">
	//独立建立一个表单以delete方式提交
	</from>
	<script type="text/javascript">
	
		function deleteUserInfo(userId){
			alert("----------->" + userId);
			var delFrom = document.getElementById("deleteUserForm");
			document.getElementById("userId").value = userId;
			delFrom.submit();
		}
发布了35 篇原创文章 · 获赞 0 · 访问量 669

猜你喜欢

转载自blog.csdn.net/Azadoo/article/details/104185531
今日推荐