• 【综合练习12】实现静态网页的相关功能


    登录:

    创建好相关的实体类,预先准备好数据库连接池及其业务处理成和dal层的登录方法,在web文件夹下创建与登录有关的方法,运行时保证相关数据库是开启状态。

    1. //用户实体类
    2. package itcast.domain;
    3. public class User {
    4. private int id;
    5. private String name;
    6. private String gender;
    7. private int age;
    8. private String address;
    9. private String qq;
    10. private String email;
    11. private String username;
    12. private String password;
    13. public String getUsername() {
    14. return username;
    15. }
    16. public void setUsername(String username) {
    17. this.username = username;
    18. }
    19. public String getPassword() {
    20. return password;
    21. }
    22. public void setPassword(String password) {
    23. this.password = password;
    24. }
    25. public int getId() {
    26. return id;
    27. }
    28. public void setId(int id) {
    29. this.id = id;
    30. }
    31. public String getName() {
    32. return name;
    33. }
    34. public void setName(String name) {
    35. this.name = name;
    36. }
    37. public String getGender() {
    38. return gender;
    39. }
    40. public void setGender(String gender) {
    41. this.gender = gender;
    42. }
    43. public int getAge() {
    44. return age;
    45. }
    46. public void setAge(int age) {
    47. this.age = age;
    48. }
    49. public String getAddress() {
    50. return address;
    51. }
    52. public void setAddress(String address) {
    53. this.address = address;
    54. }
    55. public String getQq() {
    56. return qq;
    57. }
    58. public void setQq(String qq) {
    59. this.qq = qq;
    60. }
    61. public String getEmail() {
    62. return email;
    63. }
    64. public void setEmail(String email) {
    65. this.email = email;
    66. }
    67. @Override
    68. public String toString() {
    69. return "User{" +
    70. "id=" + id +
    71. ", name='" + name + '\'' +
    72. ", gender='" + gender + '\'' +
    73. ", age=" + age +
    74. ", address='" + address + '\'' +
    75. ", qq='" + qq + '\'' +
    76. ", email='" + email + '\'' +
    77. ", username='" + username + '\'' +
    78. ", password='" + password + '\'' +
    79. '}';
    80. }
    81. }

    因为存在验证码,还需要写一个类设置验证码:

    1. package itcast.web.servlet;
    2. import javax.imageio.ImageIO;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.annotation.WebServlet;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import java.awt.*;
    9. import java.awt.image.BufferedImage;
    10. import java.io.IOException;
    11. import java.util.Random;
    12. /**
    13. * 验证码
    14. */
    15. @WebServlet("/checkCodeServlet")
    16. public class CheckCodeServlet extends HttpServlet {
    17. public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    18. //服务器通知浏览器不要缓存
    19. response.setHeader("pragma","no-cache");
    20. response.setHeader("cache-control","no-cache");
    21. response.setHeader("expires","0");
    22. //在内存中创建一个长80,宽30的图片,默认黑色背景
    23. //参数一:长
    24. //参数二:宽
    25. //参数三:颜色
    26. int width = 80;
    27. int height = 30;
    28. BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    29. //获取画笔
    30. Graphics g = image.getGraphics();
    31. //设置画笔颜色为白色
    32. g.setColor(Color.BLACK);
    33. //填充图片
    34. g.fillRect(0,0, width,height);
    35. //产生4个随机验证码,12Ey
    36. String checkCode = getCheckCode();
    37. //将验证码放入HttpSession中
    38. request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);
    39. //设置画笔颜色为darkslategray
    40. g.setColor(Color.WHITE);
    41. //设置字体的小大
    42. g.setFont(new Font("黑体",Font.BOLD,24));
    43. //向图片上写入验证码
    44. g.drawString(checkCode,15,25);
    45. //将内存中的图片输出到浏览器
    46. //参数一:图片对象
    47. //参数二:图片的格式,如PNG,JPG,GIF
    48. //参数三:图片输出到哪里去
    49. ImageIO.write(image,"PNG",response.getOutputStream());
    50. }
    51. /**
    52. * 产生4位随机字符串
    53. */
    54. private String getCheckCode() {
    55. String base = "0123456789ABCDEFGabcdefg";
    56. int size = base.length();
    57. Random r = new Random();
    58. StringBuffer sb = new StringBuffer();
    59. for(int i=1;i<=4;i++){
    60. //产生0到size-1的随机值
    61. int index = r.nextInt(size);
    62. //在base字符串中获取下标为index的字符
    63. char c = base.charAt(index);
    64. //将c放入到StringBuffer中去
    65. sb.append(c);
    66. }
    67. return sb.toString();
    68. }
    69. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    70. this.doGet(request,response);
    71. }
    72. }
    1. package itcast.web.servlet;
    2. import itcast.domain.User;
    3. import itcast.service.UserService;
    4. import itcast.service.impl.UserServiceImpl;
    5. import org.apache.commons.beanutils.BeanUtils;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.annotation.WebServlet;
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. import javax.servlet.http.HttpSession;
    12. import java.io.IOException;
    13. import java.lang.reflect.InvocationTargetException;
    14. import java.util.Map;
    15. @WebServlet("/loginServlet")
    16. public class LoginServlet extends HttpServlet {
    17. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    18. //1.设置编码
    19. request.setCharacterEncoding("utf-8");
    20. //2.获取数据
    21. //2.1获取用户填写验证码
    22. String verifycode = request.getParameter("verifycode");
    23. //3.验证码校验
    24. HttpSession session = request.getSession();
    25. String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
    26. session.removeAttribute("CHECKCODE_SERVER");//确保验证码一次性
    27. if(!checkcode_server.equalsIgnoreCase(verifycode)){
    28. //验证码不正确
    29. //提示信息
    30. request.setAttribute("login_msg","验证码错误!");
    31. //跳转登录页面
    32. request.getRequestDispatcher("/login.jsp").forward(request,response);
    33. return;
    34. }
    35. Map map = request.getParameterMap();
    36. //4.封装User对象
    37. User user = new User();
    38. try {
    39. BeanUtils.populate(user,map);
    40. } catch (IllegalAccessException e) {
    41. e.printStackTrace();
    42. } catch (InvocationTargetException e) {
    43. e.printStackTrace();
    44. }
    45. //5.调用Service查询
    46. UserService service = new UserServiceImpl();
    47. User loginUser = service.login(user);
    48. //6.判断是否登录成功
    49. if(loginUser != null){
    50. //登录成功
    51. //将用户存入session
    52. session.setAttribute("user",loginUser);
    53. //跳转页面
    54. response.sendRedirect(request.getContextPath()+"list.jsp");
    55. }else{
    56. //登录失败
    57. //提示信息
    58. request.setAttribute("login_msg","用户名或密码错误!");
    59. //跳转登录页面
    60. request.getRequestDispatcher("login.jsp").forward(request,response);
    61. }
    62. }
    63. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    64. this.doPost(request, response);
    65. }
    66. }

    登录相关的jsp:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. html>
    4. <html lang="zh-CN">
    5. <head>
    6. <meta charset="utf-8"/>
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    8. <meta name="viewport" content="width=device-width, initial-scale=1"/>
    9. <title>管理员登录title>
    10. <link href="css/bootstrap.min.css" rel="stylesheet">
    11. <script src="js/jquery-2.1.0.min.js">script>
    12. <script src="js/bootstrap.min.js">script>
    13. <script type="text/javascript">
    14. //切换验证码
    15. function refreshCode(){
    16. //1.获取验证码图片对象
    17. var vcode = document.getElementById("vcode");
    18. //2.设置其src属性,加时间戳
    19. vcode.src = "${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
    20. }
    21. script>
    22. head>
    23. <body>
    24. <div class="container" style="width: 400px;">
    25. <h3 style="text-align: center;">管理员登录h3>
    26. <form action="${pageContext.request.contextPath}/loginServlet" method="post">
    27. <div class="form-group">
    28. <label for="user">用户名:label>
    29. <input type="text" name="username" class="form-control" id="user" placeholder="请输入用户名"/>
    30. div>
    31. <div class="form-group">
    32. <label for="password">密码:label>
    33. <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
    34. div>
    35. <div class="form-inline">
    36. <label for="vcode">验证码:label>
    37. <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder="请输入验证码" style="width: 120px;"/>
    38. <a href="javascript:refreshCode();">
    39. <img src="${pageContext.request.contextPath}/checkCodeServlet" title="看不清点击刷新" id="vcode"/>
    40. a>
    41. div>
    42. <hr/>
    43. <div class="form-group" style="text-align: center;">
    44. <input class="btn btn btn-primary" type="submit" value="登录">
    45. div>
    46. form>
    47. <c:if test="${!empty login_msg}">
    48. <div class="alert alert-warning alert-dismissible" role="alert">
    49. <button type="button" class="close" data-dismiss="alert" >
    50. <span>×span>
    51. button>
    52. <strong>${login_msg}strong>
    53. div>
    54. c:if>
    55. div>
    56. body>
    57. <script>
    58. alert(${login_msg});
    59. script>
    60. html>

    与登录相关的方法:

    dal层:

    1. @Override
    2. public User findUserByUsernameAndPassword(String username, String password) {
    3. try {
    4. String sql = "select * from user001 where username = ? and password = ?";
    5. User user = template.queryForObject(sql, new BeanPropertyRowMapper(User.class), username, password);
    6. return user;
    7. } catch (Exception e) {
    8. e.printStackTrace();
    9. return null;
    10. }
    11. }

    其他的以及增删改查方法,学过三层架构和数据库连接池以及数据库查询的同学应该知道怎么写了。

    这次主要讲的是分页查询,并不是数据库端的简单的分页查询,而是能具体体现在web端的分页查询:

    呈现的效果如下:

    那我们要怎么做呢?

    首先要创建一个新的实体类,里面声明跟分页查询有关的变量例如总页码、每页数据等

    ①.创建pagebean对象(包含总页数、总页码、每页数据等数据)
    public class PageBean {
        private int totalCount; // 总记录数
        private int totalPage ; // 总页码
        private List list ; // 每页的数据
        private int currentPage ; //当前页码
        private int rows;//每页显示的记录数
    ……

    1. package itcast.domain;
    2. import java.util.List;
    3. /**
    4. * 分页工具对象
    5. */
    6. public class PageBean {
    7. private int totalCount; // 总记录数
    8. private int totalPage ; // 总页码
    9. private List list ; // 每页的数据
    10. private int currentPage ; //当前页码
    11. private int rows;//每页显示的记录数
    12. public int getTotalCount() {
    13. return totalCount;
    14. }
    15. public void setTotalCount(int totalCount) {
    16. this.totalCount = totalCount;
    17. }
    18. public int getTotalPage() {
    19. return totalPage;
    20. }
    21. public void setTotalPage(int totalPage) {
    22. this.totalPage = totalPage;
    23. }
    24. public List getList() {
    25. return list;
    26. }
    27. public void setList(List list) {
    28. this.list = list;
    29. }
    30. public int getCurrentPage() {
    31. return currentPage;
    32. }
    33. public void setCurrentPage(int currentPage) {
    34. this.currentPage = currentPage;
    35. }
    36. public int getRows() {
    37. return rows;
    38. }
    39. public void setRows(int rows) {
    40. this.rows = rows;
    41. }
    42. @Override
    43. public String toString() {
    44. return "PageBean{" +
    45. "totalCount=" + totalCount +
    46. ", totalPage=" + totalPage +
    47. ", list=" + list +
    48. ", currentPage=" + currentPage +
    49. ", rows=" + rows +
    50. '}';
    51. }
    52. }


    ②.创建servlet工具类:
    其中包含:
    1.获取参数
     String currentPage = request.getParameter("currentPage");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

    分页条件判断:

            if(currentPage == null || "".equals(currentPage)){

                currentPage = "1";
            }


            if(rows == null || "".equals(rows)){
                rows = "5";
            }

    2.获取条件查询参数:

     Map condition = request.getParameterMap();


     3.调用service查询
            UserService service = new UserServiceImpl();
            PageBean pb = service.findUserByPage(currentPage,rows,condition);

            System.out.println(pb);

      4.将PageBean存入request

            request.setAttribute("pb",pb);
            request.setAttribute("condition",condition);//将查询条件存入request
            //4.转发到list.jsp
            request.getRequestDispatcher("/list.jsp").forward(request,response);

    1. package itcast.web.servlet;
    2. import itcast.domain.PageBean;
    3. import itcast.domain.User;
    4. import itcast.service.UserService;
    5. import itcast.service.impl.UserServiceImpl;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.annotation.WebServlet;
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. import java.io.IOException;
    12. import java.util.Map;
    13. @WebServlet("/findUserByPageServlet")
    14. public class FindUserByPageServlet extends HttpServlet {
    15. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    16. request.setCharacterEncoding("utf-8");
    17. //1.获取参数
    18. String currentPage = request.getParameter("currentPage");//当前页码
    19. String rows = request.getParameter("rows");//每页显示条数
    20. if(currentPage == null || "".equals(currentPage)){
    21. currentPage = "1";
    22. }
    23. if(rows == null || "".equals(rows)){
    24. rows = "5";
    25. }
    26. //获取条件查询参数
    27. Map condition = request.getParameterMap();
    28. //2.调用service查询
    29. UserService service = new UserServiceImpl();
    30. PageBean pb = service.findUserByPage(currentPage,rows,condition);
    31. System.out.println(pb);
    32. //3.将PageBean存入request
    33. request.setAttribute("pb",pb);
    34. request.setAttribute("condition",condition);//将查询条件存入request
    35. //4.转发到list.jsp
    36. request.getRequestDispatcher("/list.jsp").forward(request,response);
    37. }
    38. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    39. this.doPost(request, response);
    40. }
    41. }

    ③相关方法:
    bll服务层:

     public PageBean findUserByPage(String _currentPage, String _rows, Map condition) {

            int currentPage = Integer.parseInt(_currentPage);
            int rows = Integer.parseInt(_rows);

            if(currentPage <=0) {
                currentPage = 1;
            }
            //1.创建空的PageBean对象
            PageBean pb = new PageBean();
            //2.设置参数
            pb.setCurrentPage(currentPage);
            pb.setRows(rows);

            //3.调用dao查询总记录数
            int totalCount = dao.findTotalCount(condition);
            pb.setTotalCount(totalCount);
            //4.调用dao查询List集合
            //计算开始的记录索引
            int start = (currentPage - 1) * rows;
            List list = dao.findByPage(start,rows,condition);
            pb.setList(list);

            //5.计算总页码
            int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
            pb.setTotalPage(totalPage);


            return pb;
        }

    Dao层:

     public int findTotalCount(Map condition) {
            //1.定义模板初始化sql
            String sql = "select count(*) from user001 where 1 = 1 ";
            StringBuilder sb = new StringBuilder(sql);
            //2.遍历map
            Set keySet = condition.keySet();
            //定义参数的集合
            List params = new ArrayList();
            for (String key : keySet) {

                //排除分页条件参数
                if("currentPage".equals(key) || "rows".equals(key)){
                    continue;
                }

                //获取value
                String value = condition.get(key)[0];
                //判断value是否有值
                if(value != null && !"".equals(value)){
                    //有值
                    sb.append(" and "+key+" like ? ");
                    params.add("%"+value+"%");//?条件的值
                }
            }
            System.out.println(sb.toString());
            System.out.println(params);

            return template.queryForObject(sb.toString(),Integer.class,params.toArray());
        }


      public List findByPage(int start, int rows, Map condition) {
            String sql = "select * from user001  WHERE 1 = 1 ";

            StringBuilder sb = new StringBuilder(sql);
            //2.遍历map
            Set keySet = condition.keySet();
            //定义参数的集合
            List params = new ArrayList();
            for (String key : keySet) {

                //排除分页条件参数
                if("currentPage".equals(key) || "rows".equals(key)){
                    continue;
                }

                //获取value
                String value = condition.get(key)[0];
                //判断value是否有值
                if(value != null && !"".equals(value)){
                    //有值
                    sb.append(" and "+key+" like ? ");
                    params.add("%"+value+"%");//?条件的值
                }
            }

            //添加分页查询
            sb.append(" limit ?,? ");
            //添加分页查询参数值
            params.add(start);
            params.add(rows);
            sql = sb.toString();
            System.out.println(sql);
            System.out.println(params);

            return template.query(sql,new BeanPropertyRowMapper(User.class),params.toArray());
        }

    而查询页面的样式和布局可以从boostrap官网上寻找喜欢的表单样式:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. html>
    4. <html lang="zh-CN">
    5. <head>
    6. <meta charset="utf-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1">
    9. <title>用户信息管理系统title>
    10. <link href="css/bootstrap.min.css" rel="stylesheet">
    11. <script src="js/jquery-2.1.0.min.js">script>
    12. <script src="js/bootstrap.min.js">script>
    13. <style type="text/css">
    14. td, th {
    15. text-align: center;
    16. }
    17. style>
    18. <script>
    19. function deleteUser(id){
    20. //用户安全提示
    21. if(confirm("您确定要删除吗?")){
    22. //访问路径
    23. location.href="${pageContext.request.contextPath}/delUserServlet?id="+id;
    24. }
    25. }
    26. window.onload = function(){
    27. //给删除选中按钮添加单击事件
    28. document.getElementById("delSelected").onclick = function(){
    29. if(confirm("您确定要删除选中条目吗?")){
    30. var flag = false;
    31. //判断是否有选中条目
    32. var cbs = document.getElementsByName("uid");
    33. for (var i = 0; i < cbs.length; i++) {
    34. if(cbs[i].checked){
    35. //有一个条目选中了
    36. flag = true;
    37. break;
    38. }
    39. }
    40. if(flag){//有条目被选中
    41. //表单提交
    42. document.getElementById("form").submit();
    43. }
    44. }
    45. }
    46. //1.获取第一个cb
    47. document.getElementById("firstCb").onclick = function(){
    48. //2.获取下边列表中所有的cb
    49. var cbs = document.getElementsByName("uid");
    50. //3.遍历
    51. for (var i = 0; i < cbs.length; i++) {
    52. //4.设置这些cbs[i]的checked状态 = firstCb.checked
    53. cbs[i].checked = this.checked;
    54. }
    55. }
    56. }
    57. script>
    58. head>
    59. <body>
    60. <div class="container">
    61. <h3 style="text-align: center">用户信息列表h3>
    62. <div style="float: left;">
    63. <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
    64. <div class="form-group">
    65. <label for="exampleInputName2">姓名label>
    66. <input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" >
    67. div>
    68. <div class="form-group">
    69. <label for="exampleInputName3">籍贯label>
    70. <input type="text" name="address" value="${condition.address[0]}" class="form-control" id="exampleInputName3" >
    71. div>
    72. <div class="form-group">
    73. <label for="exampleInputEmail2">邮箱label>
    74. <input type="text" name="email" value="${condition.email[0]}" class="form-control" id="exampleInputEmail2" >
    75. div>
    76. <button type="submit" class="btn btn-default">查询button>
    77. form>
    78. div>
    79. <div style="float: right;margin: 5px;">
    80. <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人a>
    81. <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中a>
    82. div>
    83. <form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">
    84. <table border="1" class="table table-bordered table-hover">
    85. <tr class="success">
    86. <th><input type="checkbox" id="firstCb">th>
    87. <th>编号th>
    88. <th>姓名th>
    89. <th>性别th>
    90. <th>年龄th>
    91. <th>籍贯th>
    92. <th>QQth>
    93. <th>邮箱th>
    94. <th>操作th>
    95. tr>
    96. <c:forEach items="${pb.list}" var="user" varStatus="s">
    97. <tr>
    98. <td><input type="checkbox" name="uid" value="${user.id}">td>
    99. <td>${s.count}td>
    100. <td>${user.name}td>
    101. <td>${user.gender}td>
    102. <td>${user.age}td>
    103. <td>${user.address}td>
    104. <td>${user.qq}td>
    105. <td>${user.email}td>
    106. <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改a> 
    107. <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除a>td>
    108. tr>
    109. c:forEach>
    110. table>
    111. form>
    112. <div>
    113. <nav aria-label="Page navigation">
    114. <ul class="pagination">
    115. <c:if test="${pb.currentPage == 1}">
    116. <li class="disabled">
    117. c:if>
    118. <c:if test="${pb.currentPage != 1}">
    119. <li>
    120. c:if>
    121. <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
    122. <span aria-hidden="true">«span>
    123. a>
    124. li>
    125. <c:forEach begin="1" end="${pb.totalPage}" var="i" >
    126. <c:if test="${pb.currentPage == i}">
    127. <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}a>li>
    128. c:if>
    129. <c:if test="${pb.currentPage != i}">
    130. <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}a>li>
    131. c:if>
    132. c:forEach>
    133. <li>
    134. <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
    135. <span aria-hidden="true">»span>
    136. a>
    137. li>
    138. <span style="font-size: 25px;margin-left: 5px;">
    139. 共${pb.totalCount}条记录,共${pb.totalPage}页
    140. span>
    141. ul>
    142. nav>
    143. div>
    144. div>
    145. body>
    146. html>

    注:

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    此条语句要导入JSTL的jar包。

    jstl有什么作用?

    1、以一种统一的方式减少了JSP中的Scriptlets代码数量,可以达到程序中没有任何Scriptlest代码

    2、将业务封装到JSTL可以很方便的重用。

    3、将数据与显示分离。

    4、简化了JSP和Web应用程序的开发,并且使得JSP页面的编程风格统一、易于维护。

    5、允许JSP设计工具与Web应用程序开发的进一步集成

    例如本次综合练习的修改

    ……

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. html>
    4. <html lang="zh-CN">
    5. <head>
    6. <meta charset="utf-8">
    7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    8. <meta name="viewport" content="width=device-width, initial-scale=1">
    9. <title>修改用户title>
    10. <link href="css/bootstrap.min.css" rel="stylesheet">
    11. <script src="js/jquery-2.1.0.min.js">script>
    12. <script src="js/bootstrap.min.js">script>
    13. head>
    14. <body>
    15. <div class="container" style="width: 400px;">
    16. <h3 style="text-align: center;">修改联系人h3>
    17. <form action="${pageContext.request.contextPath}/updateUserServlet" method="post">
    18. <input type="hidden" name="id" value="${user.id}">
    19. <div class="form-group">
    20. <label for="name">姓名:label>
    21. <input type="text" class="form-control" id="name" name="name" value="${user.name}" readonly="readonly" placeholder="请输入姓名" />
    22. div>
    23. <div class="form-group">
    24. <label>性别:label>
    25. <c:if test="${user.gender == '男'}">
    26. <input type="radio" name="gender" value="男" checked />
    27. <input type="radio" name="gender" value="女" />
    28. c:if>
    29. <c:if test="${user.gender == '女'}">
    30. <input type="radio" name="gender" value="男" />
    31. <input type="radio" name="gender" value="女" checked />
    32. c:if>
    33. div>
    34. <div class="form-group">
    35. <label for="age">年龄:label>
    36. <input type="text" class="form-control" value="${user.age}" id="age" name="age" placeholder="请输入年龄" />
    37. div>
    38. <div class="form-group">
    39. <label for="address">籍贯:label>
    40. <select name="address" id="address" class="form-control" >
    41. <c:if test="${user.address =='四川'}">
    42. <option value="陕西" selected>陕西option>
    43. <option value="北京">北京option>
    44. <option value="上海">上海option>
    45. <option value="大理">大理option>
    46. <option value="四川">四川option>
    47. c:if>
    48. <c:if test="${user.address =='大理'}">
    49. <option value="陕西" selected>陕西option>
    50. <option value="北京">北京option>
    51. <option value="上海">上海option>
    52. <option value="大理">大理option>
    53. <option value="四川">四川option>
    54. c:if>
    55. <c:if test="${user.address == '北京'}">
    56. <option value="陕西" selected>陕西option>
    57. <option value="四川">四川option>
    58. <option value="上海">上海option>
    59. <option value="大理">大理option>
    60. <option value="北京">北京option>
    61. c:if>
    62. <c:if test="${user.address == '上海'}">
    63. <option value="陕西" selected>陕西option>
    64. <option value="四川">四川option>
    65. <option value="上海">上海option>
    66. <option value="大理">大理option>
    67. <option value="北京">北京option>
    68. c:if>
    69. <c:if test="${user.address =='陕西'}">
    70. <option value="陕西" selected>陕西option>
    71. <option value="北京">北京option>
    72. <option value="上海">上海option>
    73. <option value="大理">大理option>
    74. <option value="四川">四川option>
    75. c:if>
    76. <c:if test="${user.address !=null}">
    77. <option value="陕西" selected>陕西option>
    78. <option value="北京">北京option>
    79. <option value="上海">上海option>
    80. <option value="大理">大理option>
    81. <option value="四川">四川option>
    82. c:if>
    83. select>
    84. div>
    85. <div class="form-group">
    86. <label for="qq">QQ:label>
    87. <input type="text" id="qq" class="form-control" value="${user.qq}" name="qq" placeholder="请输入QQ号码"/>
    88. div>
    89. <div class="form-group">
    90. <label for="email">Email:label>
    91. <input type="text" id="email" class="form-control" value="${user.email}" name="email" placeholder="请输入邮箱地址"/>
    92. div>
    93. <div class="form-group" style="text-align: center">
    94. <input class="btn btn-primary" type="submit" value="提交" />
    95. <input class="btn btn-default" type="reset" value="重置" />
    96. <input class="btn btn-default" type="button" value="返回"/>
    97. div>
    98. form>
    99. div>
    100. body>
    101. html>

    数据:

    书写本次综合练习需要的jar包如下:

     

     

  • 相关阅读:
    【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
    加州法案提议在州一级监管人工智能
    Git 版本控制
    【Web】浅聊Hessian异常toString姿势学习&复现
    Maven的安装与配置(详细版)
    如何彻底禁止 macOS Monterey 自动更新,去除更新标记和通知
    CNNVD发布微软多个安全漏洞,涉高危及以上等级漏洞56个
    CLion 2023:专注于C和C++编程的智能IDE mac/win版
    EasyCVR服务private.pem文件被清空,导致无法正常启动该如何处理?
    VBA_MF系列技术资料1-192
  • 原文地址:https://blog.csdn.net/akisi/article/details/126260319