动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Jsp → 阅读文章

 html与jsp开发分离技术

作者来源: 
阅读 1357 人次 , 2006-3-29 4:24:00 


  在传统的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 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) 定义你的标签


<?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">
<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.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 < 1)
number = 1;
// 获取保存在session中的数据集,当然这里也可以从数据库中取数据
vector bookvector = (vector)pagecontext.getsession().getattribute("bookvector");
if(number*10<bookvector.size())
max = number*10;
else
max = bookvector.size();
if(bookvector.size()>0){
// 获取标签内部的内容
bodycontent bc = getbodycontent();
for (int i = (number - 1) * 10; i < max; 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)

<%@page contenttype="text/html; charset=gbk"%>
<%@ taglib uri="/mybooktag" prefix="mybooktag" %>
<html>
<head>
<title>一个基于j2ee的图书demo</title>

<script language="javascript">
function returnback(){
  document.form1.action = "bookadmin.jsp";
}
</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();" class="annew1">
  </div>
  </td>
  </tr>
  </table>
  </form>
  <p align="left"> </p>
</body>
</html


 本文TagsHTML/CSS  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:通过Jsp发送动态图像
· 下一篇:Servlet、Jsp中的多国语言显示
· 在JSP页面中实现检索数据的分页显示
· JSP由浅入深(1)
· JSP连接SQL SERVER问题总结
· Servlet及JSP中的多线程同步问题
· JSP中tomcat的SQL Server2000数据库连接池的配置


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077