XDoclet learning notes (using xdoclet)

xiaoxiao2021-03-05  23

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 = " form"

* /

Public class form extends actionform {

Private

Public () {

Return

}

Public void ( value) {

= value;

}

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"%>

>>

TAGNAME = "dw.genstruts" paramname = "formlabel" />

Size = "

"/>

Submit

RESET

Note that the code value specified in the XDoclet tag in the original addressBean.java code is obtained in the code in .

When you perform the GenStruts Ant target, the GenFormJsp.xDT template displayed by Listing 5 is also handled. You can find the generated addressBeanForm.jsp file in the generated subdirectory, check the contents of the file, you can see the replacement of the template.

Example, but first ... To test the template code generation in this example, you need to install and test the ANT and XDoclets. The code in this article is all based on ANT 1.5.4 and XDoclet 1.2.1. To generate all artifacts, and test the generated web application, you also need to install Hibernate, Struts, and Application Server, and you need to access the relational database. Example is based on Hibernate 2.1.4, Struts 1.1, and Servlet 2.3. See Resources for more information on these technologies. Be sure to read the ReadMe file I provide with the source code. Generate other workpieces You can generate any text-based output with an xdoclet. I use the XDoclet to generate Java code, JSP page, XML file, configuration file, and other more outputs using the XDoclet. It creates a complete data portal web application from a simple Java source file that tagged with xdoclet. In order to do this, it performs the XDoclet's built-in template (in the JAR file, called a module) to generate:

Struts configuration and support files. Hibernate configuration and support files. The deployment descriptor (Web.xml) of the web application.

Table 1 shows all files generated for the sample application (usually called artifact):

Table 1. Workpools generated by xdoclets for AddressBean.java

The generated workpiece description position addressBeanForm.javajava source file, contains a form bean class, using the generated directory AddressBeanForm.jspjSP form in Struts, with the Struts tag library Accept user address Enter the JSP directory AddressBeanAction.javastruts action class, accept the input value, Save the value to the relational database generated directory addressBean.hbm.xmlhibernate mapping file, mapping Web / Classes directory DWSCHEMA.SQLRDBMS table architecture in the AddressBean Java object and database, used to use the architecture of the AddressBean object An instance for persistent SQL directory hibernate.cfg.xmlhibernate runtime configuration file Web / class Directory Web.xml Deployment Descriptor Web Directory Struts-Config.xmlstruts Frame Web Directory

In this article, you know more about the generation of the first workpiece in the two artifacts listed in Table 1, in-depth understanding of the templates that generate them. Workpiece AddDressBeanAction.java uses a similar method to generate templates called GenAction.xdt. XDoclets have built-in templates and label processors that can generate other workpieces in Table 1.

Table 2 lists the ANT targets and ANT tasks corresponding to each generated workpiece. You can perform each Ant target in the table to generate a corresponding workpiece. All of these generated workpieces plus the original and addressBean.java, together forms an example web application. You will also find the default Ant target called ALL, which will do anything, including establish WAR to the application (web archive that can be deployed). Before working, you must read the Readme.txt file in the code release package.

The merge points in the XDoclet are in the xdoclet documentation, you will see the merge point (Merge file) very frequently. The merge file is a text file, you can merge it into the specified location of the xdoclet generating code - "consolidated points" (specified by the template). You can use a merged file to include static text (eg, code snippet and XML pieces), which may be difficult or unable to generate with xDoclet. For example, in the Merge / Web Directory of the sample code, you will find the struts-action.xml file. During the code generation, you can use this file to merge into the Struts action map, which constitutes a part of the generated struts-config.xml file. Table 2. Ant directory and ANT tasks corresponding to the generating workpiece

Ant task Ant target workpiece genstrutsxdoclet.DocletTaskAddressBeanForm.javagenstrutsxdoclet.DocletTaskAddressBeanForm.jspgenstrutsxdoclet.DocletTaskAddressBeanAction.javagenerateHIBxdoclet.modules.hibernate.HibernateDocletTaskAddressBean.hbm.xmlgenerateHIBxdoclet.modules.hibernate.HibernateDocletTaskhibernate.cfg.xmlcreateDDLxdoclet.modules.hibernate.HibernateDocletTaskdwschema.sqlgenerateDDxdoclet.modules.web .Webdoclettaskweb.xmlgeneratedDXDocuLet.Modules.Web.webdoclettaskstruts-config.xml

转载请注明原文地址:https://www.9cbs.com/read-38826.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.055, SQL: 9