ANT Loyal Partner XDoclet Basic Task Manual (Transfer)
Recently, I use XDoclets in the use of AppFuse, which is used, although used before, but there is no XDoclet to generate like Appfuse. It is really convenient to write the program now. I want to write my own harvest, but I found that the online high-end people have already prepared (one after another, huh, huh, it is late to come to master), the current repost is as follows:
Anyone wants to generate code, you should write an analyzer, it is not easy, and XDoclet gives us a chance to build its own code generator.
XDoclets were first used to deal with EJB, because the local and remote interfaces that generated EJB and the corresponding home and descriptors were simple but boring, I thought that only JBuilder's integrated editor can be very simplified, later I know that xdoclet can also be completed.
My earliest uses xdoclets because of learning hibernate, hibetnate's description file is not complicated, but the key part is not much, but once the POJO is modified, it is necessary to find the corresponding descriptor to find the corresponding descriptor, add an error chance, and You can make synchronous modifications using xDoclets.
There is also struts, first is the configuration file, many people operate the same file will have conflicts. We are not afraid of xDoclet, and the validate file is the same. With xDoclets will solve most of the conflict.
The reason why this name is mainly because XDoclet and Ant are combined very tightly, although there is no direct connection, XDoclet is only some Maven interface plugins in addition to the ANT interface, so xdoclet is almost completely dependent on Ant .
The directory structure of this article is as follows, because it is just to explain the problem, there is no reference to the path name in my Ant's build file, everything is a direct way.
├ --classes
├─DOC
├─gen
├-lib
│ Commons-Collectes-2.1.jar
│ commons-logging-1.0.3.jar
│ comMMons-Validator.jar
│ Log4j-1.2.8.jar
│ servlet.jar
│ Struts.jar
│ XDoclet-1.2.1.jar
│ xdoclet-apache-module-1.2.1.jar
│ XDoclet-Ejb-Module-1.2.1.jar
│ xdoclet-hibernate-module-1.2.1.jar
│ xdoclet-web-module-1.2.1.jar
│ xdoclet-xdoclet-module-1.2.1.jar
│ xjavadoc-1.0.3.jar
├─merge
├ --SRC
├─todo
└─Web
Only the files in the lib are listed later later.
Build.xml
Beginning an increase
fileset>
path>
*********************************************************** ************************************************1, The simplest Todolist
Every article tells XDoclets to start here, there are many reasons. The inspiration of XDoclets comes from Javadoc, Javadoc writes documents in code to mitigate the multi-year documentation and program synchronization issues for many years in the field of programs. Here is a very interesting thing, that is, people in the UNIX industry pass such a tradition, that is, the code is the best document, and the synchronization of the maintenance document is really costly, so they put forward such a good idea, but Javadoc is more Smart, document is part of the program comment and can be extracted.
Come on, see this task.
fileset>
documentdoclet>
target>
Then SRC writes such a file
Package xdoclet;
Public class todolisttest {
/ **
* @todo I have a lot of work to do, just test, ignore it.
* /
Public todolisttest () {
}
/ **
* @todo I still don't know the name, just test, ignore it.
*
* /
Public string getYourname () {
Return NULL;
}
}
Note To follow Javadoc's writing. Also pay attention to the subtasten system in Ant, where INFO is the subtraction of our DocumentDoclet task, we will see a lot of similar situations later.
Then run Ant Todolist
The result is a structure similar to Javadoc, but only includes the Todo tag, html document, huh, you can look at what is to do in the project.
2, Web.xml and Taglib
The servlet mapping is an annoying work. When you receive other items, how how the servlet's servlet may be more trouble, it may be found at the time of web.xml, what should I do?
See this file
Package com.xdocletbook.blog.servlet;
Import java.io.ioException;
Import javax.servlet.servletconfig;
Import javax.servlet.servletException;
Import javax.servlet.http.httpservlet;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse; import org.apache.log4j.level;
Import org.apache.log4j.logger;
/ **
* @ Web.Servlet
* Name = "HomePage"
* @ Web.Servlet-Init-PARAM
* Name = "loglevel"
* Value = "$ {log_level}"
* @ Web.Servlet-maping
* URL-PATTERN = "/ home"
*
* @ Web.Security-Role
* Role-name = "$ {oownerrole}"
* @ Web.Security-Role-Ref
* Role-name = "blogowner"
* Role-link = "$ {oownerrole}"
* /
Public Class HomepageServlet
Extends httpservlet {
Private static logger logger = logger.get.class;
Public void init () throws servletexception {
String loglevel = GetInitParameter ("loglevel");
IF (loglevel! = null && loglevel.Length ()> 0) {
Logger.Setlevel (Level.Tolevel (Loglevel);
}
}
Public void service (httpservletRequest Request, httpservletResponse response) throws
ServletException, IOException {
Logger.debug ("Displaying Home Page");
Request.getRequestDispatcher ("JSP / HOME.JSP"). Forward (Request, Response);
}
}
Then configure build.xml to add the following tasks
fileset>
WebDoclet>
target>
Then run the Ant Generate-Web
The result is such a web.xml
XML Version = "1.0" encoding = "UTF-8"?>
To Use Non xdoclet filters, create a filters.xml file That
Contains The Additional Filters (EG Sitemesh) and Place It in Your
Project's merge dir. don't include filter-mapings in this file,
INCLUDE THEM IN A File Called Fille-mapings.xml and put this in
The Same Directory.
->
To use non xdoclet filter-mapings, create a filter triappings.xml file this
Contains the additional filter-mapings and place it in Your
Project's merge dir.
->
To use non xdoclet listener, create a listener.xml file That
Contains The Additional Listeners and Place IT IN Your
Project's merge dir.
->
servlet>
init-param>
security-role-ref>
servlet>
servlet-maping>
servlet-maping>
To specify mAppings, create a file named mime-mapings.xml, Put IT IN Your Project's Mergedir.
Organize Mime-mapings.xml Following this DTD SLICE:
->
To Specify Error Pages, Create A File named error-pages.xml, Put IT IN Your Project's Mergedir.
Organize Error-Pages.xml Following this DTD SLICE:
->
To add taglibs by XML, Create a file called taglibs.xml and place it
IN your mer
->
TO SET UP Security Settings for your Web app, create.xml, Put IT IN Your Project's mergedir.
Organize Web-Security.xml Following this DTD SLICE:
->
security-role>
web-app>
Take a closer look at this file, you must be a surprised Struts configuration information, this is another way of xdoclet, for a third-party servlet, we have no way to deal with the original code, so we have a merge option, see
servlet>
servlet-mappings.xml
servlet-maping>
Also pay attention to $ {log_level}, this is to quote variables in Ant, so these two items have been added in front of my build.xml.
So, we can dynamically change the deployed LOG level
Struts Automatic configuration
Struts has two more important classes, Action and Form.
For Action, we need to configure the Action mapping and forward properties. We also need to register your name and check parameters for Form. Here is what we can do with xdoclet.
For Action we write such a Java file
Package com.xdocletbook.blog.servlet;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.action;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
/ **
* Simple Class To Test Jakarta Struts Generation (Jakarta Struts 1.2 Beta 2 Only).
*
* @ Struts.Action
* Path = "/ struts / foo"
* Name = "Userform"
* INPUT = "jsp / createblog.jsp"
*
* @ Struts.Action-Forward
* Name = "SUCCESS"
* Path = "/ struts / getAll.do"
* Redirect = "false"
*
*
* @ Struts.Action-Exception * type = "com.xdocletbook.blog.exception.ApplicationException"
* Key = "app.exception"
* path = "jsp / error.jsp"
*
* @ Struts.Action-form name = "blog.create"
* /
Public Final Class StrutsAction Extends Action
{
Public ActionForward Execute (ActionMapping Mapping, Actionform Form,
HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE
{
Return mapping.findforward ("Success");
}
}
The key part is the comment section.
See a task of increasing Build.xml,
fileset>
WebDoclet>
target>
Run Ant Generate-web, we get struts-config.xml in Gen
The key content is as follows
PATH = "/ struts / foo" TYPE = "com.xdocletbook.blog.servlet.strutsaction" Name = "Userform" Scope = "request" INPUT = "jsp / createblog.jsp" unknown = "false" Validate = "True" > Key = "app.exception" TYPE = "com.xdocletbook.blog.exception.ApplicationException" Path = "jsp / error.jsp" /> Name = "success" Path = "/ struts / getAll.do" REDIRECT = "false" /> action> If we have a lot of Action, you can generate such a file at any time, don't have to change this file in someone. At the same time, you don't have to worry that I accidentally forgot to change this document, because when you change Java, many default properties have changed this. Seeing this here, many feelings that I have used Workshop is not the case? When the flow is defined by a graphical interface, you will find some of the source code of the page stream. Special tag, this shows that Workshop has the same functionality, but Workshop provides a good interface without having to write comments, and Workshop is provided. Better syntax check, huh, just write a lot of Action together, there is some chaos. Another major part of Struts is Form, although I started something in trouble, dealing with the dynamic Form Some powerful, but after combining some related plugins, the effect It is really good. This is our FORM file, we also use ValidatorForm to do automatic verification. Package com.xdocletbook.blog.servlet; Import java.io.serializable; Import org.apache.struts.validator.validatorform; / ** * * @ Struts.form * Name = "blog.create" * / Public Class Blogcreateform Extends ValidatorForm Implements serializable { PRIVATE STRING NAME; Private string ooner; PRIVATE STRING EMAIL; Public blogcreateform () {} Public string getname () { Return this.name; } / ** * @ Struts.validator * Type = "required" * / Public void setname (String name) { THIS.NAME = Name; } Public string getowner () { Return this.owner; } / ** * @ Struts.validator * Type = "required" * / Public void setowner (String Owner) { this.owner = Owner; } Public string getemail () { Return this.Email; } / ** * @ Struts.validator * Type = "required" * @ Struts.validator * Type = "email" * / Public void setemail (String email) { THIS.EMAIL = Email; } } Then run the Ant Generate-Web This struts-config.xml has Name = "blog.create" TYPE = "com.xdocletbook.blog.servlet.blogcreateform" />
- If you have non xdoclets, define theims.xml andplace it in your mer GIGML ANDPLACE IN YOURE MERGE Directory. -> form-beans> There is a trap here that the Struts XDoclet handles Form-Beans, only the type is form, for the type of ValidatorForm if you don't put the corresponding class file Under ClassPath, xDoclets ignore it, so struts' packages must be placed in the classpath, so that xdoclets have the opportunity to know that ValidatorForm is the subclass of Form. There is also a SETXXX method, there are some comments that represent limitations, these help us have generated Validation.xml XML Version = "1.0" encoding = "UTF-8"?>
Validator Rules Configuration 1.0 // en "" http://jakarta.apache.org/commons/dtds/validator_1_0.dtd ">
Define Global Validation Config in Validation-Global.xml -> Depends = "Required"> field> Depends = "Required"> Key = "blog.create.Owner" /> field> Depends = "Required, Email"> field> form> formset> form-validation>