Javaweb Custom Labels &JSTL& internationalization (ix)

Source: Internet
Author: User
Tags array length date now dateformat i18n locale tld

Introduction to Custom Labels

Custom labels because there is no practice, so the master is not very deep, but for me enough to meet the user login registration case and shopping cart case, have time or more understanding of the good

1. Introduction to Custom Tags

1. Role: Replace the Java Script (<%%>) in the JSP page. (Official recommendation: JSP2.0 start)

2. Development steps
1. Customize a class to inherit Simpletagsupport implementation Dotag Method 2. Web-inf, create TLD file, define label address/define label Contents 3. The JSP page uses the TAGLIB directive to introduce the URI address, prefix defines the label name, and searches for jsp2-example-taglib.tldbody-content directly on the page called the label TLD file configuration-->tomcat server: The value of the content JSP: do not consider. Empty: Traditional and simple labels can be used for the traditional label processing class. The start tag and end tag are used without the body content. Scriptless: for simple labels. The start tag and end tag are used when there is a body content. But the content cannot have <%tagdependent: for simple label use. Tells the label processing class that the body content is normal text
3. Custom Label Execution principle
Method in Simpletag interface: void Dotag (): Called by the server. Called when a label is encountered in the JSP. Jsptag getParent (): Called by the programmer. Gets the parent label object for the label. There is a common feature of the following three methods that do not return null: Called by the server, the void Setjspbody (Jspfragment jspbody) is completed before calling Dotag: called by the server. The content of the incoming label is void Setjspcontext (Jspcontext pc): Called by the server. PageContext object passed into the current page void SetParent (jsptag parent): Called by the server
4. Label that implements the If Else feature (nested label case)
1. First define the Choose Label class, because choose has no principal content, so you can output getjspbody () directly. Invoke (null); 2. Defines a variable flag to remember whether Otherwis performs 3. Define when label class, after execution, set the parent tag to FALSE4. Gets the parent tag of a variable, such as True value, to perform a label body content TLD file configuration <tag><name>choose</name><tag-class> Com.itheima.web.mytag.app1.mychoosedemo2</tag-class><body-content>scriptless</body-content> </tag> <tag><name>when</name><tag-class>com.itheima.web.mytag.app1.mywhendemo2 </tag-class><body-content>tagdependent</body-content> <attribute><!--Parameter name-->< name>test</name><!--is a must--><required>true</required><!--support expression rt:runtime expr: Expression value El--><rtexprvalue>true</rtexprvalue></attribute></tag> <tag> <name>otherwise</name><tag-class>com.itheima.web.mytag.app1.myotherwisedemo2</tag-class ><body-content>tagdependent</body-content></tag>
5. Implement the FOR function label (repeat the main content)
Private Object items;//is used to receive the collection private Collection Collection = new ArrayList (); Sub-interface list Set map is not its subclass Map.entryset () can convert map to setprivate String var;//it's wonderful. public void Setitems (Object items) {//To Handle the collection of uploaded pages if (items instanceof list) {collection = (List) items;} else if (items instanceof Map) {collection = ((MAP) items). EntrySet ();} else if (items instanceof Set) {collection = (Set) items;} else if (Items.getclass (). IsArray ()) {//As long as the array is on line, 8 basic types of arrays, and object[]int length = array.getlength (items);//Get the array length for ( int i = 0; i < length; i++) {//takes each element in the array object obj = Array.get (items, i);//Take the first element collection.add (obj);//Add the elements in the array to the collection}}this.items = items;} public void SetVar (String var) {this.var = var;} public void Dotag () throws Jspexception, IOException {//Traversal collection for (Object obj:collection) {//Traverse a//2. Because the value is directly on the page, is to take the value from the PageContext domain, now let the data in the collection be put into the pagecontextpagecontext pc = (PageContext) getjspcontext ();p C.setattribute (Var, obj);//3. Executes the principal content Getjspbody (). Invoke (null);}} TLD Label Configuration <tag> &LT;NAME&GT;FOREACH2&Lt;/name><tag-class>com.itheima.web.mytag.app1.myforeach2demo3</tag-class><!--setting up body content            Empty cannot be placed in the main content--><body-content>scriptless</body-content> <attribute> <!--parameter Names--            <name>items</name> <!--is required--<required>true</required>        <!--support expression rt:runtime expr:expression value el--<rtexprvalue>true</rtexprvalue> </attribute> <attribute> <!--parameter Names--<name>var</name> <!- -is required--<required>true</required> <!--support expression Rt:runtime expr:expression val  UE el--<rtexprvalue>true</rtexprvalue> </attribute></tag>

