学习网考试学习资料

Gzu521.com

html与jsp开发分离技术

Java教程   点击:次   发布时间:2004-1-12   【字体: 】   来源:
贵 州 学 习 网
在传统的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) 定义你的标签



    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 < 1)
     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 < 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" %>


一个基于<a href="http://www.baidu.com/s?tn=lu333&ct=2097152&si=gzu521.com&s=on&word=J2EE" target="_blank" class=akey>J2EE</a>的图书demo

&#106avascript">
function returnback(){
  document.form1.action = "bookadmin.jsp";
}






图书列表



 
  
   
   
   
   
   
   
  
 
 
  

   
   
   
   
   
   
  
 

 
序号图示名称图书作者出版社图书价格操作
$_serialno$_bookname$_author$_phouse$_price
   
    编辑
   

   |
    查看
   

   

 
  
   
  
 

    

     &#111nclick="&#106avascript:returnback();" class="annew1">
    

   

 

 



责任编辑:gzu521

网络编程分类
ASP教程
.Net教程
Java教程
PHP教程
数据库基础
ACCESS教程
SQL Server教程
MySQL教程
Oracle教程
分类推荐信息
更多...
大类最新文章
更多...