jsp下拉列表—自定义标签
2016-12-11 05:14
260 查看
有时一个网页中下拉列表很多,而且是动态变化的时候,这时有自定义标签就会很方便
首先是jdbc数据连接
[c-sharp] view
plain copy
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类:
[c-sharp] view
plain copy
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的配置
[c-sharp] view
plain copy
<?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的调用:
[java] view
plain copy
<%@ 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>
首先是jdbc数据连接
[c-sharp] view
plain copy
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类:
[c-sharp] view
plain copy
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的配置
[c-sharp] view
plain copy
<?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的调用:
[java] view
plain copy
<%@ 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>
相关文章推荐
- JSP自定义标签实例---从数据库读取数据放在下拉列表中
- jsp中的下拉列表
- Ajax调用后台action返回JSONArray(JSONObject)类型实现jsp中下拉列表的动态显示
- JSP自定义标签实现多选下拉框
- jquery通过struts2请求,在jsp页面形成二级联动下拉列表
- jsp中的一个下拉列表实例
- JSP页面下拉列表中选中的值传递到文本框中
- jsp下拉列表取值问题
- jsp页面,给下拉列表自动选中指定的选项
- JSP页面中form表单的下拉列表
- 下拉列表select动态初始化 (JSP页面)
- jsp页面中的下拉列表
- 【JSP案例】无限级联|下拉列表
- jquery通过struts2请求,返回json数据,在jsp页面形成二级联动下拉列表
- jsp中下拉列表
- loushang5关于DataSet通过后台查询出的数据如何映射到前台JSP页面的下拉列表中
- JSP 如何获取下拉列表的选中的值是数据库表的对应数据
- jsp中 获得下拉列表中文本内容
- 下拉列表select动态初始化 (JSP页面)
- 解决jsp页面下拉列表可以输入文本的问题