jsp下拉列表—自定义标签

有时一个网页中下拉列表很多,而且是动态变化的时候,这时有自定义标签就会很方便

首先是jdbc数据连接

[c-sharp]  view plain  copy
  1. ackage pro.shopping.util;  
  2.   
  3. import <a href="http://lib.csdn.net/base/javase" class='replace_word' title="Java SE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.IOException;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import java.util.Properties;  
  10.   
  11. import com.sun.java_cup.internal.internal_error;  
  12.   
  13. /* 
  14.  * 该类用于连接<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>和对数据库进行操作 
  15.  */  
  16. public class JdbcUtil {  
  17.     public Connection con = null;   
  18.       
  19.     //连接数据库  
  20.     public Connection getCon(){  
  21.         //获取配置文件的数据  
  22.         Properties pro = new Properties();  
  23.         try {  
  24.             pro.load(JdbcUtil.class.getResourceAsStream("/db.properties"));  
  25.             String userName = pro.getProperty("userName");  
  26.             String userPassword = pro.getProperty("userPassword");  
  27.             String url = pro.getProperty("url");  
  28.             String driver = pro.getProperty("driver");  
  29.               
  30.             Class.forName(driver);  
  31.             con = DriverManager.getConnection(url,userName,userPassword);  
  32.         } catch (Exception e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }  
  36.         return con;  
  37.           
  38.     }  
  39.       
  40.     //查询操作  
  41.     public ResultSet gerSet(String sql,Object ...p){  
  42.         //连接数据库  
  43.         con = this.getCon();  
  44.         ResultSet re = null;  
  45.         try {  
  46.             PreparedStatement pre = con.prepareStatement(sql);  
  47.             if(p!=null){  
  48.                 for (int i = 0; i < p.length; i++) {  
  49.                     pre.setObject(i+1, p[i]);  
  50.                 }  
  51.                 re = pre.executeQuery();  
  52.             }  
  53.         } catch (SQLException e) {  
  54.             // TODO Auto-generated catch block  
  55.             e.printStackTrace();  
  56.         }  
  57.         return re;  
  58.     }  
  59.       
  60.     //增加、删除、修改操作  
  61.     public int zsg(String sql,Object ...p){  
  62.         con = this.getCon();  
  63.         int j = 0;  
  64.         try {  
  65.             PreparedStatement pre = con.prepareStatement(sql);  
  66.             if(p!=null){  
  67.                 for (int i = 0; i < p.length; i++) {  
  68.                     pre.setObject(i+1, p[i]);  
  69.                 }  
  70.                 j = pre.executeUpdate();  
  71.                 con.close();    //关闭数据库  
  72.             }  
  73.         } catch (SQLException e) {  
  74.             // TODO Auto-generated catch block  
  75.             e.printStackTrace();  
  76.         }  
  77.         return j;  
  78.     }  
  79.       
  80. }  

   然后编写Dao类:

 

[c-sharp]  view plain  copy
  1. package pro.shoping.Tag;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6.   
  7. import javax.servlet.jsp.JspException;  
  8. import javax.servlet.jsp.JspWriter;  
  9. import javax.servlet.jsp.tagext.SimpleTagSupport;  
  10.   
  11. import pro.shopping.util.JdbcUtil;  
  12.   
  13. public class HtmlTag extends SimpleTagSupport{  
  14.     private JdbcUtil jUtil = new JdbcUtil();  
  15.       
  16.     private String table;    
  17.     private String value;    
  18.     private String label;   
  19.     public void setTable(String table) {  
  20.         this.table = table;  
  21.     }  
  22.     public void setValue(String value) {  
  23.         this.value = value;  
  24.     }  
  25.     public void setLabel(String label) {  
  26.         this.label = label;  
  27.     }  
  28.       
  29.     @Override  
  30.     public void doTag() throws JspException, IOException {  
  31.         // TODO Auto-generated method stub  
  32.         String sql = "select * from "+table;  
  33.         ResultSet rs = jUtil.gerSet(sql);  
  34.         JspWriter out = getJspContext().getOut();  
  35.         out.print("<select name="+table+">");  
  36.         out.print("<option value=-1>--请选择--</option>");  
  37.            try {  
  38.             while (rs != null && rs.next()) {  
  39.                 String v = rs.getString(value);  
  40.                 String l = rs.getString(label);  
  41.                 out.print("<option value=" + v + ">" + l + "</option>");  
  42.             }  
  43.         } catch (SQLException e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         }  
  47.         out.print("</select>");  
  48.         super.doTag();  
  49.     }  
  50.       
  51.       
  52.   
  53. }  

最后是tld的配置

[c-sharp]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <taglib xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"  
  6.     version="2.1">  
  7.   
  8.     <description>JSTL 1.1 core library</description>  
  9.     <display-name>JSTL core</display-name>  
  10.     <tlib-version>1.1</tlib-version>  
  11.     <short-name>ccc</short-name>  
  12.     <uri>http://java.sun.com/jsp/jstl/core3</uri>  
  13.   
  14.     <tag>  
  15.         <name>select</name>  
  16.         <tag-class>pro.shoping.Tag.HtmlTag</tag-class>  
  17.         <body-content>empty</body-content>  
  18.   
  19.         <!-- 属性 -->  
  20.         <attribute>  
  21.             <name>table</name>  
  22.             <required>true</required>  
  23.         </attribute>  
  24.   
  25.         <attribute>  
  26.             <name>value</name>  
  27.             <required>true</required>  
  28.         </attribute>  
  29.   
  30.         <attribute>  
  31.             <name>label</name>  
  32.             <required>true</required>  
  33.         </attribute>  
  34.   
  35.     </tag>  
  36.   
  37. </taglib>  
  

 

html的调用:

[java]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix="html" uri="/WEB-INF/html.tld" %>  
  3.   
  4. 部门:<html:select label="depname" table="dep" value="depid"></html:select>  <br>  
  5. 员工:<html:select label="empname" table="emp" value="empid"></html:select>  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值