# 3.3 Action parameter injection

Action parameter injection refers to passing parameters to the action method, which can eliminate the need for the getPara(...) code and directly obtain parameter values. Below is a code example:

public class ProjectController extends Controller {
    public void index(Project project) {
       project.save();
       render("index.html");
    }
}
1
2
3
4
5
6

Action parameter injection can replace the getPara, getBean, and getModel series of methods to obtain parameters. When using the File and UploadFile parameters, it can replace the getFile method to implement file upload. Another advantage of this method of passing parameters is that it is convenient for seamless integration with third parties like Swagger, allowing for API documentation generation.

Note that if you don't want a Controller's public method with parameters to become an action, you can use the @NotAction annotation to indicate this.

Important usage: If the action parameter is a model or bean, and was previously obtained using getBean(User.class, "") with the second parameter being an empty string or null, then the equivalent parameter injection only requires the @Para("") annotation:

public void action(@Para("")User user) {. }
1

To use the Action parameter injection function, you need to configure your development tool to retain method parameter names during compilation. Note that older versions of Eclipse do not support Java 8 and this configuration option. It's recommended to use at least the Eclipse Mars version. [The document then shows various screenshots for configuring Eclipse and IntelliJ IDEA.]

In IntelliJ IDEA, you simply need to add a compilation parameter -parameters.

If you are using the Maven plugin for compilation, add a <compilerArgument>-parameters</compilerArgument> property to the maven-compiler-plugin.

The red arrows in the screenshots above point to the critical configuration. Below is the XML configuration content, which is identical to the content in the screenshot, provided for easy copying:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.6.1</version>
   <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <encoding>UTF-8</encoding>
      <!-- Java 8 retain parameter name compilation argument -->
      <compilerArgument>-parameters</compilerArgument>
   </configuration>
</plugin>
1
2
3
4
5
6
7
8
9
10
11
12

Finally, note that starting from JFinal version 3.5, action parameter injection functionality is directly supported. If you're using JFinal versions 3.2, 3.3, or 3.4 and wish to support this feature, you need to use the JFinal jfinal-java8 branch release. Its Maven coordinates are provided above. If your JFinal version is 3.5 or higher, you can ignore this.

Last Updated: 9/17/2023, 5:50:29 AM