带属性标签开发全过程----j2ee小应用之二(Inber)
来源:互联网 发布:java里的public 编辑:程序博客网 时间:2024/04/29 18:43
带属性标签开发全过程----j2ee小应用之二(Inber)
标签应用:
数据源
MyDataSource2
用户sa
密码(空)
数据库pubs 表:employee
说明:windows ODBC ---SQL 数据库pubs 表:employee
功能:检索 job_lvl>=(输入值)and job_lvl<=(输入值)的数据记录,在JSP页面中任意想调用的地方
调用它。
(1)
标签处理程序:EmployeeTag.java
package net.inber;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Hashtable;
import java.io.Writer;
import java.io.IOException;
import java.util.Date;
import java.sql.*;
import java.math.*;
import java.util.*;
public class EmployeeTag extends TagSupport
{
Connection con = null;
Statement state = null;
ResultSet rs = null;
private String min;
private String max;
public void setMinValue(String t)
{
this.min = t;
}
public String getMinValue(){return this.min;}
public void setMaxValue(String m)
{
this.max = m;
}
public String getMaxValue(){return this.max;}
public EmployeeTag() throws ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
public int doStartTag() throws JspTagException {
return EVAL_BODY_INCLUDE;
}
public int doEndTag()throws JspTagException
{
String dateString =new Date().toString();
try
{
pageContext.getOut().write("");
try{
JspWriter out = pageContext.getOut();
con = DriverManager.getConnection("jdbc:odbc:MyDataSource2","sa","");
String strQuery = "select emp_id,fname,lname,job_lvl,hire_date from employee where job_lvl>="+min+" and job_lvl<="+max;
System.out.println(strQuery);
state = con.createStatement();
rs = state.executeQuery(strQuery);
String emp_id=null;
String fname=null;
String lname=null;
String job_lvl=null;
String hire_date=null;
out.write("<table width='677' height='22' border='1'>") ;
while(rs.next())
{
emp_id = rs.getString(1);
fname = rs.getString(2);
lname = rs.getString(3);
job_lvl = rs.getString(4);
hire_date = rs.getString(5);
out.write("<tr>");
out.write("<td width='25%'>"+emp_id+"</td>");
out.write("<td width='17%'>"+fname+"</td>");
out.write("<td width='13%'>"+lname+"</td>");
out.write("<td width='9%'>"+job_lvl+"</td>");
out.write("<td width='36%'>"+hire_date+"</td>");
out.write("</tr>");
}
out.write("</table>");
}
catch(Exception e)
{
//throw new JspTagException("Fatal error:connect error!");
}
}
catch(IOException ex)
{
throw new JspTagException("Fatal error:hello tag conld not write to JSP out");
}
return EVAL_PAGE;
// return SKIP_PAGE;
}
}
(2)配置标签库描述符(TLD)文件 configTLD.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib 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"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<description>
A simple tab library for the examples
</description>
<tag>
<!-- 标识描述 -->
<description>select db pubs tb employees </description>
<!--定义标识名字 -->
<name>selectSQL</name>
<!--定义标识的关联类 -->
<tag-class>net.inber.EmployeeTag</tag-class>
<body-content>jsp</body-content>
<!--带参数标识-->
<attribute>
<!--参数名-->
<name>maxValue</name>
<required>true</required>
<!-- 是否允许表达式输出-->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>minValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
(3)创建index.jsp文件
<%@ taglib uri="/tagURI" prefix="myTag" %>
<%@ page import="net.inber.*"%>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>first cumstomed tag</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="386" height="67" border="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="392" height="94" border="0">
<tr>
<td>输入最大值
<input name="theMaxPut" type="text" id="theMaxPut"></td>
</tr>
<tr>
<td>输入最小值
<input name="theMinPut" type="text" id="theMinPut"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="提交">
<input name="hd" type="hidden" id="hd" value="yes"></td>
</tr>
</table>
</form></td>
</tr>
</table>
<%
String theMax,theMin;
String ghd;
ghd=request.getParameter("hd");
theMax=request.getParameter("theMaxPut");
theMin=request.getParameter("theMinPut");
%>
<p>以下的内容从库pubs检索表employee中job_lvl大于等于(输入值)<%=theMin%>小于(输入值)<%
=theMax%>的记录显示:</p>
<table width='677' height='21' border='1' bgcolor="#C14646">
<tr>
<td width="25%" >编号</td>
<td width="17%">名 </td>
<td width="13%">姓 </td>
<td width="9%">job_lvl</td>
<td width="36%">日期</td>
</tr>
</table>
<myTag:selectSQL minValue='<%=theMin%>' maxValue='<%=theMax%>'/>
此为主体结束部分
</body>
</html>
(4)最后布署(将文件按tomcat 目录结构布署,将修改web.xml文件如下)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app 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 http://java.sun.com/xml/ns/j2ee/web
-app_2_4.xsd"
version="2.4">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<taglib>
<!-- 定义标签URI -->
<taglib-uri>/tagURI</taglib-uri>
<!--定义标签具体位置 -->
<taglib-location>/WEB-INF/tlds/configTLD.tld</taglib-location>
</taglib>
<!-- JSPC servlet mappings end -->
</web-app>
总结:小技巧--注意JSP 页面定义的属性名开头第一个字母要小写如 minValue 在处理程序中的set(get)
方法后第一个字母要大写以免混淆出错;在各项文件配置参数中应该尽量避免同名的参数出现以免造成难
以理解,造成维护的麻烦。
本例原代码下载:http://inber.51.net/csdn_blog/soft/tag2.rar
- 带属性标签开发全过程----j2ee小应用之二(Inber)
- java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)
- java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)——JAVA夜未眠
- 用标签实现循环功能---标签开发之三(Inber原作)
- J2EE开发之(二)之apache tomcat https应用
- Taglib自定义标签之二带属性标签
- JSP的自定义标签(二)之带属性的标签
- JSTL标签开发(带属性)
- 开发带属性的标签
- 开发带属性的标签
- 开发带属性的标签
- JAVAWEB开发之JSTL标签库的使用、 自定义EL函数、自定义标签(带属性的、带标签体的)
- JAVAWEB开发之JSTL标签库的使用、 自定义EL函数、自定义标签(带属性的、带标签体的)
- 自定义JSP中的Taglib标签之二带自定义属性的标签
- 自定义JSP中的Taglib标签之二带自定义属性的标签
- EJB之无状态会话Bean简单应用-学习与实践(Inber原作)
- (41)自定义标签库开发(简单标签的使用和带属性的标签)
- 用Rational Rose和UML开发J2EE应用(二)
- ADSL使用中常见协议
- 什么是代理ARP(proxy ARP)?
- 掌柜的——写在七夕
- 超级忙碌的一周
- GNU/Linux Beginner's favourite content !!!
- 带属性标签开发全过程----j2ee小应用之二(Inber)
- 最近比较忙,忙的烦琐,都没时间继续做下去。。
- 好了-好了?了了?
- [转文]对于补丁的态度
- asp常用函数(一)
- Using DropDownList control in DataGrid
- C#程序编码规范
- asp常用函数2
- 一个简单的保护视力的软件