|
![]() | 作者: wenyunbo [wenyunbo]
![]() |
登录 |
原文收录在中国IT认证实验室:http://www.chinaitlab.com/www/news/article_show.asp?id=18872 在传统的JSP程序中,我们将HTML代码与Java代码混合在一起编写,这样虽然方便,但同时也导致页面难以维护,HTML开发人员和JSP开发人员负担加重,我们可以将这种传统的技术成为页面拉数据技术。 怎样才能做到将HTML开发和JSP开发分离呢?答案就是使用Tag技术,通过使用Tag技术,我们就可以在页面程序中不出现JSP代码,在需要数据的地方,大家先约定好标签,然后由Tag的后台处理程序去替换这些标签,显示数据。我称这种技术叫做向页面推数据,页面只要定义好格式就行了。这样,我们可以让HTML开发人员专注于页面的外观,而Java程序员则不用理会页面显示,专注于后台程序,大大提高了程序的可维护性和方便性。便于各程序员之间的协作开发。 首先你要懂一些Tag技术,然后才能阅读本文。下面是样例程序: 一、首先是替换字符串的replace函数 // 替换字符串函数 // String strSource - 源字符串 // String strFrom - 要替换的子串 // String strTo - 替换为的字符串 public static String replace(String strSource, String strFrom, String strTo) { // 如果要替换的子串为空,则直接返回源串 if(strFrom == null || strFrom.equals("")) return strSource0 String strDest = ""0 // 要替换的子串长度 int intFromLen = strFrom.length()0 int intPos0 // 循环替换字符串 while((intPos = strSource.indexOf(strFrom)) != -1) { // 获取匹配字符串的左边子串 strDest = strDest + strSource.substring(0,intPos)0 // 加上替换后的子串 strDest = strDest + strTo0 // 修改源串为匹配子串后的子串 strSource = strSource.substring(intPos + intFromLen)0 } // 加上没有匹配的子串 strDest = strDest + strSource0 // 返回 return strDest0 } 二、Tld文(MyBookTag.tld) 定义你的标签 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd";0> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name></short-name> <tag> <name>ListBook</name> <tag-class>com.book.taglib.ListBookTag</tag-class> <body-content>JSP</body-content> </tag> </taglib> 三、Tag的后台处理文件,负责解释标签(ListBookTag.java) package com.book.taglib0 import java.util.*0 import java.lang.*0 import com.book.model.bookmodel0 import com.book.utils.StringHelper0 import javax.servlet.jsp.JspTagException0 import javax.servlet.jsp.tagext.BodyTagSupport0 import javax.servlet.jsp.tagext.BodyContent0 import javax.servlet.jsp.PageContext0 import javax.servlet.jsp.JspWriter0 import javax.servlet.ServletRequest0 public class ListBookTag extends BodyTagSupport { // 标志开始位置执行 public int doStartTag(){ return EVAL_BODY_BUFFERED0 } // 标志结束位置执行 public int doEndTag()throws JspTagException { int max = 00 String ListBody = null0 int number = 10 // 获取页码信息,也就是request对象中的内容 String serialNo = pageContext.getRequest().getParameter("serialNo")0 // 转换为整数 try{ number = Integer.parseInt(serialNo)0 } catch(Exception e){ number = 10 } if (number < 1) number = 10 // 获取保存在Session中的数据集,当然这里也可以从数据库中取数据 Vector bookVector = (Vector)pageContext.getSession().getAttribute("bookVector")0 if(number*10<bookVector.size()) max = number*100 else max = bookVector.size()0 if(bookVector.size()>0){ // 获取标签内部的内容 BodyContent bc = getBodyContent()0 for (int i = (number - 1) * 100 i < max0 i++) { // 获取一条记录 bookmodel model = (bookmodel) bookVector.get(i)0 if (model == null) model = new bookmodel()0 // 替换内容(就是在这里输出数据的,替换) String body = bc.getString()0 body = StringHelper.replace(body, "$_SerialNo", model.getBookid())0 body = StringHelper.replace(body, "$_BookName", model.getBookname())0 body = StringHelper.replace(body, "$_Author", model.getAuthor())0 body = StringHelper.replace(body, "$_PHouse", model.getPhouse())0 body = StringHelper.replace(body, "$_Price", model.getPrice().toString())0 body = StringHelper.replace(body, "$_index", Integer.toString(i))0 // 向页面输出 try{ pageContext.getOut().print(body)0 } catch(Exception e){ } } } return EVAL_PAGE0 } } 四、JSP页面(BookList.jsp) <%@page contentType="text/html0 charset=GBK"%> <%@ taglib uri="/MyBookTag" prefix="MyBookTag" %> <html> <head> <title>一个基于J2EE的图书DEMO</title> <script language="javascript"> function returnBack(){ document.form1.action = "BookAdmin.jsp"0 } </script> </head> <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0"> <h2 align="center"><font face="黑体" color="#0000CC">图书列表</font></h2> <form name="form1" method="post"> <table width="750" border="1" cellspacing="0" align="center" cellpadding="3" bordercolor="#A5ABB6" bordercolordark="#ffffff"> <tr align="center"> <td width="100" bgcolor="FEFBF4" height="41">序号</td> <td width="200" bgcolor="FEFBF4" height="41">图示名称</td> <td width="100" bgcolor="FEFBF4" height="41">图书作者</td> <td width="200" bgcolor="FEFBF4" height="41">出版社</td> <td width="50" bgcolor="FEFBF4" height="41">图书价格</td> <td width="100" bgcolor="FEFBF4" height="41">操作</td> </tr> <!--这里使用标签技术,如果不用,就麻烦了,相信您一定有感触--> <MyBookTag:ListBook> <tr align="center"> <td width="100" height="19">$_SerialNo</td> <td width="200" height="19">$_BookName</td> <td width="100">$_Author</td> <td width="200">$_PHouse</td> <td width="50" height="19">$_Price</td> <td width="100" height="19" align="left"> <a href="bookEditTable.jsp?ItemNo=$_index"> <font color="#0000CC">编辑</font> </a> |<a href="bookview.jsp?ItemNo=$_index"> <font color="#FF0000">查看</font> </a> </td> </tr> </MyBookTag:ListBook> </table> <table width="400" border="0"> <tr> <td width="100%" align="right"> <div align="right"> <input type="submit" name="Submit" value="返回" onClick="javascript:returnBack()0" class="annew1"> </div> </td> </tr> </table> </form> <p align="left"> </p> </body> </html> ![]() |
地主 发表时间: 04-04-18 15:51 |
![]() | 回复: moley [moley] ![]() |
登录 |
顶! |
B1层 发表时间: 04-04-19 16:35 |
![]() | 回复: ziaichen [ziaichen] ![]() |
登录 |
不错! |
B2层 发表时间: 04-04-19 17:11 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号