有时一个网页中下拉列表很多,而且是动态变化的时候,这时有自定义标签就会很方便
首先是jdbc数据连接
- ackage pro.shopping.util;
- 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;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
- import com.sun.java_cup.internal.internal_error;
- /*
- * 该类用于连接<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>和对数据库进行操作
- */
- public class JdbcUtil {
- public Connection con = null;
- //连接数据库
- public Connection getCon(){
- //获取配置文件的数据
- Properties pro = new Properties();
- try {
- pro.load(JdbcUtil.class.getResourceAsStream("/db.properties"));
- String userName = pro.getProperty("userName");
- String userPassword = pro.getProperty("userPassword");
- String url = pro.getProperty("url");
- String driver = pro.getProperty("driver");
- Class.forName(driver);
- con = DriverManager.getConnection(url,userName,userPassword);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return con;
- }
- //查询操作
- public ResultSet gerSet(String sql,Object ...p){
- //连接数据库
- con = this.getCon();
- ResultSet re = null;
- try {
- PreparedStatement pre = con.prepareStatement(sql);
- if(p!=null){
- for (int i = 0; i < p.length; i++) {
- pre.setObject(i+1, p[i]);
- }
- re = pre.executeQuery();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return re;
- }
- //增加、删除、修改操作
- public int zsg(String sql,Object ...p){
- con = this.getCon();
- int j = 0;
- try {
- PreparedStatement pre = con.prepareStatement(sql);
- if(p!=null){
- for (int i = 0; i < p.length; i++) {
- pre.setObject(i+1, p[i]);
- }
- j = pre.executeUpdate();
- con.close(); //关闭数据库
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return j;
- }
- }
然后编写Dao类:
- package pro.shoping.Tag;
- import java.io.IOException;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspWriter;
- import javax.servlet.jsp.tagext.SimpleTagSupport;
- import pro.shopping.util.JdbcUtil;
- public class HtmlTag extends SimpleTagSupport{
- private JdbcUtil jUtil = new JdbcUtil();
- private String table;
- private String value;
- private String label;
- public void setTable(String table) {
- this.table = table;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public void setLabel(String label) {
- this.label = label;
- }
- @Override
- public void doTag() throws JspException, IOException {
- // TODO Auto-generated method stub
- String sql = "select * from "+table;
- ResultSet rs = jUtil.gerSet(sql);
- JspWriter out = getJspContext().getOut();
- out.print("<select name="+table+">");
- out.print("<option value=-1>--请选择--</option>");
- try {
- while (rs != null && rs.next()) {
- String v = rs.getString(value);
- String l = rs.getString(label);
- out.print("<option value=" + v + ">" + l + "</option>");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- out.print("</select>");
- super.doTag();
- }
- }
最后是tld的配置
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglib xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
- version="2.1">
- <description>JSTL 1.1 core library</description>
- <display-name>JSTL core</display-name>
- <tlib-version>1.1</tlib-version>
- <short-name>ccc</short-name>
- <uri>http://java.sun.com/jsp/jstl/core3</uri>
- <tag>
- <name>select</name>
- <tag-class>pro.shoping.Tag.HtmlTag</tag-class>
- <body-content>empty</body-content>
- <!-- 属性 -->
- <attribute>
- <name>table</name>
- <required>true</required>
- </attribute>
- <attribute>
- <name>value</name>
- <required>true</required>
- </attribute>
- <attribute>
- <name>label</name>
- <required>true</required>
- </attribute>
- </tag>
- </taglib>
html的调用:
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%@ taglib prefix="html" uri="/WEB-INF/html.tld" %>
- 部门:<html:select label="depname" table="dep" value="depid"></html:select> <br>
- 员工:<html:select label="empname" table="emp" value="empid"></html:select>