Customize your xDoclet tag

xiaoxiao2021-03-06  68

Source: http://www.javaresearch.org/Article/showArticle.jsp? Column = 2 & thread = 15082XDoclet is a tool for a specific tab in the Java source file and then generates a specified file. The xdoclet tag itself has provided some commonly used labels, such as @ ejb, @ hibernate, @ web, etc., but still can't meet our needs. For example, our latest project references a JavaScript verification framework, you can complete the client table verification by configuring a specific XML configuration file, but don't want the developer to learn a set of frames, so I want the developer to write in the source code. @javascript Label and then generate its configuration file. JavaScript client verification has always been a problem with a more headache in Web development. It is often full of many similar or even the same verification code. How to unify these codes, and how to do the code page. Good solution. Using this verification framework then the client development becomes configured Validation-config.xml This file is so easy. This article does not intend to tell how to use the JSValidation framework, interested friends can go to http://www.cosoft.org The official website of .CN / Projects / JSValidation Jsvalidation goes to learn. Although using this framework has been well completed by the client verification code, and provide the DTD file to perform XML files, but manual writing XML files is still very easy to errors, the efficiency is relatively low, and new developers must master a A new framework is not suitable for developers. Most of the framework like Struts or JSF needs to write a similar formbean thing. Take JSF as an example. If there is a text box in the form , then the corresponding Page class or there should be called class FormBean following code: private HtmlInputText txtUsername = new HtmlInputText (); / *** @return user name * / public HtmlInputText getTxtUsername () {return txtUsername;} / ** * @param text * / public void setTxtTeaName (Htmlinputtext text) {txtteaname = text;} If you can use XDoclet, you can automatically generate JSVALIDATION configuration files, and also beneficial to train new developers, add Ant Task, you can also form a daily construction.

The imaginary code should be the following: / ** * @ javascript.field * name = "frmzbaddglobalpage: txtteaname" * display-name = "User name" * * @ javascript.depend * name = "required" * * @return User Name * / Public HTMLINPUTTEXT GETTXTUSERNAME () {return txtusername;} By analyzing some tag packs comes with the XDoclet, you can only provide a custom xdoclet tag that you only need to provide three files: a class inherited in Xmlsubtask, one inherited in doclettask Class (for Ant), a template language file for an XDT.

In XmlSubTask class, first of all, define the template filename: private static String DEFAULT_TEMPLATE_FILE = "resources / validation-config.xdt"; defined dtd file name: private final static String DTD_FILE_NAME_20 = "resources / validation-config.dtd"; define generating a configuration file name: private static String GENERATED_FILE_NAME = "validation-config.xml"; then simply combined together either three files, the code in detail as follows: public JavascriptSubTask () {setTemplateURL (getClass () getResource (DEFAULT_TEMPLATE_FILE). ); setDestinationFile (GENERATED_FILE_NAME);} public void execute () throws XDocletException {setDtdURL (getClass () getResource (DTD_FILE_NAME_20));. startProcess ();} protected void engineStarted () throws XDocletException {System.out.println (Translator.getString (XDocletMessages.class, xdocletMessages.Generating_something, new string [] {getDestinationFile ();} To use Ant, you only need to simply code: / * * Create Date 2004-4-26 * / package paradise. XDoclet.Modules.javascript; import xdoclet.doclettask; / ** * @author Breeze * / public class JavascriptDocletTask extends DocletTask {public JavascriptDocletTask () {addSubTask (new JavascriptSubTask ());}} are defined as follows in the ant: Next, the core part is related to the XDT template language,

XDT files can be said to be one of the most important files for custom XDoclet tags. Take JavaScriptxDoclet as an example, briefly introduce the XDT template language: Traversing all classes containing tags (specified in Ant) Traverse all methods of the current class If the method traversed in the method contains the specified label all labels that travers the current method more Template language, refer to XDoclet's .xdt document, is a very well understood template language.

Next, start to customize your own label, create a new XTags.xml file, plus the beginning then write all custom labels, for example: JavaScript method javaScript.form form id form id true show- Error form error display true onfail Form Error Run Custom JavaScript Function false Note Places: " Method " represents the label on the method instead of the class.

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

New Post(0)