java - Struts 2.5中如何使用注解@AllowedMethods实现动态方法调用?

标签 java struts2 dmi

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/

相关文章:

java - 从 xwork-core-2.2.1.jar 设置 Struts-Spring 集成时出现 fatal error

java - 在 Struts2 中使用 AJAX 根据另一个选择菜单填充一个选择菜单

java - Struts 2 S2-016 漏洞缓解直至升级

java - 使用 Java 在 Windows 上隐藏文件/文件夹

java - 在 Java 中如何从一个字符数组中减去另一个字符数组?

java - 在没有spring boot的情况下使用spring mvc项目配置angular 2

java - 如何从 Java 代码在 JasperReport 中传递整数参数值?

java - 隐藏查询字符串以在 URL : URL Rewriting 中显示

Python 2 子进程(dmidecode)到一个变量?