JSTL1. What is Jstl
JSTL is an Apache extension to El expressions (that is, Jstl relies on EL), Jstl is the tag language import tag library requires the TAGLIB directive <%@ taglib prefix= "C" uri= "http://java.sun.com/ Jsp/jstl/core "%>
2. Jstl Core Library
1. Out and set (emphasis) 1). Out<c:out value= "${aaa}"/><==> with ${aaa} <c:out value= "${a}" default= "xxx" escapexml= "false"/> When ${a } does not exist when the output xxx string, when EscapeXML is false, does not convert "<", ">" 2). Set<jsp:usebean id= "user" class= "cn.itcast.domain.User"/><c:set target= "${user}" property= "username" value = "Qdmmy6"/><c:set target= "${user}" property= "password" value= "123456"/><c:out value= "${user}"/> Assign a value of Qdmmy6 to the user's Username property, assigning a value of 1234562 to the password property of the user. Remove<c:remove var= "A" scope= "page"/> Delete data in PageContext of name a 3. Url<c:url value= "/" var= "a" scope= "request"/> Output context Path:/DAY08_01/, assigns the result that should be output to variable A, the range is request4. If and choose (emphasis) if the test property of the If label must be a Boolean value, if the value of test is true, then the contents of the If label is executed, otherwise the Choose tag corresponds to If/else in Java if/ else structure when the label's test is true, the content of this when is executed. The contents of the otherwise tag are executed when all when label test is false, 5. The foreach (Focus) foreach is currently the Loop label, Var remembers the traversed variable, and the items traversal object, the foreach tag, also has a property: Varstatus, which is used to specify the name of the variable that receives the "loop state", for example: <foreach Varstatus= "vs" .../> at this point, you can use the VS variable to get the state of the loop.? count:int type, current to traverse the number of elements;? index: int type, subscript for current element; First:boolean type, is the first element;? Last:boolean type, is the last element;? current:object type, which represents the current project 

Internationalization 1. i18n and l10n full name

internationalization:i18n internationalization--------------> localization:l10n (localization)

2. Localization
<%locale Locale = Request.getlocale ();////The resource file depends on the client's language environment ResourceBundle RB = Resourcebundle.getbundle (" Com.itheima.day11i18n.message ", Locale), Out.write (rb.getstring (" Hello "));%><!--el expression fetch request, get client locale-- ><fmt:setlocale value= "${pagecontext.request.locale}"/><!--binding resource file--><fmt:setbundle basename= " Com.itheima.day11i18n.message "var=" Mybundle "/> <fmt:message bundle=" ${mybundle} "key=" Hello "></fmt: Message>
3. Time formatting
Date now = new Date ();D Ateformat df = dateformat.getdatetimeinstance (dateformat.full,dateformat.full,locale.us); String s = Df.format (now); System.out.println (s); System.out.println ("--------------------"); String S1 = "Monday, January 6, 4:44:48 PM CST";D ate d = df.parse (S1); System.out.println (d); label for date internationalization <%pagecontext.setattribute ("Now", new Date ()),%><fmt:formatdate value= "${ Now} "type=" both "datestyle=" full "timestyle=" full "/><br/><fmt:formatdate value=" ${now} "pattern=" yyyy mm month DD Day "/><br/>
4. Digital formatting
int money = 10000; NumberFormat NF = numberformat.getcurrencyinstance (locale.us); String s = nf.format (money); System.out.println (s); String S1 = "$10,000.00"; Long m = (long) nf.parse (S1); System.out.println (m); <%pagecontext.setattribute ("num", 10000);%><fmt:formatnumber value= "${num}" Groupingused= "true" type= "Currency" ></fmt:formatNumber>

Javaweb Custom Labels &JSTL& internationalization (ix)

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.