Some Live Templates for IDEA for Managers and Struts

Writing Java code is often repetitive and tedious. IDEA can make parts of it easier with Live Templates. I think most people use the built-in ones regularly, but we don’t often write our own Live Templates. Here are a few templates that I have created, while writing a bunch of Struts Actions, adding a bunch of Managers, and writing tests for those Managers. If anyone else has any Live Templates you have created, I would like to know about them.

To use any of these, open up your Settings panel in IDEA. Under IDE Settings, go to Live Templates. Expand the "user" tree, and click on the Add button.

Give the Live Template an abbreviation that you will remember, and paste in the code into the Template Text box. Then you may want to set up some of the variables that get expanded, by clicking Edit Variables.

To use a Live Template that you have created, type the abbreviation into the code you are writing, and hit Tab (this is the default button to expand Live Templates, but you can change it if you like).

 

 

New Action Method

This template will create the skeleton of a new method in an action.

Template Text:

public ActionForward $method_name$(ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                throws Exception {

        $form_name$ $form_var_name$ = ($form_name$) form;

        return mapping.findForward("$forward_name$");
}

Variables

form_var_name — put suggestVariableName() in the expression column

 

Add a Manager to a Class

This template will add a Manager and provide the proper setter for Spring injection.

Template Text:

private $man_name_cap$Manager $man_name$Manager = null;

public void set$man_name_cap$Manager($man_name_cap$Manager $man_name$Manager) {
        this.$man_name$Manager = $man_name$Manager;
}

Variables

man_name — put decapitalize(man_name_cap) in the expression column

 

Set up Mock expectations for a Manager

This template will create the skeleton of the expectations for a Mock Manager object. Useful when writing tests.

Template Text:

$mgr_name$.expects($expects$)
        .method("$method_name$")
        .with($with$)
        .will($will$);

Variables

expects — put once() in the default column (for me at least, this is the most common expectation)

 

 

 

Comments are closed.