Separation of HTML and JSP development (interesting)

Source: Internet
Author: User
Tags tld
Separation of HTML and JSP development

Date: 2003-10-15 17:28:16
Source: dynamic website creation Guide
Author: sayyes

In the traditional JSPProgram.CodeIt is convenient to write code together with Java code, but it also makes the page difficult to maintain. The burden on HTML developers and JSP developers increases, we can turn this traditional technology into page pulling data technology.

How can we separate HTML development from JSP development? The answer is to use the tag technology. By using the tag technology, we can avoid JSP code in the page program. When we need data, we should first agree on the tag, then, these tags are replaced by the tag's background handler to display data. I call this technology push data to pages. You only need to define the format of the page. In this way, HTML developers can focus on the appearance of the page, while Java programmers ignore page display and focus on background programs, greatly improving the maintainability and convenience of the program. It facilitates collaborative development between programmers.

First, you must understand some tag technologies before you can read this article. The following is an example program:

I. replace functions that replace strings

// Replace string functions
// String strsource-source string
// String strfrom-substring to be replaced
// String strto-string to be replaced
Public static string Replace (string strsource, string strfrom, string strto)
{
// If the substring to be replaced is null, the source string is directly returned.
If (strfrom = NULL | strfrom. Equals (""))
Return strsource;
String strdest = "";
// Length of the substring to be replaced
Int intfromlen = strfrom. Length ();
Int intpos;
// Replace the string cyclically
While (intpos = strsource. indexof (strfrom ))! =-1)
{
// Obtain the left substring of the matched string
Strdest = strdest + strsource. substring (0, intpos );
// Add the substring after replacement
Strdest = strdest + strto;
// Modify the source string to the substring after matching the substring
Strsource = strsource. substring (intpos + intfromlen );
}
// Add a child string that does not match
Strdest = strdest + strsource;
// Return
Return strdest;
}

Ii. TLD (mybooktag. TLD) defines your tag


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

3. Tag processing in the background, responsible for interpreting tags (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 {

// Mark start position execution
Public int dostarttag (){
Return eval_body_buffered;
}
// Mark the end position for execution
Public int doendtag () throws jsptagexception {
Int max = 0;
String listbody = NULL;
Int number = 1;
// Obtain the page number, that is, the content of the request object.
String serialno = pagecontext. getrequest (). getparameter ("serialno ");
// Convert to an integer
Try {
Number = integer. parseint (serialno );
}
Catch (exception e ){
Number = 1;
}
If (number <1)
Number = 1;
// Obtain the dataset stored in the session. Of course, the data can also be retrieved 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 ){
// Obtain the content inside the tag
Bodycontent BC = getbodycontent ();
For (INT I = (number-1) * 10; I <Max; I ++ ){
// Obtain a record
Bookmodel model = (bookmodel) bookvector. Get (I );
If (model = NULL)
Model = new bookmodel ();
// Replace the content (that is, output 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;
}
}

4. jsp page (Booklist. jsp)

<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Taglib uri = "/mybooktag" prefix = "mybooktag" %>
<HTML>
<Head>
<Title> A J2EE-based book 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"> book list </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"> NO. </TD>
<TD width = "200" bgcolor = "fefbf4" Height = "41"> graphic name </TD>
<TD width = "100" bgcolor = "fefbf4" Height = "41"> author </TD>
<TD width = "200" bgcolor = "fefbf4" Height = "41"> press </TD>
<TD width = "50" bgcolor = "fefbf4" Height = "41"> book price </TD>
<TD width = "100" bgcolor = "fefbf4" Height = "41"> operation </TD>
</Tr>
<! -- The label technology is used here. If it is not used, it will be troublesome. I believe you will feel it. -->
<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"> edit </font>
</A>
| <A href = "bookview. jsp? Itemno = $ _ Index ">
<Font color = "# ff0000"> View </font>
</A>
</TD>
</Tr>
</Mybooktag: listbook>














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.