标签:beanutils and turn ted info dog class targe encoding
1、流程图
因为在定义的JavaBean中没有与之对应的实体(学生姓氏、性别),因此需要创建一个VO(只有姓氏和性别),将表单提交的数据封装到VO实体中,再封装到MAP集合中,将该实体作为参数查询对应的学生集合。因为有两个条件(姓氏和性别),因此,需要用到多条件查询。
2、核心代码
(1)表单页面
<form id="Form1" name="Form1" action="${pageContext.request.contextPath}/studentselectservlet" method="get"> 学生姓氏:<input type="text" name="sname" value="${studentlist.sname }"> 学生性别:<select id="sex" name="sex"> <option value="">不限</option> <option value="男">男</option> <option value="女">女</option> <input type="submit" value="搜索">
(2)Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Map<String, String[]> properties = request.getParameterMap(); Condition condition = new Condition(); try { BeanUtils.populate(condition, properties); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } StudentService studentService=new StudentService(); List<Student> studentList = null; try { studentList = studentService.StudentSelectService(condition);/ } catch (SQLException e) { e.printStackTrace(); } request.setAttribute("studentList", studentList); request.getRequestDispatcher("/student/list.jsp").forward(request,response); }
(3)Dao层:
public List<Student> selectStudent(Condition condition) throws SQLException { Connection con=null; con = C3p0Utils.getConnection(); QueryRunner qr = new QueryRunner(); List<String> list = new ArrayList<String>(); String sql = "select * from student where 1=1"; if(condition.getSname()!=null&&!condition.getSname().trim().equals("")){ sql+=" and sname like ? "; list.add(condition.getSname().trim()+"%"); } if(condition.getSex()!=null&&!condition.getSex().trim().equals("")){ sql+=" and sex=? "; list.add(condition.getSex().trim()); } List<Student> studentList = qr.query(con,sql, new BeanListHandler<Student>(Student.class) , list.toArray()); return studentList; }
(4)jsp:
<c:forEach items="${studentList}" var="stu" varStatus="vs"> <%--forEach语句实现学生信息的罗列,var表示一个迭代的变量,items表示迭代的集合--%> <tr onmouseover="this.style.backgroundColor = ‘white‘" onmouseout="this.style.backgroundColor = ‘#F5FAFE‘;"> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="6%">${stu.studentno}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="6%">${stu.sname}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="15%">${stu.classno}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="15%">${stu.birthday}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="5%">${stu.sex}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="10%">${stu.phone}</td> <td style="CURSOR: hand; HEIGHT: 22px" align="center" width="10%">${stu.point}</td>
3、结果
(1)数据库:
(2)查询结果:
标签:beanutils and turn ted info dog class targe encoding
原文地址:https://www.cnblogs.com/zhai1997/p/12244073.html