struts标签

来源:互联网 发布:linux 深度压缩命令 编辑:程序博客网 时间:2024/05/22 07:42

<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 {

 

         publicvoid addEmp(Emp e) throws Exception{

                   Connectioncon = null;

                   PreparedStatementps = null;

                   try{

                            con= ConnectionUtil.getConnection();

                            Stringsql = "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(Exceptionee){

                            throwee;

                   }finally{

                            ConnectionUtil.closeDB(con,ps, null);

                   }

         }

        

        

         publicList findDeptAll() throws Exception{

                   Connectioncon = null;

                   Statementst = null;

                   ResultSetrs = null;

                   Listlist = new ArrayList();

                   try{

                            con= ConnectionUtil.getConnection();

                            st= con.createStatement();

                            Stringsql = "select deptno,dname from dept";

                            rs= st.executeQuery(sql);

                            Deptd = null;

                            while(rs.next()){

                                     d= new Dept();

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

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

                                     list.add(d);

                            }

                   }catch(Exceptione){

                            throwe;

                   }finally{

                            ConnectionUtil.closeDB(con,st, rs);

                   }

                   returnlist;

         }

        

         publicList findEmpAll(Emp e) throws Exception{

                   Connectioncon = null;

                   Statementst = null;

                   ResultSetrs = null;

                   Listlist = new ArrayList();

                   StringBuffersql = new StringBuffer("select ename,empno,sal,job,deptno from emp where1=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());

                            Empemp = 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(Exceptionee){

                            throwee;

                   }finally{

                            ConnectionUtil.closeDB(con,st, rs);

                   }

                   returnlist;

         }

}

 

package struts.t02;

importjavax.servlet.http.HttpServletRequest;

importorg.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

importorg.apache.struts.action.ActionMessage;

import emp.Emp;

public class EmpForm extends ActionForm{

         privateEmp emp = new Emp();

         publicEmp getEmp() {

                   returnemp;

         }

         publicvoid setEmp(Emp emp) {

                   this.emp= emp;

         }

         publicActionErrors validate(ActionMapping mapping,

                            HttpServletRequestrequest) {

         //               ActionErrors errors = newActionErrors();

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

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

//                         returnerrors;

//               }

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

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

//                         returnerrors;

//               }

                   returnnull;

         }

}

 

package struts.t02;

 

import javax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

importorg.apache.struts.action.ActionForward;

importorg.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

importorg.apache.struts.action.ActionMessages;

import java.util.*;

import emp.EmpDao;

 

public class QueryEmpAction  extends Action{

         publicActionForward execute(ActionMapping mapping, ActionForm form,

                            HttpServletRequestrequest, HttpServletResponse response) {

                   EmpFormef = (EmpForm)form;

                   Stringtarget = "success";

                   try{

                            EmpDaodao = new EmpDao();

                            Listlist = dao.findDeptAll();

                            ListempList = dao.findEmpAll(ef.getEmp());

                            request.setAttribute("deptList",list);

                            request.setAttribute("eList",empList);

                   }catch(Exceptione){

                            e.printStackTrace();

                   }

                   returnmapping.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="Thisis 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>

 

原创粉丝点击