欢迎光临
我们一直在努力

html与jsp开发分离技术-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

在传统的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

转自:动态网制作指南 www.knowsky.com

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » html与jsp开发分离技术-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章