When writing code with IDEA, it is often frustrating to repeatedly write similar or identical code.
IDEA comes with a Live Template feature, which not only has many built-in shortcuts for writing code, but also allows you to customize your own Live Templates.
Custom Commands#
You can customize some commands. Open IDEA settings, find Live Templates, add on the right side, you can first add your own group, such as I added a group called My Live
, and then add specific configurations.
Live Template in settings
Variables are wrapped in $$
, so IDEA knows that it is a variable. Variables can be assigned a value in the bottom right corner of Edit variables
.
In the Expression
column, you can set a calculation method for the variable, such as className()
, which indicates that the CLASS
variable is the current class name. You can also specify a default value in Default value
. If the last column is checked, if the variable has a calculated value, the user will not be prompted to enter a value when using it.
Here are some examples I use myself.
Logger Definition#
Template text
private static final Logger logger = LoggerFactory.getLogger($CLASS$.class);
Variables
Name | Expression | Default value | Skip if defined |
---|---|---|---|
CLASS | className() | ☑️ |
Effect
Logger Definition
Return ModelAndView#
Template text
@RequestMapping("/$NAME$.html")
public ModelAndView $NAME$(HttpServletRequest request, HttpServletResponse response) {
logger.info("****************$CLASS$:$NAME$ Start****************");
logger.info("****************$CLASS$:$NAME$ End****************");
return new ModelAndView("$NAME$/$NAME$");
}
Variables
Name | Expression | Default value | Skip if defined |
---|---|---|---|
NAME | |||
CLASS | className() | ☑️ |
Effect
Return ModelAndView (with URL parameters)#
Template text
@RequestMapping("/$NAME$/{$PATHVARIABLE$}.html")
public ModelAndView $NAME$(@PathVariable("$PATHVARIABLE$") String $PATHVARIABLE$, HttpServletRequest request, HttpServletResponse response) {
logger.info("****************$CLASS$:$NAME$ Start****************");
logger.info("****************$CLASS$:$NAME$ End****************");
return new ModelAndView("$NAME$/$NAME$");
}
Variables
Name | Expression | Default value | Skip if defined |
---|---|---|---|
NAME | |||
PATHVARIABLE | |||
CLASS | className() | ☑️ |
Effect
Create Controller#
Template text
@RestController
public class $ControllerName$Controller {
private static final Logger logger = LoggerFactory.getLogger($ControllerName$Controller.class);
/**
* $viewName$
*/
@RequestMapping("/$viewName$.html")
public ModelAndView $viewName$(HttpServletRequest request, HttpServletResponse response) {
logger.info("****************$ControllerName$Controller:$viewName$ Start****************");
logger.info("****************$ControllerName$Controller:$viewName$ End****************");
return new ModelAndView("$viewName$/$viewName$");
}
}
Variables
Name | Expression | Default value | Skip if defined |
---|---|---|---|
ControllerName | |||
viewName |
Effect
Built-in Commands#
You can also use built-in commands to template statements, such as looping statements (for, foreach), wrapping a string with String.format(), wrapping an expression with type conversion, generating if statements based on null or other conditional statements, and generating branch judgment statements using instanceOf. The usage is also very simple, just press the dot . after an expression, then enter some prompts or select a candidate from the list. Common candidates will be accompanied by GIF demonstrations.
var Declaration#
null Check#
notnull Check#
nn Check#
for Loop#
fori Loop with Index#
not Negation#
if Conditional Statement#
cast Type Conversion#