时间:2021-07-01 10:21:17 帮助过:21人阅读
方法modelAutoBindtoIndex作用是当浏览器输入http://localhost:8080/MoneyRecordSys/index的时候,向view层返回显示index.jsp的响应,而方法modelAutoBindtoLogin是当用户从index.jsp页面
post回一些数据的时候,控制器映射到的该方法负责进行逻辑处理,并且再次向View层返回View下一步要进行的响应,即应该继续显示哪个页面,此处为showUser1.jsp,并且将list这个对象传到该JSP当中,list[0]记录当前
用户信息,list[1]里头包涵另个list,这个list内容是网页当中所有用户的数据。
一下是index.jsp代码:
<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
</style>
</head>
<body>
<!-- 控制格式,纵向居中 -->
<table width="500" height="195" align="center">
<tr>
<td> </td>
</tr>
</table>
<!--标签中的name和ID是两个概念,name面向后台,id面向jsp -->
<form method="post">
<table width="1500" height="570" align="center" background="images/2.png">
<tr>
<td colspan="2"> </td>
<td>${error}</td>
</tr>
<tr>
<td width="500"> </td>
<td width=136><span style="font-size:30pt;font-family:宋体;font-weight:bold">用户名</span></td>
<td width="404"><input name="id" type="text" style="height:35px;width:350px"></td>
</tr>
<tr>
<td> </td>
<td><span style="font-size:30pt;font-family:宋体;font-weight:bold">密码</span></td>
<td><input name="pwd" type="password" style="height:35px;width:350px"></td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center">
<input type="submit" value="login" style="height:50px;width:200px;font-size:18pt;font-family:Calibri;font-weight:bold"></td>
</tr>
</table>
</form>
<br>
</body>
</html>
此处补充说明一个点:在jsp当中,我们可以看到标签中的name和ID是两个概念,name是面向后台,id面向的则是jsp,在index.jsp我们将用户输入的用户名和密码以 name="id"和name="pwd"
POST回控制器对应的接受方法 public ModelAndView modelAutoBindtologin(HttpServletRequest request,UserInfo UserInfo),形参中并没有HttpServletResponse而是UserInfo是因为
UserInfo这个POJO类当中,正好有两个字段名为id和pwd,所以index.jsp POST回来的用户名和密码则自动解析到了UserInfo当中。
当用户以管理员的权限登录成功之后,进入到主界面当中,并且可以向网页的注册用户信息进行一系列的操作,这一系列的操作,我们分化出一个UserOperationController控制器进行管理和监控,包括
添加用户,删除用户,更改用户的信息等操作。
package Fmu.wellhold.controller; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import Fmu.wellhold.dao.ITF.UserInfoDaoITF; import Fmu.wellhold.dao.pojo.UserInfo; @Controller public class UserOperationController { @Resource private UserInfoDaoITF UserInfoService; @Resource private FileUpload fileUpload;//注入则不需要new了 @RequestMapping(value="showUser1",method={RequestMethod.GET}) public ModelAndView showUser(HttpServletRequest request,HttpServletResponse response) { String number = request.getParameter("number"); List list=new ArrayList<>(); list.add(UserInfoService.findByld(Integer.valueOf(number))); // System.out.println("showUser控制器已启动"); list.add(UserInfoService.findAll()); return new ModelAndView("showUser1","list",list); } @RequestMapping(value="updateTheUser",method={RequestMethod.GET}) public ModelAndView getToupdateTheUser(HttpServletRequest request,HttpServletResponse response) throws Exception { String number = request.getParameter("number"); String user= request.getParameter("user"); // System.out.println("updateTheUser,已经启动控制器"); List list=new ArrayList<>(); UserInfo user1=(UserInfo)UserInfoService.findByld(Integer.valueOf(user)); list.add(user1); list.add(UserInfoService.findByld(Integer.valueOf(number))); return new ModelAndView("updateTheUser","list",list); } @RequestMapping(value="postTheUpdateUserInfo",method={RequestMethod.POST}) public ModelAndView updateTheUserInfo(HttpServletRequest request,HttpServletResponse response) throws Exception { Map<String, String> map = fileUpload.upload(request, response); // System.out.println(map.toString()); UserInfo user = new UserInfo(); user.setId(map.get("id")); user.setPwd(map.get("pwd")); user.setName(map.get("name")); user.setSex(map.get("sex")); // user.setBirthday(map.get("birthday")); user.setCome(map.get("come")); user.setRemark(map.get("remark")); user.setNumber(Integer.valueOf(map.get("number").toString())); // System.out.println(user.toString()); UserInfoService.update(user); // return null; return showUser(request, response); } @RequestMapping(value="deleteUser",method={RequestMethod.GET}) public ModelAndView deleteUser(HttpServletRequest request,HttpServletResponse response) throws Exception { String number=request.getParameter("deleteUserNum"); // System.out.println("deleteUser"+number); UserInfoService.delete(Integer.valueOf(number)); return showUser(request, response); } @RequestMapping(value="insertTheUser",method={RequestMethod.GET}) public ModelAndView getToInsertUser(HttpServletRequest request,HttpServletResponse response) throws Exception { String number = request.getParameter("number"); // System.out.println("insertTheUser,已经启动控制器"); List list=new ArrayList<>(); UserInfo user1=(UserInfo)UserInfoService.findByld(Integer.valueOf(number)); list.add(user1); return new ModelAndView("insertTheUser","list",list); } @RequestMapping(value="postTheInsertUserInfo",method={RequestMethod.POST}) public ModelAndView postTheInsertUserInfo(HttpServletRequest request,HttpServletResponse response) throws Exception { // System.out.println("postTheInsertUserInfo控制器已启动"); Map<String, String> map = fileUpload.upload(request, response); // System.out.println(map.toString()); if (map != null) { UserInfo user = new UserInfo(); user.setId(map.get("id")); user.setPwd(map.get("pwd")); user.setName(map.get("name")); user.setSex(map.get("sex")); user.setCome(map.get("come")); user.setRemark(map.get("remark")); UserInfoService.insert(user); } else { PrintWriter out = response.getWriter(); out.print("<script type=‘text/javascript‘>"); out.print("alert(‘你提交的新建用户为空‘);"); out.print("window.history.go(-1);"); out.print("</script>"); out.close(); } return showUser(request, response); } }
未完待续。。。
SpringMVC + Hibernate + MySQL 的简易网页搭建(Control实现篇)
标签:span bsp erro 注册 ann char model 需求 content