Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences

Edit this page


Referenced by
...nobody




JSPWiki v2.2.33

[RSS]


Hide Menu

XDocletIndexedProperties


One of the things I've recently added to AppFuse is the ability to generate indexed-property-ready ActionForms using XDoclet. It's pretty simple really - if you're using XDoclet, you can add it too with the following steps:

Step 1: Add the following code to the bottom of your struts_form.xdt file. NOTE: You will need Velocity JARs in your classpath for this modified template to work.

    /**
     * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping,
     *                                                javax.servlet.http.HttpServletRequest)
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        // reset any boolean data types to false
<XDtStrutsForm:forAllFormFields>
        <XDtType:ifIsOfType value="return-type" type="java.lang.Boolean,boolean" extent="concrete-type">
        this.<XDtMethod:propertyName/> = false;
        </XDtType:ifIsOfType>
</XDtStrutsForm:forAllFormFields>
<XDtClass:ifHasClassTag tagName="struts.form" paramName="indexedProperties" value="true">
<XDtStrutsForm:forAllFormFields>
        <XDtType:ifIsOfType value="return-type" type="java.util.List,java.util.Set" extent="concrete-type">
        <XDtVelocity:generator>
        #set( $method = $currentMethod.name)
        ## trim off the 'get'
        #set( $objectName = $method.substring(3, $method.lastIndexOf('s')))
        </XDtVelocity:generator>
        this.<XDtMethod:propertyName/> = 
             org.apache.commons.collections.ListUtils.lazyList(new java.util.ArrayList(), 
                 new ObjectFactory("<XDtVelocity:getVariable name="objectName"/>"));
        </XDtType:ifIsOfType>
</XDtStrutsForm:forAllFormFields>
</XDtClass:ifHasClassTag>
    }

    <XDtClass:ifHasClassTag tagName="struts.form" paramName="indexedProperties">
    /**
     * <code>ObjectFactory</code> - used if indexed properties exist on this form.
     *
     * @see org.apache.commons.collections.ListUtils
     */
    class ObjectFactory implements org.apache.commons.collections.Factory {
        private String name;
        /**
         * Create a new instance of the specified object
         */
        public Object create() {
	    	Class c = null;
	    	try {
		    	c = Class.forName("org.appfuse.webapp.form." + name + "Form");
	                return c.newInstance();
		} catch (Exception e) {
	                System.err.println("Error instantiating class: " + c.getName());
	                throw new RuntimeException(e);
		}
        }
        public ObjectFactory(String name) {
        	this.name = name;
        }
    }
    </XDtClass:ifHasClassTag>

Step 2: For the generated Forms that have indexed properties (i.e. those with Hibernate many-to-one associations), add an attribute to the @struts.form tag: indexedProperties="true". If you name your Lists as plural forms of your Objects, everything should work fine. For instance, if you have a list of User objects, your List should be named "users" with appropriate getters and setters:


private List users;

public void setUsers(List users) {
    this.users = users;
}

public List getUsers(List users) {
    return users;
}

This stuff works really slick with Hibernates ability to Lazy load objects. In AppFuse the BaseAction has a convertLists() method that allows easy conversion from your indexed Forms to indexed POJOs and vise-versa.



Go to top   Edit this page   More info...   Attach file...
This page last changed on 06-Nov-2006 13:52:59 MST by MattRaible.