(1)
In fact, the development of custom tags does not require Struts2 support, in general, only need to inherit
Javax.servlet.jsp.tagext.BodyTagSupport class, rewrite Dostarttag,doendtag and other methods can be.
When implementing a custom label in struts2.x, the inherited 2 classes are org.apache.struts2.views.jsp.ComponentTagSupport and
Org.apache.struts2.components.Component.
Componenttagsupport:
is actually a encapsulation of the bodytagsupport, inheriting the Componenttagsupport class to get the property values set in the user-defined tag in the JSP page, and to wrap it into a component object.
Component:
The component class is inherited to obtain a relative value from the Valuestack in the Struts2.
(2) Analysis:
Tag class inside,
Attributes to be declared:
You need to define the properties of the tag settings that are coming from your JSP page. such as MaxLength, and the corresponding set get.
The two methods required are:
1.
Public Component Getbean (Valuestack stack, httpservletrequest req, httpservletresponse res);
This method is to get the object of a base class. In the basic class you need to have the constructor of the incoming Valuestack, if the basic class logic inside requires request or response, then need to have incoming valuestack stack, httpservletrequest req, HttpServletResponse Res's constructor;
2.
protected void Populateparams () This method is used to assign a property value from a JSP page to a base class. This is inherited, so first call super's corresponding method. I understand that the first method returned above is a global component object, which is the object used in the second method.
(3)
The implementation process is divided into two steps:
The first step is to generate the base class and assign the Valuestack value and other optional values to it.
The second step, the JSP page comes from the attribute value, through Componenttagsupport, using its getter,setter to obtain. Assigns a property value from a JSP page to a base class instance.
Instance:
(1)
Page:
<%@ taglib prefix= "Jason" uri= "/web-inf/tlds/time.tld"%>
<jason:jtime who= "HWJ" message= "Hello"/>
(2)
Jtimetag to define each corresponding attribute defined in the TLD file.
public class Jtimetag extends Componenttagsupport {
Private String message;
Private String who;
Public String GetMessage () {
return message;
}
public void Setmessage (String message) {
this.message = message;
}
Public String getwho () {
return to WHO;
}
public void setwho (String who) {
this.who = who;
}
Public Component Getbean (Valuestack arg0, HttpServletRequest arg1,
HttpServletResponse arg2) {
return new Jtimecomponent (ARG0);
}
protected void Populateparams () {
Super.populateparams ();
Jtimecomponent jtime = (jtimecomponent) component;
Jtime.setwho (WHO);
Jtime.setmessage (message);
}
}
(3)
Jtimetag to define the corresponding attributes defined in the TLD file. Because Jtimetag to assign it.:)
public class Jtimecomponent extends Component {
Private String message;
Private String who;
Private String A;
Public String GetMessage () {
return message;
}
public void Setmessage (String message) {
this.message = message;
}
Public String getwho () {
return to WHO;
}
public void setwho (String who) {
this.who = who;
}
Public jtimecomponent (Valuestack arg0) {
Super (ARG0);
}
The logic that you need to output is passed through the writer output string.
Public Boolean start (writer writer) {
Boolean result = Super.start (writer);
try {
A=this.getstack (). findString ("PageSize");
Writer.write (ToHTML (who,message));
} catch (IOException ex) {
}
return result;
}
Protected string ToHTML (string who,string message) {
Date Date=new date ();
Return who+message+ "The current time is:" +date+ "page number:" +a;
}
}
(4)
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>my</short-name>
<uri>/jtime</uri>-No effect
<display-name> "Jason time Tab" </display-name>
<tag>
<name>jtime</name>
<tag-class>com.coship.dhm.dm.common.taglib.JtimeTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>message</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>who</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
(5)
Execution process:
1
When the page opens, load into <%@ taglib prefix= "Jason" uri= "/web-inf/tlds/time.tld"%>;
When the program reads "<jason", it is defined to time.tld-->jtimetag.java; according to the label above.
2.
Jtimetag inherits Componenttagsupport, the JSP page label property is fetched, because this class has defined properties and their getter,setter-
Generates an instance of a base class, assigning values to the base class instance.
3.
The base class instance, execute the Start () method, and execute Super.start () to get the properties in the action and customize the information with the Writer.write () output.
Original URL: http://www.fengfly.com/plus/view-169588-1.html