如何使用Struts2.5注释@AllowedMethods('test')来实现动态调用方法?

时间:2017-06-21 06:53:22

标签: java struts2

@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状态404 - 没有针对与上下文路径[/ sshe]关联的命名空间和操作名称[/] [userAction测试]映射的操作。!

我想知道是否有任何地方可以设置。我如何访问此地址?

1 个答案:

答案 0 :(得分:1)

您应该将注释直接放在方法上。因为如果将它放在类上,则默认方法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.NONE

如果您要使用SMI,则应将execute()方法添加到操作类。以上解释了为什么您需要此方法来映射操作,并且返回结果保持不变,因为方法执行仍然是操作方法。您不能使用动作映射来任意执行动作类中的任何方法。

@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;
    }
}

操作方法区分大小写,因此您必须使用URL

http://localhost:8080/sshe/userAction!test.action
相关问题