如何在 Struts 2 的操作类中使用 Result 注释重定向到不同的命名空间

How to redirect to a different namespace using Result annotation in an action class in Struts 2

提问人:Steven Banks 提问时间:10/19/2023 最后编辑:Roman CSteven Banks 更新时间:10/23/2023 访问量:38

问:

我有一个网站,里面有公共和保护区。它是使用 Struts 2 (2.5.30) 和 Convention 插件 (2.5.30) 设置的。

在我的一个操作中,我试图使用结果注释从公共 () 重定向到保护区 () 中的另一个操作。/pub/enrollmentAction/prot/CompleteEnrollmentAction

执行重定向时,它会将 添加到生成的 URL 之前。即.它应该是./pub/pub/prot/complete-enrollment/prot/complete-enrollment

EnrollmentAction

@Action(results = { @Result(name = "success", location = "/prot/complete-enrollment", type = "redirectAction") }, interceptorRefs = { @InterceptorRef("BasicStack") })
public class Enrollment extends ActionSupport {
  
  @Override
  public String execute() throws Exception {
    
    return "success";
  }

我尝试在每个类中加入注释,但没有运气。 我尝试将结果放入 ,但它没有正确解析。@namespacestruts.xml

如果我输入重定向的 URL,我可以手动点击它。

java 注解 struts2-convention-plugin

评论


答:

0赞 Roman C 10/23/2023 #1

由于您使用的是约定插件,因此您需要知道如何通过约定创建配置。该插件也有自己的配置,如果更改了它,那么它可能会影响您的代码。

您错误地使用了结果配置。如果要使用属性,则应具有重定向结果类型。redirectAction 结果类型中没有此类属性。location

@Result(name = "success", location = "/prot/complete-enrollment", type = "redirect")

如果要使用结果类型,则redirectAction

@Result(name = "success", namespace = "/prot", actionName = "complete-enrollment", type = "redirectAction")