Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize tags for JSP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "JSP how to customize the tag", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JSP how to customize the tag" bar!

Create "Hello" tag

Next, we want to create a custom label called, the label format is:

To create a custom JSP tag, you must first create a Java class that handles the tag. So, let's create a HelloTag class, as follows:

Package com.runoob;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.io.*;public class HelloTag extends SimpleTagSupport {public void doTag () throws JspException, IOException {JspWriter out = getJspContext () .getOut (); out.println ("Hello Custom Tag!");}}

The following code overrides the doTag () method, which uses the getJspContext () method to get the current JspContext object and sets the "Hello Custom Tag!" Passed to the JspWriter object.

Compile the above classes and copy them to the environment variable CLASSPATH directory. Finally, create the following tag library: webapps\ ROOT\ WEB-INF\ custom.tld.

1.0 2.0 Example TLD Hello com.runoob.HelloTag empty

Next, we can use the Hello tag in the JSP file:

"ex" uri= "WEB-INF/custom.tld" > A sample custom tag

The output of the above program is as follows:

Hello Custom Tag! Access tag body

You can include the message content in the tag like a standard tag library. If we want to include content in our custom Hello, the format is as follows:

This is message body

We can modify the tag handling class file as follows:

Package com.runoob;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.io.*;public class HelloTag extends SimpleTagSupport {StringWriter sw = new StringWriter (); public void doTag () throws JspException, IOException {getJspBody () .invoke (sw); getJspContext () .getOut () .println (sw.toString ());}}

Next we need to modify the TLD file, as shown below:

1.0 2.0 Example TLD with Body Hello com.runoob.HelloTag scriptless

Now we can use the modified tag in JSP, as shown below:

"ex" uri= "WEB-INF/custom.tld" > A sample custom tag This is message body

The output of the above program is as follows:

This is message body Custom tag Properties

You can set various properties in the custom standard. To receive attributes, the value custom tag class must implement the setter method. The setter method in JavaBean is as follows:

Package com.runoob;import javax.servlet.jsp.tagext.*;import javax.servlet.jsp.*;import java.io.*;public class HelloTag extends SimpleTagSupport {private String message; public void setMessage (String msg) {this.message = msg;} StringWriter sw = new StringWriter (); public void doTag () throws JspException, IOException {if (message! = null) {/ * use message * / JspWriter out = getJspContext (). GetOut () Out.println (message);} else {/ * use messages from the body * / getJspBody (). Invoke (sw); getJspContext (). GetOut (). Println (sw.toString ());}

The name of the property is "message", so the setter method is setMessage () of. Now let's add this attribute to the element we used in the TLD file:

1.0 2.0 Example TLD with Body Hello com.runoob.HelloTag scriptless message

Now we can use the message attribute in the JSP file, as shown below:

"ex" uri= "WEB-INF/custom.tld" > A sample custom tag "This is custom tag" / >

The output result of the above instance data is as follows:

This is custom tag

You can also include the following properties:

Property description name defines the name of the property. Each tag is that the attribute name must be unique. Required specifies whether the property is required or optional, and optional if set to false. Rtexprvalue declares whether the tag attribute is valid when the expression is run. Type defines the Java class type for this property. The default is specified as Stringdescription description information fragment. If this property is declared, the property value is treated as a JspFragment.

The following is an example of the property associated with the specified property:

. Attribute_name false type > java.util.Datetype > false.

If you use two properties, modify the TLD file as follows:

. Attribute_name1 false type > java.util.Booleantype > false attribute_name2 true type > java.util.Datetype >. Thank you for your reading, the above is the content of "JSP how to customize the tag", after the study of this article, I believe you have a deeper understanding of how to customize the label of JSP, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report