Contents
  1. 1. References
    1. 1.1. Documentations
    2. 1.2. Questions
  2. 2. Example Project Download
  3. 3. Multiple Action Methods
  4. 4. Dynamic Method Invocation
    1. 4.1. Enable DMI
    2. 4.2. Write the Code
  5. 5. Wildcard Method

Today, I learned how to use multiple actions in struts2 in class. Surely, the way the teacher taught was using XML. I tried to search for some documentation and tutorials, but they were very scattered. Here I try to write a tutorial about 2 ways to do multiple actions in one class.

References

Documentations

Questions

Example Project Download

This is my 5th lab assignment project. It uses git and has 2 branches. Each branch represents a different way to implement multiple actions.

Download Java-Ent-Lab5 project

Multiple Action Methods

The easiest way is using @Action annotation for functions but not for entire class, so that these methods will be mapped to corresponding URIs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Namespace("/calc")
@Result(name="show", location="/show.jsp")
public class CalcAction extends ActionSupport {
// ...
@Action("add")
public String add() throws Exception {
value = x + y;
msg = "You select addition!";
return "show";
}
@Action("sub")
public String sub() throws Exception {
value = x - y;
msg = "You select subtraction!";
return "show";
}
@Action("mul")
public String mul() throws Exception {
value = x * y;
msg = "You select multiplication!";
return "show";
}
@Action("div")
public String div() throws Exception {
value = x / y;
msg = "You select division!";
return "show";
}
}

Dynamic Method Invocation

Dynamic Method Invocation (DMI) is a feature of struts2 which can let you dynamically call methods in the Action class. It provides similar URI like GET parameters. ? is replaced by !. The example below will have pages /calc!add, /calc!sub and so on.

Enable DMI

This feature is disabled by default because of some security reason. If you really want to use it, you must do some configuration. Fortunately, I find a way so that I can still not to use struts.xml. We can add a configuration file named struts.properties, put it under src directory and add the following line:

1
struts.enable.DynamicMethodInvocation = true

More configurations can be found in default.properties in above links.

Write the Code

Add @AllowedMethods annotation for methods you want to be called. In struts 2.5, the DMI is in some strict mode, so it will be more secure. In older version, the @AllowedMethods annotation below is not required and you can call any method in the Action class. But in newer version, it must be added so only these methods can be dynamically called. You can configure it to make it not need to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Namespace("/")
@Result(name="show", location="/show.jsp")
@AllowedMethods({"add", "sub", "mul", "div"})
public class CalcAction extends ActionSupport {
// ...
public String add() throws Exception {
value = x + y;
msg = "You select addition!";
return "show";
}
public String sub() throws Exception {
value = x - y;
msg = "You select subtraction!";
return "show";
}
public String mul() throws Exception {
value = x * y;
msg = "You select multiplication!";
return "show";
}
public String div() throws Exception {
value = x / y;
msg = "You select division!";
return "show";
}

Wildcard Method

Forget about this. Through my hours search, I can bet there is no way to use wildcard method only with annotations. I give up. Struts convention plugin sucks. The documentation is so incomplete. I miss my Red language.

Contents
  1. 1. References
    1. 1.1. Documentations
    2. 1.2. Questions
  2. 2. Example Project Download
  3. 3. Multiple Action Methods
  4. 4. Dynamic Method Invocation
    1. 4.1. Enable DMI
    2. 4.2. Write the Code
  5. 5. Wildcard Method