Action 代码:
@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");
}
}
在struts.xml
配置文件中:
<constant name="struts.strictMethodInvocation.methodRegex" value="([a-zA-Z]*)"/>
我想去看看
http://localhost:8080/sshe/userAction!Test.action
现在的错误:
HTTP Status 404 - There is no Action mapped for namespace and action name [/] [userAction test] associated with context path [/sshe].
不知道有没有什么地方可以设置的。我怎样才能访问这个地址?
最佳答案
您应该将注释直接放在方法上。因为如果你把它放在类上,默认方法 execute()
用于映射。
@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;
}
}
action方法应该返回一个结果,如果你不想执行一个结果你应该返回Action.NONE
。
如果要用SMI那么您应该将 execute()
方法添加到操作类中。上面解释了为什么你需要这个方法来映射 Action 并且返回结果保持不变,因为方法执行仍然是 Action 方法。您不能使用 Action 映射来任意执行 Action 类中的任何方法。
@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;
}
}
action 方法是区分大小写的,所以你必须使用 URL
http://localhost:8080/sshe/userAction!test.action
关于java - Struts 2.5中如何使用注解@AllowedMethods实现动态方法调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44668770/