`
wenchenyangailiuyan
  • 浏览: 19440 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

在jsp页面中取出List封装的值的问题

    博客分类:
  • java
阅读更多

此工程的模式是javaBean+servlet+jsp

 

1.po层

package com.inspur.po;

import java.sql.Date;

public class EmpDept {
 private int empno;
    private String ename;
    private String mgr;
    private String job;
    private Date hiredate;
    private int sal;
    private int comm;
    private String dname;
    private String loc;
    private int deptno;
 public int getComm() {
  return comm;
 }
 public void setComm(int comm) {
  this.comm = comm;
 }
 public int getDeptno() {
  return deptno;
 }
 public void setDeptno(int deptno) {
  this.deptno = deptno;
 }
 public String getDname() {
  return dname;
 }
 public void setDname(String dname) {
  this.dname = dname;
 }
 public int getEmpno() {
  return empno;
 }
 public void setEmpno(int empno) {
  this.empno = empno;
 }
 public String getEname() {
  return ename;
 }
 public void setEname(String ename) {
  this.ename = ename;
 }
 public Date getHiredate() {
  return hiredate;
 }
 public void setHiredate(Date hiredate) {
  this.hiredate = hiredate;
 }
 public String getJob() {
  return job;
 }
 public void setJob(String job) {
  this.job = job;
 }
 public String getLoc() {
  return loc;
 }
 public void setLoc(String loc) {
  this.loc = loc;
 }
 public String getMgr() {
  return mgr;
 }
 public void setMgr(String mgr) {
  this.mgr = mgr;
 }
 public int getSal() {
  return sal;
 }
 public void setSal(int sal) {
  this.sal = sal;
 }
 public EmpDept(int empno, String ename, String mgr, String job, Date hiredate, int sal, int comm, String dname, String loc, int deptno) {
  super();
  this.empno = empno;
  this.ename = ename;
  this.mgr = mgr;
  this.job = job;
  this.hiredate = hiredate;
  this.sal = sal;
  this.comm = comm;
  this.dname = dname;
  this.loc = loc;
  this.deptno = deptno;
 }
 public EmpDept() {
  super();
 }
   
}

 

2.dao层

 

package com.inspur.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.inspur.po.EmpDept;

import com.inspur.tool.DBConn;

public class SelectEmp {
   public List Select(int deptno, String name){
    Connection conn=DBConn.getConn();
    PreparedStatement pst=null;
    ResultSet rst=null;
    EmpDept emp=null;
    List list=new ArrayList();
    String sql="select s1.empno,s1.ename,s2.ename,s1.job,s1.hiredate,s1.sal,s1.comm,dname,loc,s1.deptno " +
      "from emp s1,emp s2,dept " +
      " where dept.deptno=s1.deptno and s2.empno=s1.mgr " +
      " and s1.deptno=? and s1.ename like ?";//联表查询,s1和s2是自查询
    try {
  pst=conn.prepareStatement(sql);
  pst.setInt(1,deptno);
  pst.setString(2,"%"+name+"%");//模糊查询
  rst=pst.executeQuery();
  while(rst.next()){
   emp=new EmpDept(rst.getInt("empno"),rst.getString(2),rst.getString(3),rst.getString("job"),rst.getDate("hiredate"),rst.getInt("sal"),rst.getInt("comm"),rst.getString("dname"),rst.getString("loc"),rst.getInt("deptno"));
   list.add(emp);
  }
  
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
  return list; 
   }
}

 

 

3.servlet层

 

package com.inspur.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.inspur.dao.SelectEmp;


public class EmpServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request, response);
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String deptno=request.getParameter("deptno");
  String name=request.getParameter("name");
  HttpSession session=request.getSession();
  int dept_no=Integer.parseInt(deptno);//将从表里取出的部门号转换为整形
  SelectEmp emp=new SelectEmp();
  List list=new ArrayList();
  list=emp.Select(dept_no,name);
  if(list.isEmpty()){
   session.setAttribute("error_message","没有此部门");
   request.getRequestDispatcher("selectemp.jsp").forward(request,response);
  }else{
  session.setAttribute("deptno",list);
  request.getRequestDispatcher("selectemp.jsp").forward(request,response);//将从数据库中查出的信息返回到页面
  }
 }
    
}

 

4.jsp页面

 

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.*,com.inspur.po.EmpDept" %>
<html>
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="selectemp" name="" method="post">

   <table border="1" align="center">
        <tr align="center">
           <td width="150">
       <select name="deptno">
                     <option value="0">---请选择部门号---</option>
                     <%-- <option value="ename">姓名</option>--%>
                     <option value="10">10</option>
                     <option value="20">20</option>
                     <option value="30">30</option>
                     <option value="40">40</option>
              </select>
          </td>
          <td width="83">请输入姓名:</td>
          <td width="168"><input  type="text" name="name"/></td>
           <td width="79"><input type="submit" value="查询"></td>
     </tr>
        <tr align="center">
           <td>员工号</td>
           <td>员工姓名</td>
           <td>经理姓名</td>
           <td>工作类型</td>
           <td width="83">工作时间</td>
          <td width="55">工资</td>
          <td width="56">奖金</td>
           <td width="64">部门名称</td>
          <td width="77">公司地址</td>
          <td width="70">部门号</td>
     </tr>
        <%List list=(List)session.getAttribute("deptno");
       
         if(list==null||list.equals("")){%>
           <%--<%=session.getAttribute("error_message") %>这个不管用不知道为什么取不到值--%>
        
         <%}else{
             
         
         
           for(Iterator it=list.iterator();it.hasNext();){
                 EmpDept emp=(EmpDept)it.next();%>
                 <tr align="center">
                     <td><%=emp.getEmpno() %></td>
                     <td><%=emp.getEname() %></td>
                     <td><%=emp.getMgr() %></td>
                     <td><%=emp.getJob() %></td>
                     <td><%=emp.getHiredate() %></td>
                     <td><%=emp.getSal() %></td>
                     <td><%=emp.getComm() %></td>
                     <td><%=emp.getDname()%></td>
                     <td><%=emp.getLoc() %></td>
                     <td><%=emp.getDeptno() %></td>
                    
                 </tr>
             <%} %>
          <%} %>
   </table>
</form>
</body>
</html>

 

这里web.xml就不往出贴了,呵呵

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics