JSP自定义标签——调用数据库(通过id号搜索相关信息)
一、创建新表(假设在master数据库下新建)
二、连接数据库
开始-->控制面板-->管理工具-->数据源-->系统DSN-->添加-->SQL Server-->名称:userdb-->服务器:(local)\SQLEXPRESS或选择本机名-->下一步-->下一步-->打钩-更改默认的数据库为:master-->下一步-->完成
三、DefinedTagData项目
1、新建Java类:User
package sample.model; public class User { private int id; private String name; private String phone; public void setId(int id){this.id=id;}//setId中的d必须小写 public void setName(String name){this.name=name;} public void setPhone(String phone){this.phone=phone;} public int getID(){return id;} public String getName(){return name;} public String getPhone(){return phone;} }
2、新建Java类:UserDAO
package sample.model; import java.sql.*; public class UserDAO { private ResultSet rst; private Connection conn; private void open()throws Exception{ String driver="sun.jdbc.odbc.JdbcOdbcDriver"; String url= "jdbc:odbc:userdb";//test--定义的数据库的名称 Class.forName(driver); conn=DriverManager.getConnection(url); } private void close()throws SQLException{ if(rst!=null) rst.close(); if(conn!=null) conn.close(); } public void save(User user)throws Exception{ String sql=""; open(); Statement stmt=conn.createStatement(); rst=stmt.executeQuery(sql); close(); } public void load(User user)throws Exception{ String sql="use master select name,phone from dbo.usera"+" where id = "+user.getID(); open(); Statement stmt=conn.createStatement(); rst=stmt.executeQuery(sql); if(rst.next()){ user.setName(rst.getString(1)); user.setPhone(rst.getString(2)); } close(); } }
3、创建TLD文件
新建文件--> Web--> 标记库描述符
TLD名称:Data 前缀:data
4、创建标记处理程序
新建文件--> Web-->标记处理程序
类名:DataTag 包:sample.tag 要扩展的标记支持类:SimpleTagSupport
TLD文件:WEB-INF--> tlds--> Data 新建属性:color和id
package sample.tag; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.JspException; import sample.model.*; public class DataTag extends TagSupport { private String id;//添加的属性 public void setId(String id) { this.id = id; } private String color;//添加的属性 public void setColor(String color) { this.color = color; } public int doStartTag(){ User user=new User(); user.setId(Integer.parseInt(id)); UserDAO dao=new UserDAO(); try{ dao.load(user); JspWriter out=pageContext.getOut(); String html="<br><b style=\"color:"+color+"\">Welcome to Employee Detail Table.</b><br>"; html+=" <table border=\"1\"><tr><th>员工ID</th><th>员工姓名</th><th>联系电话</th></tr>"; html+="<tr><td>"+user.getID()+"</td><td>"+user.getName()+"</td><td>"+user.getPhone()+"</td></tr></table>"; out.println(html); }catch(Exception e){ System.out.println(e.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException{ return EVAL_PAGE; } }
PS:Data.tld内容
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <short-name>data</short-name> <uri>/WEB-INF/tlds/Data</uri> <tag> <name>DataTag</name> <tag-class>sample.tag.DataTag</tag-class> <body-content>empty</body-content> <attribute> <name>id</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>color</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> <tag> <name>DataTable</name> <tag-class>sample.tag.DataTable</tag-class> <body-content>JSP</body-content> <attribute> <name>id</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <attribute> <name>color</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
5、index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="/WEB-INF/tlds/Data" prefix="data"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <data:DataTag id="1" color="blue"/> </body> </html>
6、显示结果