jsp自定义标签下拉框

来源:岁月联盟 编辑:exp 时间:2012-02-18

package com.soft.test;
 
 import java.sql.*;
 
 import sun.jdbc.odbc.ee.ConnectionPool;
 
 public class BaseDao {
     //定义数据源驱动
     private static final String drive="oracle.jdbc.driver.OracleDriver";
     //定义连接字符串
     private static final String url="jdbc:oracle:thin:@10.72.240.34:1522:ffv2dev2";
     //用户名
     private static final String uid="produsr";
     //密码
     private static final String pwd="prod_123";
     //获得连接
     public static Connection getConnection()
     { 
         Connection con=null;
         try {
             //加载驱动
             Class.forName(drive);
             //建立连接
             con=DriverManager.getConnection(url,uid,pwd);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return con;
     }
     //运行有结果,没有参数的sql语句
     public static ResultSet ResultrunSelectSql(String sql)
     {
         Connection con=null;
         PreparedStatement ps=null;
         ResultSet res=null;
         try
         {
         con=getConnection();
         ps=con.prepareStatement(sql);
         res=ps.executeQuery();  
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
         return res;
     }
     //执行有结果有参数的sql语句
     public static ResultSet runSelectSql(String sql,Object[] params)
     {
         Connection con=null;
         PreparedStatement pre=null;
         ResultSet res=null;
         
         try {
             con=getConnection();
             pre=con.prepareStatement(sql);
           for(int i=0;i<params.length;i++)
           {
               pre.setObject(i+1, params[i]);
               
           }
           res=pre.executeQuery();
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return res;
     }
     
     //运行没有结果,没有参数的sql语句
     public static boolean runUpdateSql(String sql)
     
     {
         Connection con=null;
         PreparedStatement ps=null;
         ResultSet res=null;
         try
         {
             con=getConnection();
             ps=con.prepareStatement(sql);
             
         ps.executeUpdate();
         return true;
             
         }
         catch(Exception e)
         {
             e.printStackTrace();
             return false;
         }
         
         
     }
     //执行有结果有参数的sql语句
     public static boolean runUpdateSql(String sql,Object[] params)
     {
         Connection con=null;
         PreparedStatement pre=null;
     
         
         try {
             con=getConnection();
             pre=con.prepareStatement(sql);
           for(int i=0;i<params.length;i++)
           {
               pre.setObject(i+1, params[i]);
                
           }
           pre.executeUpdate();
           return true;
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             return false;
         }   
     }
 }
 [java]
 package com.soft.test;
 
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.JspWriter;
 import javax.servlet.jsp.tagext.TagSupport;
 
 public class MyTag extends TagSupport{
     private String tableName;//表明
     private String label;//下拉框要显示的名称
     private String value;//下拉框的值
     private String where;//条件
     private String selectName;//下拉框的名称
     private String selectId;//下拉款的ID
 @Override
 public int doEndTag() throws JspException {
     // TODO Auto-generated method stub
     JspWriter out=this.pageContext.getOut();
     String sql="select "+label+","+value+" from "+tableName+" "+where+"";//定义sql语句
     Connection conn=BaseDao.getConnection();
     try {
         PreparedStatement ps=conn.prepareStatement(sql);
         ResultSet res=ps.executeQuery();
         out.print("<select id=/""+selectId+"/" name=/""+selectName+"/">");
         out.print("<option value=/"/">请选择</option>");
         while(res.next()){
             Object values=res.getObject(value);
             Object labels=res.getObject(label);
             out.print("<option value=/""+values+"/">"+labels+"</option>");
         }
         out.print("</select>");
     } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }finally{
     
     }
     return super.doEndTag();
 }
 public String getTableName() {
     return tableName;
 }
 public void setTableName(String tableName) {
     this.tableName = tableName;
 }
 public String getLabel() {
     return label;
 }
 public void setLabel(String label) {
     this.label = label;
 }
 public String getValue() {
     return value;
 }
 public void setValue(String value) {
     this.value = value;
 }
 public String getWhere() {
     return where;
 }
 public void setWhere(String where) {
     this.where = where;
 }
 public String getSelectName() {
     return selectName;
 }
 public void setSelectName(String selectName) {
     this.selectName = selectName;
 }
 public String getSelectId() {
     return selectId;
 }
 public void setSelectId(String selectId) {
     this.selectId = selectId;
 }
 
 }
 
[java]
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                         "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
 <taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>s</short-name>
  <uri>http://www.574394550.com</uri>
  
  <tag>
  <name>Select</name>
  <tag-class>com.soft.test.MyTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
  <name>tableName</name>
  <required>true</required>
  </attribute>
  
   <attribute>
  <name>label</name>
  <required>true</required>
  </attribute>
  
    <attribute>
  <name>value</name>
  <required>true</required>
  </attribute>
  
   <attribute>
  <name>where</name>
  <required>true</required>
  </attribute>
  
   <attribute>
  <name>selectName</name>
  <required>true</required>
  </attribute>
  
   <attribute>
  <name>selectId</name>
  <required>true</required>
  </attribute>
  </tag>
 </taglib>
 
[java]
 <%@ page language="java"  pageEncoding="gbk"%>
 <%@ taglib uri="http://www.2cto.com" prefix="s" %>
 <%
 String path = request.getContextPath();
 %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <title>My JSP 'index.jsp' starting page</title>
   </head>
   
   <body>
     This is my JSP page. <br>
     <s:Select selectName="select" selectId="select" label="user_name" value="user_id" tableName="tu_oaf_users" where="where 1=1"/>
   </body>
 </html>
 
 摘自xinghui_liu的专栏