(Transfer) Master the JSP custom label: (6)

zhaozj2021-02-16  85

Overview

Page 1 (4 pages of)

Sometimes, it may be desirable to modify the body of the method based on some application-specific display logic. This can be done by returning a value from dostartTAG (): skip_body skipped the body of the label, and Eval_Body is judged. Iterative labels need to implement the ITERATIONTAG interface. The container calls the DoafterBody () method to determine if it is necessary to re-judge the body. This method returns EVAL_BODY_AGAIN to indicate that the container should continue to determine the body. DOAFTERBODY () method Returns Skip_body indicates that iterative operations have ended. Both the Tagsupport class and the Bodytagsupport class implementationTAG interface. Back to the MAP theme, in the following sections We will create a custom label handler that iterates a group of MAPs and prints their values.

Control flow order example

Page 2 (4 pages a total)

Let's take a look at how the sample label is used before starting to implement a custom label handler. First, define two maps: <% @ taglib uri = "map" prefix = "map" in the example in the above example

{firstname = jennifer, lastname = Wirth, AGE = 33}

{FIRSTNAME = kiley, lastname = mckeon, agn = 27}

Then, create a collection named List, join the two MAPs (Employee1 and Employee2) defined above.

<% list.add (employee1); list.add (employee2);%> The following shows how to use a custom label iteration this collection:

First Name $ {firstname} last name $ {lastname} age $ {agn}

Map: PrintMaps tag iterative List collection. In each iteration, it uses its body, search for the current iteration of the current iteration in the map by searching the child character starting with $ {key}. It analyzes the key from the string $ {} and replaces it with the value of this key obtained from the MAP (ie map.get (key)).

Implement DostartTag () method

Page 3 (4 pages a total)

The dostartTag () method captures the collection from the range and then captures the iterator from the collection. If there is no item in the iterator (ore.hasnext ()), then the dostartTag () method returns Skip_Body to implement logic IF. In this case, this IF is equivalent to "if the collection is empty, the body judgment" is skipped. The iterator then grabs the first MAP from the collection. public class MapPrintMapsTag extends BodyTagSupport {private String name; private Iterator iter; private Map map; private String scope; public int doStartTag () throws JspException {Collection collection = null; / * Grab the collection out of scope using the scope attribute * /. if (scope == null) {collection = (Collection) pageContext.findAttribute (name);} else if ( "page" .equalsIgnoreCase (scope)) {collection = (Collection) pageContext.getAttribute (name);} else if ( "request" .equalsIgnoreCase (scope)) {collection = (Collection) pageContext.getRequest () getAttribute (name);.} else if ( "session" .equalsIgnoreCase (scope)) {collection = (Collection) pageContext.getSession () .getAttribute (name);} else if ( "application" .equalsIgnoreCase (scope)) {collection = (Collection) pageContext.getServletContext () getAttribute (name);..} / * Get the iterator from the collection * / iter = Collection.iterator (); / * if the collection is empty skip the body evataration. * / if (ore.hasnext () == false) Return Skip_body; / * Grab the first map out of the collection. * / Map = (map) iter.next (); returnval_body_buffered;} Implement the DoafterBody () method

Page 4 (4 pages of)

DOAFTERBODY () The iteration is achieved by returning Skip_Body when there is no overlapping item. If there is an item, then it returns Eval_Body_again. As long as DOAFTERBODY () returns Eval_Body_AGAIN, the container will continue to judge the body, as shown below: Public int dressboDy () throws jspexception {/ ** process body * / ... / ** Write the process buffer to the previous out * /.. Then get the content of the string with the getString () method of the text. Next it clears the textual content buffer, use StringTokenizer to find strings starting with $ {. It also creates a StringBuffer named buffer. When it iterates a string in the Tokenizer, they are attached to the buffer. If the string in the tokenizer begins with $ {, DOAFTERBODY (), then it extracts the key from the string and uses this key to find values ​​in the MAP. It is called the value object's toString () method converts the values ​​in the MAP into a string and attached to the result in the buffer. The following list shows how this process is performed.

转载请注明原文地址:https://www.9cbs.com/read-11708.html

New Post(0)