The separation technology of HTML and JSP development

Source: Internet
Author: User
Tags execution integer tostring tld
Js


In the traditional JSP program, we mix the HTML code with Java code, which is convenient, but also lead to difficult to maintain the page, HTML developers and JSP developers to increase the burden, we can make this traditional technology as a page pull data technology.

How can you do to separate HTML development from JSP development? The answer is using tag technology, through the use of tag technology, we can in the page program does not appear in the JSP code, where the data need, we first agreed to a good label, and then by the tag's background handler to replace these tags, display data. I call this technology called to push data to the page, as long as the page is defined in a good format. In this way, we can let the HTML developers focus on the appearance of the page, while the Java programmers ignore the page display, focus on the background program, greatly improve the maintainability and convenience of the program. Facilitates collaborative development among programmers.

First you need to know some tag technology before you can read this article. Here is the sample program:

First, the Replace function that replaces the string

Replace String function
String strsource-source string
String strfrom-substring to replace
String Strto-strings replaced with
public static string replace (string strsource, String strfrom, String strto)
{
If the substring to be replaced is empty, the source string is returned directly
if (strfrom = null | | strfrom.equals (""))
return strsource;
String strdest = "";
Substring length to replace
int intfromlen = Strfrom.length ();
int intpos;
Loop Replacement string
while ((intpos = Strsource.indexof (strfrom))!=-1)
{
Gets the left substring of the matching string
strdest = strdest + strsource.substring (0,intpos);
Plus the replaced substring
strdest = strdest + strto;
To modify the substring after the source string is a matching substring
strsource = strsource.substring (intpos + Intfromlen);
}
Plus no strings to match
strdest = strdest + strsource;
Return
return strdest;
}

Two, the TLD (MYBOOKTAG.TLD) defines your label


<?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>

Third, the tag of the background processing files, responsible for the interpretation of the label (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 {

Flag Start Position Execution
public int doStartTag () {
return eval_body_buffered;
}
Flag End Position Execution
public int Doendtag () throws Jsptagexception {
int max = 0;
String listbody = null;
int number = 1;
Get the page number information, which is the content in the Request object
String Serialno = Pagecontext.getrequest (). GetParameter ("Serialno");
Convert to Integer
try{
Number = Integer.parseint (Serialno);
}
catch (Exception e) {
Number = 1;
}
if (number < 1)
Number = 1;
Gets the dataset saved in the session, and of course it can also fetch data from the database
Vector bookvector = (vector) pagecontext.getsession (). getattribute ("Bookvector");
if (Number*10<bookvector.size ())
max = number*10;
Else
max = Bookvector.size ();
if (Bookvector.size () >0) {
Get the content inside the label
bodycontent BC = getbodycontent ();
for (int i = (number-1) * i < Max; i++) {
Get a record
Bookmodel model = (Bookmodel) bookvector.get (i);
if (model = = NULL)
Model = new Bookmodel ();
Replace the content (that is, the output of the data here, replace)
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));
Output to the page
try{
Pagecontext.getout (). print (body);
}
catch (Exception e) {

}
}
}
return eval_page;
}
}

Four, JSP page (booklist.jsp)

<% @page contenttype= "text/html; CHARSET=GBK "%>
<%@ taglib uri= "/mybooktag" prefix= "Mybooktag"%>
<title> a book demo</title> based on Java-EE

<script language= "JavaScript" >
function Returnback () {
Document.form1.action = "bookadmin.jsp";
}
</script>

<body bgcolor= "#FFFFFF" text= "#000000" leftmargin= "0" topmargin= "0" >


&LT;H2 align= "center" ><font face= "bold" color= "#0000CC" > Book list </font><form name= "Form1" method= "POST" >
<table width= "750" border= "1" cellspacing= "0" align= "center" cellpadding= "3" bordercolor= "#A5ABB6" Bordercolordark = "#ffffff" >
&LT;TR align= "center" >
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Serial number </td>
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Icon name </td>
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Book author </td>
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Publishing house </td>
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Book price </td>
&LT;TD width= "bgcolor=" FEFBF4 "height=" > Operation </td>
</tr>
<!--use label technology here, if not, the trouble, I believe you must have feelings-->
<MyBookTag:ListBook>
&LT;TR align= "center" >
&LT;TD width= "height=" >$_SerialNo</td>
&LT;TD width= "height=" >$_BookName</td>
&LT;TD width= ">$_Author</td>"
&LT;TD width= ">$_PHouse</td>"
&LT;TD width= "height=" >$_Price</td>
&LT;TD width= "height=" align= "left" >
<a href= "bookedittable.jsp? Itemno=$_index ">
<font color= "#0000CC" > Edit </font>
</a>
|<a href= "bookview.jsp? Itemno=$_index ">
<font color= "#FF0000" > View </font>
</a>
</td>
</tr>
</MyBookTag:ListBook>

</table>
<table width= "border=" 0 ">
<tr>
<TD width= "100%" align= "right" >
<div align= "Right" >
<input type= "Submit" name= "Submit" value= "return" class= "Annew1" >
</div>
</td>
</tr>
</table>
</form>
<p align= "Left" > </p>
</body>





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.