struts1.3 Custom Label

Source: Internet
Author: User
Tags tag name tld

One, web.xml add custom label file

  <jsp-config>
		     <taglib>
		       <taglib-uri>/WEB-INF/bing</taglib-uri>
		       < taglib-location>/web-inf/bing.tld</taglib-location>
		     </taglib>
  </jsp-config>

Second, Bing.tld

<?xml version= "1.0" encoding= "UTF-8"?> <!
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.3</tlib-version>
    < jsp-version>1.2</jsp-version>
    <short-name>bean</short-name>
    <uri>bing</ uri>
    
    <tag>
        <name>hello</name>
       <tag-class>com.bing.tags.hellotag</ tag-class>
       <body-content>JSP</body-content>
       <attribute> 
         <name>name </name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue><! --whether to accept dynamic values, such as El expression  -->
         <type>java.lang.String</type>
       </attribute>
    </tag>
</taglib>
Explain:
URI: The address to use for the introduction of the page. The
name attribute in tag: Defines our tag name, which is used later. The
tag-class attribute in tag: Specifies the implementation class for our tag.
bod-ycontent attribute in tag: Specifies what the nature of our page content is. (Note: In JSP development, you must write JSP here)
Attribute attributes in tag: Defines the possible properties of our tag. Attribute
: Specifies the name of the property.
Attribute Required: Indicates whether this attribute is required.

Third, the Label class

Package com.bing.tags;

Import java.io.IOException;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import javax.servlet.jsp.tagext.BodyContent;
Import Javax.servlet.jsp.tagext.BodyTagSupport;

Import Javax.servlet.jsp.tagext.TagSupport;

Import Org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;  
/** * Skip_body implicitly 0: Skips the code between the start and end tags.  
Eval_body_include implied 1: The content of the body output to the existing output stream Skip_page implied 5: Ignore the remaining pages. Eval_page implied 6: Continue to execute the following page Eval_body_again 2 eval_body_include: Read the body into the existing output stream, doStartTag () function can be eval_page: Continue processing the page, The Doendtag () function can be skip_body: ignores processing of the body, doStartTag () and Doafterbody () functions are available skip_page: ignoring processing of the remaining pages, the Doendtag () function is available eval_body_ Tag: has been abolished, replaced by eval_body_buffered eval_body_buffered: Application buffer, by the setbodycontent () function to get the Bodycontent object to handle the tag body, If the class implements Bodytag, then doStartTag () is available, otherwise the lifetime of the custom tag for an illegal extension bodytagsupport is as follows: 1. Create tag 2. Call setter Method 3. Call doStartTag () Method 4. Invoke the Setbodycontent () method 5. Call Initbody () method 6. Handle the body 7.doAfterBody () of the token, and, depending on the return value, if Eval_body_again, continue to perform 6, if not, holdLine 8 8. Call Doendtag () Method 9. Determine if the token needs to be reused, execute 4 if necessary, or execute the release () method.  * @author ABC * */public class Hellotag extends bodytagsupport{@Override public void setbodycontent (Bodycontent b)
			{try {b.append (' C ');
			catch (IOException e) {e.printstacktrace ();
	@Override public void Doinitbody () throws Jspexception {System.out.println ("doinitbody");
	@Override public int doafterbody () throws jspexception {return eval_page;
	@Override public void Release () {super.release ();
	@Override public bodycontent getbodycontent () {return super.getbodycontent ();
	@Override public JspWriter Getpreviousout () {return super.getpreviousout ();
	private String name;
	Public String GetName () {return name; public void SetName (string name) {try {this.name = (string) expressionevaluatormanager.evaluate (' name '), name.to
		String (), Object.class, this, PageContext);
		catch (Jspexception e) {e.printstacktrace (); }} @OverrIDE public int doStartTag () throws Jspexception {JspWriter out=pagecontext.getout ();
		try {out.println ("hello" +name);
		catch (IOException e) {e.printstacktrace ();
	return 1;
		@Override public int Doendtag () throws jspexception {release ();
	return 6;
 }
	

}
Iv. use <%@ taglib prefix= "Bing" uri= "/web-inf/bing"%> note that the URI here is the Web.xml value defined in Taglib-uri
<bing:hello name= "${name}" >aaaaaaaaaaaaaa</bing:hello><br/>bbbb

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.