问题
@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "userAction")
@AllowedMethods("test")
public class UserAction {
private static final String[] test = null;
private static Logger logger = Logger.getLogger(UserAction.class);
public void test() {
logger.info("进入action");
}
}
In the struts.xml
configuration file:
<constant name="struts.strictMethodInvocation.methodRegex" value="([a-zA-Z]*)"/>
I want to visit http://localhost:8080/sshe/userAction! Test.action
!
Now the error: HTTP Status 404 - There is no Action mapped for namespace and action name [/] [userAction test] associated with context path [/sshe].!
I wonder if there is any place to set up. How can I access this address?
回答1:
You should place the annotation directly on the method. Because if you put it on the class the default method execute()
is used for the mapping.
@ParentPackage("basePackage")
@Namespace("/")
@AllowedMethods("test")
public class UserAction {
private static final String[] test = null;
private static Logger logger = Logger.getLogger(UserAction.class);
@Action(value = "userAction")
public String test() {
logger.info("进入action");
rerurn Action.NONE;
}
}
The action method should return a result, if you don't want to execute a result you should return Action.NONE
.
If do you want to use SMI then you should add execute()
method to the action class. The above explains why do you need this method to map the action and the return result remains the same since the method execution still remains the action method. You cannot use action mapping to arbitrary execute any method in the action class.
@ParentPackage("basePackage")
@Namespace("/")
@AllowedMethods("test")
@Action(value = "userAction")
public class UserAction {
private static final String[] test = null;
private static Logger logger = Logger.getLogger(UserAction.class);
public String execute() {
rerurn Action.NONE;
}
public String test() {
logger.info("进入action");
rerurn Action.NONE;
}
}
The action method is case sensitive, so you have to use URL
http://localhost:8080/sshe/userAction!test.action
来源:https://stackoverflow.com/questions/44668770/how-do-i-use-the-struts2-5-annotation-allowedmethods-test-to-implement-a-dy