I have been studying a custom label recently, so I want to try to realize the label function of some JSP, and the reflection mechanism is also reviewed. (If you are not familiar with these two technologies, there are some Example and PPT documentation for reflection and custom labels for the lowermost side of this blog.
The function to be implemented today is the usebean tag. The following table is some of its properties and purposes. (I only selected a more important attribute, did not implement all attributes)
The attribute use ID gives a name that will apply bean variables, if you find the same ID and SCOPE bean object, apply this object without generating a new exemplary. Class indicates the entire package name of Bean. Scope indicates the scope of this bean, a total of four values: Page, Request, Session, and Application, the default is the Page property, indicating that this bean can only be applied to the current page (saved in the current page); Request Attributes indicate that this bean can only be applied to the current user request (saved in the servletRequest object); the session property indicates all the pages in the current httpsession life cycle; Application property value indicates that this bean can be applied to share servletContext All pages. It should be noted that when there is no identical ID and scope object, a JSP: UseBean entity can only act in a new exemplary, in turn, acting on the previous object, at this time, between JSP: Usebean tag Any JSP: SetParameter and other entities will be ignored. Type Description The variable type of the object will be indexed, which must match the class name and the parent class name. Remember, the name of this variable is replaced by the id attribute value. BeanName gives the name of this bean, which can be provided to the depth of the bean, only the case of BeanName and Type and ignore the class attribute are allowed.
Below is the label processing method class: usebean.java:
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Import java.lang.reflect. *;
//
Public Class UseBean Extends Tagsupport {// Inherited from Tagsupport Class
PRIVATE STRING Scope;
PRIVATE STRING TYPE;
Public usebean () {super ();
/ **
* Set the attribute access method, this method is automatically called by the container. SETID () and getId () are automatically implemented by the system
* /
Public void setscope (string s) {
THIS.SCOPE = S;
}
Public string getscope () {return this.scope;}
Public void settype (String type) {
THIS.TYPE = TYPE;
}
Public string gettype () {return this.type;
/ **
* Cover the DostartTAG method
* /
Public int dostarttag () THROWS JSptagexception
{
Object o = NULL;
// Find the bean in the specified scope
IF (Scope.equals ("Page")) {
o = pagecontext.getattribute (getId (), pageContext.page_scope);
} else IF (Scope.equals ("Request")) {
o = PageContext.getattribute (getId (), pageContext.request_scope);
} else if (Scope.equals ("session")) {
o = pagecontext.getattribute (getId (), pageContext.Session_scope;
} else if (scope.equals ("application")) {
o = PageContext.getattribute (getId (), pageContext.Application_scope;
}
IF (o == NULL)
{
System.out.println ("o is null!");
Try {
Class u = class.Forname (TYPE);
o = u.newinstance (); // no parameter construction method
System.out.println ("CREATE SUCCESS!");
}
Catch (Exception E) {
Throw new JSptagexception ("Error to create a" getId () "Object!");
}
}
PageContext.setttribute (getId (), o); // save instance objects into context objects
RETURN EVAL_BODY_INCLUDE; / / Return Type
}
}
Now we have put the object instance in pageContext, is it necessary to reference directly in the JSP page? Of course, it is different from the Java object in PageContext to be directly referenced in the script. The difference is that the JSP container is responsible for obtaining this object and is provided to the page in the form of a script variable. That is, the JSP container is responsible for maintaining the status of the script variable to the corresponding object in the pageContext. There are two ways to declare script variables for custom tags.
One is a declaration of Variable, a declared variable through the TAGEXTRAINFO class. The former is a method after jdk1.2, and the advantage is relatively convenient. The latter is troublesome, but more flexible, more flexible, but more flexible, suggestions, but also adopt the latter. (For details, please refer to the PPT documentation)
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Public class usebeantag extends TAGEXTRAINFO {
Public variableinfo [] getvariableinfo (tagdata data) {
Return new variableinfo [] {
New variableinfo
Data.GetId (),
Data.GetaTRibuteString ("type"),
True,
VariableInfo.at_begin)
}
}
}
Now, define a USEBean tag has been made in most, the definition label description (TLD) file below, the file is an XML document, which is the statement of the label, processing class, and expansion information classes. The main declarations are as follows: (tag.tld)
........................ (省 去 去 标 部分)
attribute>
attribute>
attribute>
tag>
In fact, this label library description file should be the first to build, because we are mainly to illustrate the method of implementation, so the label description is behind. The next is to deploy these things you have to go to our application. Quote it is OK in the target JSP page.
<% @ Taglib Uri = "/ Web-INF / TAG.TLD" prefix = "dever"%>
OK, until this, our custom Usebean label is already able to start working, is it easy, in fact, it is not difficult, as long as everyone has mastered the process and configuration method of defining the label, the rest is just written label processing Class, in the next few blog, I will no longer detail these deployments and configuration, directly give the tag handling class code.
Custom label PPT
Reflective mechanism EXAMPLE