怎样才能做到将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 strsource;
string strdest = ;
// 要替换的子串长度
int intfromlen = strfrom.length();
int intpos;
// 循环替换字符串
while((intpos = strsource.indexof(strfrom)) != -1)
{
// 获取匹配字符串的左边子串
strdest = strdest + strsource.substring(0,intpos);
// 加上替换后的子串
strdest = strdest + strto;
// 修改源串为匹配子串后的子串
strsource = strsource.substring(intpos + intfromlen);
}
// 加上没有匹配的子串
strdest = strdest + strsource;
// 返回
return strdest;
}
二、tld文(mybooktag.tld) 定义你的标签
br>public -//sun microsystems, inc.//dtd jsp tag library 1.2//en
http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd;>
1.0
1.2
listbook
com.book.taglib.listbooktag
jsp
三、tag的后台处理文件,负责解释标签(listbooktag.java)
package com.book.taglib;
import java.util.*;
import java.lang.*;
import com.book.model.bookmodel;
import com.book.utils.stringhelper;
import javax.servlet.jsp.jsptagexception;
import javax.servlet.jsp.tagext.bodytagsupport;
import javax.servlet.jsp.tagext.bodycontent;
import javax.servlet.jsp.pagecontext;
import javax.servlet.jsp.jspwriter;
import javax.servlet.servletrequest;
public class listbooktag extends bodytagsupport {
// 标志开始位置执行
public int dostarttag(){
return eval_body_buffered;
}
// 标志结束位置执行
public int doendtag()throws jsptagexception {
int max = 0;
string listbody = null;
int number = 1;
// 获取页码信息,也就是request对象中的内容
string serialno = pagecontext.getrequest().getparameter(serialno);
// 转换为整数
try{
number = integer.parseint(serialno);
}
catch(exception e){
number = 1;
}
if (number number = 1;
// 获取保存在session中的数据集,当然这里也可以从数据库中取数据
vector bookvector = (vector)pagecontext.getsession().getattribute(bookvector);
if(number*10 max = number*10;
else
max = bookvector.size();
if(bookvector.size()>0){
// 获取标签内部的内容
bodycontent bc = getbodycontent();
for (int i = (number - 1) * 10; i // 获取一条记录
bookmodel model = (bookmodel) bookvector.get(i);
if (model == null)
model = new bookmodel();
// 替换内容(就是在这里输出数据的,替换)
string body = bc.getstring();
body = stringhelper.replace(body, $_serialno, model.getbookid());
body = stringhelper.replace(body, $_bookname, model.getbookname());
body = stringhelper.replace(body, $_author, model.getauthor());
body = stringhelper.replace(body, $_phouse, model.getphouse());
body = stringhelper.replace(body, $_price, model.getprice().tostring());
body = stringhelper.replace(body, $_index, integer.tostring(i));
// 向页面输出
try{
pagecontext.getout().print(body);
}
catch(exception e){
}
}
}
return eval_page;
}
}
四、jsp页面(booklist.jsp)
一个基于j2ee的图书demo
图书列表
序号
图示名称
图书作者
出版社
图书价格
操作
$_serialno
$_bookname
$_author
$_phouse
$_price
编辑
|
查看
