In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve JSP page and code separation, I believe that most people still do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1. Replace the replace function of the string
Through the use of this function to start our page and code separation.
/ / replacement string function / / String strSource-Source string / / String strFrom-the substring to be replaced / / String strTo-the string public static String replace (String strSource, String strFrom, String strTo) {/ / if the substring to be replaced is empty, the source string if (strFrom = = null | | strFrom.equals (")) return strSource; String strDest ="is returned directly. / / the length of the substring to be replaced int intFromLen = strFrom.length (); int intPos; / / Loop replacement string while ((intPos = strSource.indexOf (strFrom))! =-1) {/ / get the left substring strDeststrDest = strDest + strSource.substring of the matching string / / add the replaced substring strDeststrDest = strDest + strTo; / / modify the substring strSourcestrSource = strSource.substring (intPos + intFromLen) after the source string is a matching substring;} / / add the unmatched substring strDeststrDest = strDest + strSource; / / return return strDest;}
Tld text (MyBookTag.tld) defines your tag
The definition of tags is also an important step in separating the page from the code in the JSP source code.
xml version= "1.0" encoding= "ISO-8859-1"? > < 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 background processing file of Tag, which is 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 execute public int doStartTag () {return EVAL_BODY_BUFFERED;} / / Mark end position execute public int doEndTag () throws JspTagException {int max = 0; String ListBody = null; int number = 1 / / get the page number information, that is, the content in the request object String serialNo = pageContext.getRequest (). GetParameter ("serialNo"); / / convert to the integer try {number = Integer.parseInt (serialNo);} catch (Exception e) {number = 1;} if (number < 1) number = 1 / / get the dataset saved in Session. Of course, you can also fetch the 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 contents inside the tag BodyContent bc = getBodyContent (); for (int I = (number-1) * 10; I < max; iTunes +) {/ / get a record bookmodel model = (bookmodel) bookVector.get (I); if (model = = null) model = new bookmodel () / / replace the content (where the data is output, 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 try {pageContext.getOut () .print (body) to the page } catch (Exception e) {} return EVAL_PAGE;}}
4. Page JSP source code (BookList.jsp)
At this point, we can manipulate JSP to separate the page from the code.
<% @ page contentType= "text/html; charset=GBK"% > <% @ taglib uri= "/ MyBookTag" prefix= "MyBookTag"% > < html > < head > < title > an 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" > < h3 align= "center" > < font face= "boldface" color= "# 0000CC" > Book list < / font > < / h3 > < form name= "form1" method= "post" > < table width= "750" border= "1" cellspacing= "0" align= "center" cellpadding= "3" bordercolor= "# A***BB6" bordercolordark= "# ffffff" > < tr align= "center" > < td width= "100" bgcolor= "FEFBF4" height= "41" > serial number < / td > < td width= "200" bgcolor= "FEFBF4" height= "41" > graphic name < / td > < td width= "100" bgcolor= "FEFBF4" height= "41" > Book author < / td > < td width= "200" bgcolor= "FEFBF4" height= "41" > Publishing House < / td > < td Width= "50" bgcolor= "FEFBF4" height= "41" > Book Price < / td > < td width= "100" bgcolor= "FEFBF4" height= "41" > Operation < / td > < / tr >
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.