执行查询前:
查询后:
jsp页面:
<form action="servlet/EmployeeServlet?method=findEmp" method="post">
<ul class="prosearch">
<li>
<label>查询:</label><i>用户名</i>
<a>
<input name="empId" type="text" class="scinput" value="${empId }"/>
</a><i>所属部门</i>
<a> <select class="select1" name="deptno">
<option value="0">--全部--</option>
<c:forEach items="${deptList }" var="dept">
<c:if test="${dept.deptno == deptno}">
<option value="${dept.deptno }" selected="selected">${dept.deptName }</option>
</c:if>
<c:if test="${dept.deptno != deptno}">
<option value="${dept.deptno }">${dept.deptName }</option>
</c:if>
</c:forEach>
</select>
</a>
</li>
<li>
<label>是否在职:</label>
<c:if test="${onDuty ==1 }">
<input name="onDuty" type="radio" value="1" checked="checked"/> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
<c:if test="${onDuty ==0 }">
<input name="onDuty" type="radio" value="1" /> 是
<input name="onDuty" type="radio" value="0" checked="checked"/> 否
</c:if>
<c:if test="${empty onDuty }">
<input name="onDuty" type="radio" value="1" checked="checked" /> 是
<input name="onDuty" type="radio" value="0" /> 否
</c:if>
</li>
<li>
<label>入职时间:</label>
<a>
<input name="hireDate" type="text" class="scinput" onfocus="WdatePicker()" value="${hireDate} "/>
</a>
</li>
<a>
<input name="" type="submit" class="sure" value="查询" />
</a>
</ul>
</form>
servlet实现
public void findEmp(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收查询条件
String empId = request.getParameter("empId");
int deptno = 0;
String sdeptno = request.getParameter("deptno"); //null
try{
deptno = Integer.parseInt(sdeptno);
}catch(NumberFormatException e){
e.printStackTrace();
}
int onDuty = 1;
String sonDuty = request.getParameter("onDuty");//null
try{
onDuty = Integer.parseInt(sonDuty);
}catch(NumberFormatException e){
e.printStackTrace();
}
String shireDate = request.getParameter("hireDate");//null
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date hireDate = null;
if(shireDate != null){
try {
hireDate = sdf.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
//调用业务层获取所有的员工信息
EmployeeService empService = new EmployeeServiceImpl();
//List<Employee> empList = empService.findAll();
//条件如果多,可以封装到一个对象中,使用一个对象做参数
List<Employee> empList = empService.findEmp(empId,deptno,onDuty,hireDate);
//获取所有部门的信息
DepartmentService deptService = new DepartmentServiceImpl();
List<Department> deptList = deptService.findAll();
request.setAttribute("deptList", deptList);
//跳转到system/empList.jsp,保存数据,回显到页面输入框中,实现查询记忆功能
request.setAttribute("empId", empId);
request.setAttribute("deptno", deptno);
request.setAttribute("onDuty", onDuty);
request.setAttribute("hireDate", shireDate);
request.setAttribute("empList", empList);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
}