Using XDoclet now, you can observe the actual work of xDoclet by studying the data entry application examples I provide to you. (To download the Java code used in this example, the xdoclet template, and Ant script, click the Code icon at the top or bottom of this article, or see the Download section.) We will start from the Java code shown in Checklist 1, this part The code represents the address of a customer. This address is encoded into a JavaBean component, where the xdoclet tag is displayed in black body:
Listing 1. ADDRESSBEAN.JAVA source file with xDoclet tag
Package com.ibm.dw.beans;
Import java.io.serializable;
/ **
* @ dw.genstrutsaction action = "/ addressdress.do"
* @ hibernate.class table = "address"
* /
Public class addressBean imports serializable {
Private string streetnumber = "";
Private string street = "";
Private string city = ""
Private String Country = "";
Private string postalcode = ""
Private long id = 0;
Public addressBean () {
}
/ **
* @ dw.genstruts forml = "street number"
* @ hibernate.property Length = "10"
* /
Public string getStreetNumber () {
Return streetnumber;
}
Public void setstreetNumber (String INPSTREETNUMBER) {
Streetnumber = INPSTREETNUMBER;
}
/ **
* @ dw.genstruts forml = "street"
* @ hibernate.property length = "40"
* /
Public string getStreet () {
Return street;
}
Public void setstreet (String INPSTREET) {
street = INPSTREET;
}
... more address bean printerties ......
/ **
* @ hibernate.id generator-class = "native"
* /
Public long getId ()
{
Return ID;
}
Public void setid (long inID) {
ID = InID;
}
}
In Listing 1, it is important to note that the XDoclet tag is to be embedded in the comment, tight in front of the related code element (such as a field, method, interface, or class). When parsing the source code, the xdoclet creates an attribute for each tag and attached the property to the code element of the structural model. Now, please pay attention to the @ dw.genstruts label because this is the first template that will be used in this example. Generate another Java class For this example, you need to generate a new Java class code - a Struts form bean. Struts will save and transfer user input with this bean. Bean must contain all data fields in the form of a bean property, and it must be subclasses of org.apache.struts.Action.ActionForm.
In order to generate a form bean code, you will generate an xdoclet template according to the pseudo code shown in Listing 2. The black body in parentheses represents the text of the control flow logic and you want to replace. Note how the template extracts information from the structural model of the parsed Java source code file:
Listing 2. Establish a pseudo code template for addressBeanForm.java struts form bean code
Package {package name of source class};
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.upload.formfile;
/ **
* Form bean class for {name of source class} ;.
*
* @ Struts.form name = "{name of source class} form"
* /
Public class {name of source class} form extends actionform {
{loop through all the methods in the source class}
{if the method is a javabean "getter" method}
{if The method Has been marked with the @ dw.genstruts tag}
Private {return type of method} {name of the javabean property};
Public {return type of method} {name of the getter method for this property} () {
Return {Name of JavaBean property};
}
Public void {name of the setter method for this property}
{Return Type of Method} value) {
{Name of the javabean proteness} = value;
}
{end of @ dw.genstruts}
{end of if javabean getter}
{end of loop}
Establish an XDoclet tab with XDoclet Please note that the xdoclet @ struts.form label generated in the template of Listing 2. You can use XDoclet to generate an XDoclet tag in the Java source code, and the xdoclet will process these tags again in later operations. When Structs-config.xml is established later, XDoclet uses the @ Struts.Form tab. The code in the loop in Listing 2 generates a field declaration and an accessor method, and generates a setter method for each accessor method of each @ dw.genstruts tag in the input source code.
Listing 2 uses an easy-to-understand pseudo code to represent template replacement tags. The actual XDoclet template tag is quite cumbersome. Listing 3 shows the GenFormBean.xdt template (all xdoclet templates are saved in the .xdt file). I have used the black body to emphasize the XDoclet template tag to facilitate the reference in pseudo code.
Listing 3. Establish a practical xdoclet template code for the Structs form bean Java code
Package
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.upload.formfile;
/ **
* Form bean class for
*
* @ Struts.form name = "
* /
Public class
Private
Public
Return
}
Public void
}
Familiar with the ANT script to write ANT scripts build.xml (I have provided this file as the sample code this article, see the link to find the code) Define all the necessary ANT targets used by the sample application. If you want to modify the script, you need to be familiar with Ant. See the reference information to learn more about Ant.
You can refer to the XDoclet's "Template Language" document, find a list of all available tags available (see Resources). To run a template for the AddressBean.java source file, use the following ANT command line:
Ant -dbase.class.java = address genstruts
This command can perform a custom ANT target (see the sidebar familiar with Ant script) to process the GenbeanForm.xdt template. The XDoclet provided by the ANT task called XDoclet.Doclettask, which is used to run the template file. If you are interested in the details of Ant, see the build.xml file in the sample code for more information.
When the XDoclet handles the template, it generates an AddressBeanForm.java file under the subdites called Generated. Listing 4 shows the file, which contains all the texts replaced during template processing:
Listing 4. XDoclet generated Java source code with Struts form bean
Package com.ibm.dw.beans;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.upload.formfile;
/ **
* Form bean class for addressbean.
*
* @ Struts.form name = "addressBeanForm"
* /
Public class addressBeanform Extends actionform {
Private java.lang.string streetnumber;
Public Java.lang.String getStreetNumber () {
Return streetnumber;
}
Public void setstreetNumber (java.lang.string value) {
streetnumber = value;
}
Private java.lang.stringstreet;
Public Java.lang.String getStreet () {
Return street;
}
Public void setStreet (java.lang.string value) {
street = value;
}
...... More bean property .....
}
Generating a JSP page for a data sheet You can use the same addressBean.java source file, but use the GenFormJsp.xdt template to generate a data entry form JSP page. Listing 5 shows genformjsp.xdt:
Listing 5. Generate a JSP page using the Struts tag library to display the XDoclet template for the HTML form
<% @ page language = "java"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>