修改功能
分析
findUserServlet类
- 获取用户id
- 根据id查询用户信息User
- 将user对象存到request
- 转发到update.jsp页面
UpdateIserServlet
- 设置编码
- 获取表单数据 map
- 封装User对象
- 调用Service完成修改
- 跳转到查询所有Servlet
代码
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="utf-8"> <title>修改用户</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面 任何其他内容都*必须*跟随其后--> <title>首页</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- jQuery(Bootstrap的所有javaScrpt插件都依赖) --> <script src="js/bootstrap.min.js"></script> <!-- 加载Bootstrap 的所有 javaScript插件 你也可以根据需要只加载单个插件 --> <script src="./js/bootstrap.js"></script> <script src="text/javascript"></script> </head> <body> <form action="" method="post"> <div class="form-group"> <label for="name">姓名:</label> <input type="text" class="form-control" id="name" name="name" value="${user.name}" readonly="readonly" placeholder="请输入姓名" /> </div> <div class="form-group"> <label>性别:</label> <c:if test="${user.gender == '男'}"> <input type="radio" name="gender" value="男" checked/>男 <input type="radio" name="gender" value="女" />女 </c:if> <c:if test="${user.gender == '女'}"> <input type="radio" name="gender" value="男" />男 <input type="radio" name="gender" value="女" checked/>女 </c:if> </div> <div class="form-group"> <label for="age">年龄:</label> <input type="text" class="form-control" id="age" name="age" value="${user.age}" placeholder="请输入年龄" /> </div> <div class="form-group"> <label for="address">籍贯:</label> <select name="address" class="form-control" id="address"> <c:if test="${user.address == '陕西'}"> <option value="陕西" selected>陕西</option> <option value="北京">北京</option> <option value="上海">上海</option> </c:if> <c:if test="${user.address == '北京'}"> <option value="陕西">陕西</option> <option value="北京" selected>北京</option> <option value="上海">上海</option> </c:if> <c:if test="${user.address == '上海'}"> <option value="陕西" >陕西</option> <option value="北京">北京</option> <option value="上海" selected>上海</option> </c:if> </select> </div> <div class="form-group"> <label for="qq">QQ:</label> <input type="text" class="form-control" id="qq" name="qq" value="${user.qq}" placeholder="请输入QQ号码"/> </div> <div class="form-group"> <label for="email">Email:</label> <input type="text" class="form-control" id="email" name="email" value="${user.email}" placeholder="请输入邮箱地址"/> </div> <div class="form-group" style="text-align: center"> <input class="btn btn-primary" type="submit" value="提交" /> <input class="btn btn-default" type="reset" value="重置" /> <input class="btn btn-default" type="button" value="返回"/> </div> </form> </div> </body> </html>
UserDao接口
User findById(int parseInt);
UserDaoImpl类
public User findById(int parseInt) { //1、定义sql String sql = "select * from user where id = ?"; User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),parseInt); return user; }
UserService接口
User findUserById(String id);
UserServiceImpl类
public User findUserById(String id) { return dao.findById(Integer.parseInt(id)); }
FindUserServlet类
package com.bai.web.servlet; import com.bai.domain.User; import com.bai.service.UserService; import com.bai.service.img.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class FindUserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、获取id String id = request.getParameter("id"); //2、调用Service查询 UserService service = new UserServiceImpl(); User user = service.findUserById(id); //3、将user存入request request.setAttribute("user",user); //4、转发到update.jsp request.getRequestDispatcher("/update.jsp").forward(request,response); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }标签:功能,request,修改,user,import,servlet,id,User From: https://www.cnblogs.com/aimz01/p/16598101.html