Struts 2:返回调用页面

发布于 2024-07-03 23:32:59 字数 604 浏览 5 评论 0原文

我正在使用 Struts 2

我想从操作返回到调用它的页面。

假设我在 x.jsp 页面中,我调用视觉操作来更改会话中的 CSS 首选项; 我想返回到x.jsp而不是固定页面(即home.jsp

这是相关的struts.xml分段:

<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="home">/pages/home.jsp</result>
</action>

当然,我的 VisualizationAction.execute() 返回 home

是否有任何我可以返回的“神奇”常量(例如 INPUT_PAGE)来实现这一目的?

我必须使用更复杂的方法(即提取请求页面并转发到它)吗?

TIA

I'm using Struts 2.

I'd like to return from an Action to the page which invoked it.

Say I'm in page x.jsp, I invoke Visual action to change CSS preferences in the session; I want to return to x.jsp rather than to a fixed page (i.e. home.jsp)

Here's the relevant struts.xml fragment:

<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="home">/pages/home.jsp</result>
</action>

Of course my VisualizationAction.execute() returns home.

Is there any "magic" constant (like, say, INPUT_PAGE) that I may return to do the trick?

Must I use a more involved method (i.e. extracting the request page and forwarding to it)?

T.I.A.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Smile简单爱 2024-07-10 23:32:59

我更喜欢通过特定操作引导用户的方式。

http://domain.com/myAction.action

您可以使用一些您想要的参数作为指示器更改当前设计:

http://domain.com/myAction.action?changeDesign=silver_theme

那么,你编写一些struts 2拦截器,其逻辑是检查“changeDesign”这样的参数是否存在,并且该拦截器将完成更改设计的必要工作并控制工作流程。 使用拦截器,您可以将操作与横切逻辑分离。

I prefer the way when you navigating users by particular actions.

http://domain.com/myAction.action

You could use some parameter as indicator, that you want to change current design:
i.e.

http://domain.com/myAction.action?changeDesign=silver_theme

So then, you write some struts 2 interceptor, which logic is to check the presence of such parameter 'changeDesign', and this interceptor will do nessesary work of changing design and will control workflow. With interceptor you decouple your actions from crosscutting logic.

隔岸观火 2024-07-10 23:32:59

您可以在 struts.xml 中使用动态结果。 例如:

<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="next">${next}</result>
</action>

然后在您的操作中,您创建一个名为 next 的字段。 因此,要调用该操作,您将传递要转发到的下一个页面的名称。 然后该操作返回“next”,struts 将知道要转到哪个页面。

这篇文章有更好的解释:Stack Overflow

You can use a dynamic result in struts.xml. For instance:

<action
   name="Visual"
   class="it.___.web.actions.VisualizationAction">
   <result name="next">${next}</result>
</action>

Then in your action, you create a field called next. So to invoke the action you will pass the name of the page that you want to forward to next. The action then returns "next" and struts will know which page to go to.

There is a nicer explanation on this post: Stack Overflow

不交电费瞎发啥光 2024-07-10 23:32:59
return INPUT;

会成功的。 INPUT 常量在 Action 接口本身中定义。 它表明行动需要更多的输入。

通过调用页面,如果您指的是将您带到操作输入页面的页面,那么您的输入页面将必须在操作的请求范围中存储 HTTP 标头“Referer”。

return INPUT;

will do the trick. INPUT constant is defined in Action interface itself. It indicates that action needs more input.

By calling page if you meant the page that took you to the action input page, then your input page will have to store HTTP header "Referer" in the request scope for the Action.

疏忽 2024-07-10 23:32:59

我的解决方案将涉及一个接口和一个拦截器。 您可以为您可能想要重定向到的所有操作实现以下接口:

public interface TargetAware {
  public String getTarget();
  public void setTarget(String target);
}

拦截器只需确保目标已设置(如果需要):

public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {
   public String doIntercept(ActionInvocation invocation) {
      Object action = invocation.getAction();
      HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
      if (action instanceof TargetAware) {
         TargetAware targetAwareAction = (TargetAware) action;
         if (targetAwareAction.getTarget() == null)
            targetAwareAction.setTarget(getCurrentUri(request));
      }     
      return invocation.invoke();
   }

   // I'm sure we can find a better implementation of this...
   private static String getCurrentUri(HttpServletRequest request) {
      String uri = request.getRequestURI();
      String queryString = request.getQueryString();
      if (queryString != null && !queryString.equals(""))
         uri += "?" + queryString;
      return uri;
   }

   public void init() { /* do nothing */ }
   public void destroy() { /* do nothing */ }
}

从那时起,一旦这两个位就位并且您的操作实现了 TargetAware 接口(如果您希望必须重定向到它们),那么您可以在需要时访问 JSP 中的 target 参数。 将该参数传递给您的 VisualizationAction(也可以实现 TargetAware 接口!),并在成功时按照 Vincent 的说明进行重定向Ramdhani:

<action name="Visual" class="it.___.web.actions.VisualizationAction">
   <result type="redirect">
      <param name="location">${target}</param>
      <param name="parse">true</param>
   </result>
</action>

我并没有尝试这个策略的每一个细节。 特别要注意 redirect 结果类型周围的符号(取决于您的 Struts2 的特定版本:2.0.x 和 2.1.x 可能会有所不同...)。

My solution would involve one interface and one interceptor. You implement the following interface for all actions to which you are likely to want to redirect:

public interface TargetAware {
  public String getTarget();
  public void setTarget(String target);
}

The interceptor simply ensures that the target is set, if required:

public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {
   public String doIntercept(ActionInvocation invocation) {
      Object action = invocation.getAction();
      HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
      if (action instanceof TargetAware) {
         TargetAware targetAwareAction = (TargetAware) action;
         if (targetAwareAction.getTarget() == null)
            targetAwareAction.setTarget(getCurrentUri(request));
      }     
      return invocation.invoke();
   }

   // I'm sure we can find a better implementation of this...
   private static String getCurrentUri(HttpServletRequest request) {
      String uri = request.getRequestURI();
      String queryString = request.getQueryString();
      if (queryString != null && !queryString.equals(""))
         uri += "?" + queryString;
      return uri;
   }

   public void init() { /* do nothing */ }
   public void destroy() { /* do nothing */ }
}

From then on, once these two bits are in place and your actions implement the TargetAware interface (if you expect to have to redirect to them), then you have access to a target parameter in your JSPs whenever you need it. Pass that parameter on to your VisualizationAction (which might as well implement also the TargetAware interface!), and on SUCCESS, redirect as explained by Vincent Ramdhanie:

<action name="Visual" class="it.___.web.actions.VisualizationAction">
   <result type="redirect">
      <param name="location">${target}</param>
      <param name="parse">true</param>
   </result>
</action>

I did not try every single detail of this strategy. In particular, beware of the notation surrounding the redirect result type (depending on your specific version of Struts2: 2.0.x and 2.1.x may differ on this...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文