Pragmatic Snippets
Previously, I showed my top seven snippets. Let us use some of the snippets together to make a new component. To follow along, you should install the snippets.
Next, we will consider our output. We will design a form bean component for a user management form consisting of the following values:
- FullName
- Status
- Username
Create a new component called snippet.cfc. Paste the four attributes listed above three times in snippet.cfc . Your file should look like the one below:
Fullname
Status
Username
Fullname
Status
Username
Fullname
Status
Username
</cfcomponent>
Begin at the last block. This will be as block of getters and setters. To be as efficient as possible, put your cursor at the beginning of Email, select the word, cut it, type getset and your snippet key combo. (Either CTRL+SHIFT+. or CTRL+J). When the prompt appears, paste the text Email, then type an s character in each box to select 'string'. Repeat this for each of the four attributes.
When complete, and with some judicious spacing, you end up with this:
Fullname
Status
Username
Fullname
Status
Username
<!--- Usage: GetEmail / SetEmail methods for Email value --->
<cffunction name="getEmail" access="public" output="false" returntype="string">
<cfreturn variables.instance.Email />
</cffunction>
<cffunction name="setEmail" access="public" output="false" returntype="void">
<cfargument name="Email" type="string" required="true" />
<cfset variables.instance.Email = arguments.Email />
</cffunction>
<!--- Usage: GetFullname / SetFullname methods for Fullname value --->
<cffunction name="getFullname" access="public" output="false" returntype="string">
<cfreturn variables.instance.Fullname />
</cffunction>
<cffunction name="setFullname" access="public" output="false" returntype="void">
<cfargument name="Fullname" type="string" required="true" />
<cfset variables.instance.Fullname = arguments.Fullname />
</cffunction>
<!--- Usage: GetStatus / SetStatus methods for Status value --->
<cffunction name="getStatus" access="public" output="false" returntype="string">
<cfreturn variables.instance.Status />
</cffunction>
<cffunction name="setStatus" access="public" output="false" returntype="void">
<cfargument name="Status" type="string" required="true" />
<cfset variables.instance.Status = arguments.Status />
</cffunction>
<!--- Usage: GetUsername / SetUsername methods for Username value --->
<cffunction name="getUsername" access="public" output="false" returntype="string">
<cfreturn variables.instance.Username />
</cffunction>
<cffunction name="setUsername" access="public" output="false" returntype="void">
<cfargument name="Username" type="string" required="true" />
<cfset variables.instance.Username = arguments.Username />
</cffunction>
</cfcomponent>
Not too shabby eh? Now we need an init function. Cut both sets of remaining attributes into the clipboard. Type fun and your snippet trigger text. When the dialogue box appears, fill in the following information:
Name: init
Access: public
Return Type: snippet
Required: False
<!--- Date: 7/9/2007 Usage: I perform initialization --->
<cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
<cfargument name="Email" type="string" required="false" default="" />
<cfargument name="Fullname" type="string" required="false" default="" />
<cfargument name="Status" type="string" required="false" default="" />
<cfargument name="Username" type="string" required="false" default="" />
Fullname
Status
Username
</cffunction>
<!--- Usage: GetEmail / SetEmail methods for Email value --->
<cffunction name="getEmail" access="public" output="false" returntype="string">
<cfreturn variables.instance.Email />
</cffunction>
<cffunction name="setEmail" access="public" output="false" returntype="void">
<cfargument name="Email" type="string" required="true" />
<cfset variables.instance.Email = arguments.Email />
</cffunction>
<!--- Usage: GetFullname / SetFullname methods for Fullname value --->
<cffunction name="getFullname" access="public" output="false" returntype="string">
<cfreturn variables.instance.Fullname />
</cffunction>
<cffunction name="setFullname" access="public" output="false" returntype="void">
<cfargument name="Fullname" type="string" required="true" />
<cfset variables.instance.Fullname = arguments.Fullname />
</cffunction>
<!--- Usage: GetStatus / SetStatus methods for Status value --->
<cffunction name="getStatus" access="public" output="false" returntype="string">
<cfreturn variables.instance.Status />
</cffunction>
<cffunction name="setStatus" access="public" output="false" returntype="void">
<cfargument name="Status" type="string" required="true" />
<cfset variables.instance.Status = arguments.Status />
</cffunction>
<!--- Usage: GetUsername / SetUsername methods for Username value --->
<cffunction name="getUsername" access="public" output="false" returntype="string">
<cfreturn variables.instance.Username />
</cffunction>
<cffunction name="setUsername" access="public" output="false" returntype="void">
<cfargument name="Username" type="string" required="true" />
<cfset variables.instance.Username = arguments.Username />
</cffunction>
</cfcomponent>
We will finish off the component by adding the cfset tags that execute the setters. Paste this block 4 times:
<cfset setFullname( arguments.Fullname ) />
<cfset setStatus( arguments.Status ) />
<cfset setUsername( arguments.Username ) />
<cfreturn this />
Congratulations! Wasn't that fun? When complete, you should have a nice Form Bean ready to use. For posterity, the complete code looks like this:
<!--- Date: 7/9/2007 Usage: I perform initialization --->
<cffunction name="init" output="false" access="public" returntype="snippet" hint="I perform initialization for this component">
<cfargument name="Email" type="string" required="false" default="" />
<cfargument name="Fullname" type="string" required="false" default="" />
<cfargument name="Status" type="string" required="false" default="" />
<cfargument name="Username" type="string" required="false" default="" />
<cfset setEmail( arguments.Email ) />
<cfset setFullname( arguments.Fullname ) />
<cfset setStatus( arguments.Status ) />
<cfset setUsername( arguments.Username ) />
<cfreturn this />
</cffunction>
<!--- Usage: GetEmail / SetEmail methods for Email value --->
<cffunction name="getEmail" access="public" output="false" returntype="string">
<cfreturn variables.instance.Email />
</cffunction>
<cffunction name="setEmail" access="public" output="false" returntype="void">
<cfargument name="Email" type="string" required="true" />
<cfset variables.instance.Email = arguments.Email />
</cffunction>
<!--- Usage: GetFullname / SetFullname methods for Fullname value --->
<cffunction name="getFullname" access="public" output="false" returntype="string">
<cfreturn variables.instance.Fullname />
</cffunction>
<cffunction name="setFullname" access="public" output="false" returntype="void">
<cfargument name="Fullname" type="string" required="true" />
<cfset variables.instance.Fullname = arguments.Fullname />
</cffunction>
<!--- Usage: GetStatus / SetStatus methods for Status value --->
<cffunction name="getStatus" access="public" output="false" returntype="string">
<cfreturn variables.instance.Status />
</cffunction>
<cffunction name="setStatus" access="public" output="false" returntype="void">
<cfargument name="Status" type="string" required="true" />
<cfset variables.instance.Status = arguments.Status />
</cffunction>
<!--- Usage: GetUsername / SetUsername methods for Username value --->
<cffunction name="getUsername" access="public" output="false" returntype="string">
<cfreturn variables.instance.Username />
</cffunction>
<cffunction name="setUsername" access="public" output="false" returntype="void">
<cfargument name="Username" type="string" required="true" />
<cfset variables.instance.Username = arguments.Username />
</cffunction>
</cfcomponent>







I have followed your tips and unzipped the files to my Eclipse, then restart my Eclipse. However, after I press the CTRL+J, I got Error message:
"Insert Snippet" did not complete normally. Please see the log for more information.
Reason: java.lang.NullPointerException.
Could you let me know if I miss anything?
Thanks,
Jerry
You will have to manually edit each snippet to put in the trigger text and the name/description. If you have not done this, navigate to the snip tree view, and use the icons to create a new snip for each snippet defined in the tutorial.
Hope this helps,
DW
Thanks again for this helpful stuff,
Jerry
<cfset setX() = arguments.x />
should be
<cfset setX(arguments.x) />
Thanks for pointing this out. I've updated the post.
Happy Snippeting!
DW