In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is about how to read the working principle of frame tag taglib. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
In Java Web application development, if the front and rear ends are not separated for data exchange, the page will generally be rendered through technologies such as JSP, FreeMarker, Velocity and so on. In the rendering process of the page, the taglib technology is often used.
For example, in order to control some of the display logic of the page and achieve an effect similar to if/else in the code, you will use the content in the core tag, like this:
To traverse a container data, you can directly use the foreach of the core tag
To format the data on the page, you can directly use the format tag to format the corresponding data.
Even in some MVC frameworks, there are some convenient tags for page data rendering that can be used directly. For example, the form tag in Spring can bind data directly.
So how does it work behind these labels?
The previous article has written about how JSP works (do you know how Tomcat handles Jsp files?), to sum up, it will generate JSP into Servlet files during execution, and then execute the corresponding service method to process the request. The part that involves the use of tags also generates the corresponding execution logic.
In fact, a tag corresponds to a Java class that implements the corresponding methods according to the specification. In the process of executing the tag, the Servlet generated by JSP will directly call the specified method of the tag corresponding class to output the corresponding content on the page according to the returned value. If it is to continue, the content will be output, and if it is skipped, it will be ignored. This is basically the way of thinking.
For example, the if tag at the beginning of this article corresponds to the generated Servlet content as follows
If you look at the foreach tag, the generated content is as follows:
We see that foreach is converted directly into a do while loop.
Among them, the two core methods are:
DoStartTag
DoEndTag
DoStartTag
This method is called by the specific tag implementation class within the JSP class. The tag for the instance begins, and when executed, the pageContext and a series of properties are considered to have been set.
Either EVAL_BODY_INCLUDE or SKIP_BODY is returned, and you can see from the variable names of these two results that if tag expects to continue processing body, it returns the former, otherwise it returns the latter.
DoEndTag is used to confirm whether the page continues to render after the tag is executed.
The whole taglib is easy to use. Declaring prefix and uri directly in JSP is equivalent to adding this part of the content dependency, and then directly using tags to set attributes, corresponding to the use of tags under the namespace, and so on.
The declaration of these tags, in a form like this, is stored in Jar files or WEB-INF.
The specific tag implementation class generally inherits TagSupport and then overrides the doStartTag and doEndTag methods we mentioned above.
So, for this so-called scriptlet, which usually has a lot of piles in a JSP page, you can directly define a tag and move the logic into the rewriting method.
The tld declared by Tag is usually scanned when the application is started after deployment, and then added to the Map.
When the page parsing is executed, it will determine whether the corresponding declared tld exists, and if not, the page execution will be stopped.
After the page parsing generates the Servlet class, the specific tag attribute settings and doStartTag methods will be called during execution. At this time, if some attributes do not exist, the binding is not successful, and these specific tag logic will be exposed.
For example, when we use Spring tags, we often use their form tags for parameter binding. In order to experiment, we randomly write a Spring tag and use it.
At this point, the content generated by JSP looks like this
Then when the page is rendered, it will really do data binding, judge the validity of these attributes, and so on. Here, because it is a random line of code, it is impossible to bind all errors.
Therefore, for the tags of these frameworks such as Spring, Struts2, and so on, the implementation is essentially the same logic. If there is a problem with the rendering of the page, know clearly at which step the problem occurs, and know how these tags work.
For example, the InputTag inheritance relationship of Spring in the following figure does not deviate from the Tag box of Servlet.
To sum up, how Taglib works is to parse the declaration of tld when the application is deployed. When the page is rendered, parsing will report an error if the introduced tld does not exist. After tld is legal, the specific logic of tag is executed to determine whether to continue the rendering of the page according to the returned value.
The above is how to understand the working principle of the frame tag taglib. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
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.