struts標籤

<action

    name="empForm"

    attribute="empForm"

      path="/queryEmp"

      scope="request"

      type="struts.t02.QueryEmpAction">

      <forward name="success" path="/struts/t02/queryEmp.jsp"/>

</action>

<form-bean name=”empForm” type=”struts.t02.EmpForm”></form-bean>

 

package emp;

public class Emp {

    private int empno = -1;

    private String ename = "aa";

    private  double sal = 0;

    private String job = "";

    private  int deptno = -1;

    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 double getSal() {

       return sal;

    }

    public void setSal(double sal) {

       this.sal = sal;

    }

    public String getJob() {

       return job;

    }

    public void setJob(String job) {

       this.job = job;

    }

    public int getDeptno() {

       return deptno;

    }

    public void setDeptno(int deptno) {

       this.deptno = deptno;

    }

}

 

package emp;

 

import java.sql.*;

import java.util.*;

import util.ConnectionUtil;

public class EmpDao {

 

         public void addEmp(Emp e) throws Exception{

                   Connection con = null;

                   PreparedStatement ps = null;

                   try{

                            con = ConnectionUtil.getConnection();

                            String sql = "insert into employee (empno,ename,job,sal,deptno) "

                                     +" values(?,?,?,?,?)";

                           

                            ps = con.prepareStatement(sql);

                            ps.setInt(1, e.getEmpno());

                            ps.setString(2, e.getEname());

                            ps.setString(3, e.getJob());

                            ps.setDouble(4, e.getSal());

                            ps.setInt(5, e.getDeptno());

                            ps.executeUpdate();

                   }catch(Exception ee){

                            throw ee;

                   }finally{

                            ConnectionUtil.closeDB(con, ps, null);

                   }

         }

        

        

         public List findDeptAll() throws Exception{

                   Connection con = null;

                   Statement st = null;

                   ResultSet rs = null;

                   List list = new ArrayList();

                   try{

                            con = ConnectionUtil.getConnection();

                            st = con.createStatement();

                            String sql = "select deptno,dname from dept";

                            rs = st.executeQuery(sql);

                            Dept d = null;

                            while(rs.next()){

                                     d = new Dept();

                                     d.setDeptno(rs.getInt(1));

                                     d.setDname(rs.getString(2));

                                     list.add(d);

                            }

                   }catch(Exception e){

                            throw e;

                   }finally{

                            ConnectionUtil.closeDB(con, st, rs);

                   }

                   return list;

         }

        

         public List findEmpAll(Emp e) throws Exception{

                   Connection con = null;

                   Statement st = null;

                   ResultSet rs = null;

                   List list = new ArrayList();

                   StringBuffer sql = new StringBuffer("select ename,empno,sal,job,deptno from emp where 1=1");

                   if(e != null){

                            if(e.getEname()!= null &&!e.getEname().equals("")){

                                     sql.append(" and ename like '%"+e.getEname()+"%'");

                            }

                            if(e.getJob()!= null &&!e.getJob().equals("")){

                                     sql.append(" and job ='"+e.getJob()+"'");

                            }

                            if(e.getDeptno() != -1){

                                     sql.append(" and deptno = "+e.getDeptno());

                            }

                   }

                   try{

                            con = ConnectionUtil.getConnection();

                            st = con.createStatement();

                            rs = st.executeQuery(sql.toString());

                            Emp emp = null;

                            while(rs.next()){

                                     emp = new Emp();

                                     emp.setEmpno(rs.getInt("empno"));

                                     emp.setEname(rs.getString("ename"));

                                     emp.setJob(rs.getString("job"));

                                     emp.setSal(rs.getDouble("sal"));

                                     emp.setDeptno(rs.getInt("deptno"));

                                     list.add(emp);

                            }

                   }catch(Exception ee){

                            throw ee;

                   }finally{

                            ConnectionUtil.closeDB(con, st, rs);

                   }

                   return list;

         }

}

 

package struts.t02;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import emp.Emp;

public class EmpForm extends ActionForm{

         private Emp emp = new Emp();

         public Emp getEmp() {

                   return emp;

         }

         public void setEmp(Emp emp) {

                   this.emp = emp;

         }

         public ActionErrors validate(ActionMapping mapping,

                            HttpServletRequest request) {

         //               ActionErrors errors = new ActionErrors();

//               if(emp.getEname() == null || emp.getEname().equals("")){

//                         errors.add("name", new ActionMessage("error.emp.add.name"));

//                         return errors;

//               }

//               if(emp.getSal() == 0){

//                         errors.add("sal", new ActionMessage("error.emp.add.sal"));

//                         return errors;

//               }

                   return null;

         }

}

 

package struts.t02;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

import java.util.*;

import emp.EmpDao;

 

public class QueryEmpAction  extends Action{

         public ActionForward execute(ActionMapping mapping, ActionForm form,

                            HttpServletRequest request, HttpServletResponse response) {

                   EmpForm ef = (EmpForm)form;

                   String target = "success";

                   try{

                            EmpDao dao = new EmpDao();

                            List list = dao.findDeptAll();

                            List empList = dao.findEmpAll(ef.getEmp());

                            request.setAttribute("deptList", list);

                            request.setAttribute("eList", empList);

                   }catch(Exception e){

                            e.printStackTrace();

                   }

                   return mapping.findForward(target);

         }

}

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'queryEmp.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

  <body>

    <br><html:form action="queryEmp" method="post">

    <table border=1>

        <tr>

            <td>姓名:</td>

            <td><html:text property="emp.ename"/><br></td>

            <td>工作:</td>

            <td>

               <html:select property="emp.job">

                   <html:option value="">請選擇</html:option>

                   <html:option value="sale">sale</html:option>

                   <html:option value="operator">operator</html:option>

               </html:select>

            </td>

        </tr>

        <tr>

            <td>部門:</td>

            <td>

            <html:select property="emp.deptno">

            <html:optionsCollection name="deptList" value="deptno" label="dname"/>

            </html:select>

            </td>

        </tr>

        <tr>

            <td colspan=4><html:submit value="查詢"/><br></td>

        </tr>

    </table>

    </html:form>

    <!--  java.util.List dataList = (List)request.getAttribute("list");-->

    <logic:empty name="eList" scope="request">

    <c:out value="沒有數據"></c:out>

    </logic:empty>

    <logic:notEmpty name="eList" scope="request">

    <table border=1>

    <tr>

        <td>編號</td>

        <td>姓名</td>

        <td>工作</td>

        <td>工資</td>

        <td>部門</td>

    </tr>

    <logic:iterate id="emp" name="eList">

    <tr>

        <td><c:out value="${emp.empno}"></c:out></td>

        <td><c:out value="${emp.ename}"></c:out></td>

        <td><c:out value="${emp.job}"></c:out></td>

        <td><bean:write name="emp" property="sal" format="#,###"/>

        <td><bean:write name="emp" property="deptno"/></td>

    </tr>

    </logic:iterate>

    </table>

    </logic:notEmpty>

  </body>

</html>

 

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章