Java Struts 2:返回调用页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struts 2: return to calling page
提问by Manrico Corazzi
I'm using Struts 2.
我正在使用Struts 2。
I'd like to return from an Action to the page which invoked it.
我想从 Action 返回到调用它的页面。
Say I'm in page x.jsp, I invoke Visual action to change CSS preferences in the session; I want to return to x.jsprather than to a fixed page (i.e. home.jsp)
假设我在页面x.jsp 中,我调用 Visual action 来更改会话中的 CSS 首选项;我想返回x.jsp而不是固定页面(即home.jsp)
Here's the relevant struts.xmlfragment:
这是相关的struts.xml片段:
<action name="Visual" class="it.___.web.actions.VisualizationAction"> <result name="home">/pages/home.jsp</result> </action>
Of course my VisualizationAction.execute()
returns home.
当然,我的VisualizationAction.execute()
回报回家。
Is there any "magic" constant (like, say, INPUT_PAGE) that I may return to do the trick?
是否有任何“魔法”常量(例如,INPUT_PAGE)我可以返回来做这个伎俩?
Must I use a more involved method (i.e. extracting the request page and forwarding to it)?
我是否必须使用更复杂的方法(即提取请求页面并转发给它)?
T.I.A.
TIA
采纳答案by Vincent Ramdhanie
You can use a dynamic result in struts.xml. For instance:
您可以在 struts.xml 中使用动态结果。例如:
<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.
然后在您的操作中,您创建一个名为 next 的字段。因此,要调用该操作,您将传递接下来要转发到的页面的名称。然后该动作返回“next”,struts 将知道要转到哪个页面。
There is a nicer explanation on this post: Stack Overflow
这篇文章有一个更好的解释:堆栈溢出
回答by nikhilbelsare
return INPUT;
will do the trick. INPUT constant is defined in Action interface itself. It indicates that action needs more input.
会做的伎俩。INPUT 常量是在 Action 接口本身中定义的。它表明动作需要更多的输入。
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.
如果您指的是将您带到操作输入页面的页面,则通过调用 page,那么您的输入页面将必须在操作的请求范围内存储 HTTP 标头“Referer”。
回答by Pavel Rodionov
I prefer the way when you navigating users by particular actions.
当您通过特定操作导航用户时,我更喜欢这种方式。
http://domain.com/myAction.action
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
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.
那么,你写了一些struts 2拦截器,它的逻辑是检查这样的参数'changeDesign'的存在,这个拦截器将做改变设计的必要工作并控制工作流。使用拦截器,您可以将您的操作与横切逻辑分离。
回答by pierdeux
My solution would involve one interface and one interceptor. You implement the following interface for all actions to whichyou 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:
从那时起,一旦这两个位就位并且您的操作实现了TargetAware
接口(如果您希望必须重定向到它们),那么您就可以target
在需要时访问JSP 中的参数。将该参数传递给您的VisualizationAction
(也可以实现TargetAware
接口!),然后SUCCESS
按照 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...).
我没有尝试这个策略的每一个细节。尤其要注意围绕redirect
结果类型的符号(取决于您的 Struts2 的特定版本:2.0.x 和 2.1.x 在这方面可能有所不同......)。
回答by Giancarlo
ok, in your class it.___.web.actions.VisualizationAction, you must return a string value containing INPUT, then, on struts.xml you have to set something like this:
好的,在你的类 it.___.web.actions.VisualizationAction 中,你必须返回一个包含 INPUT 的字符串值,然后,在 struts.xml 上你必须设置如下内容:
<action name="Visual" class="it.___.web.actions.VisualizationAction">
<result name="input">yourJspPage.jsp</result>
</action>
this will lead you to the page you want. This should work, I've been working on struts2 along 2 months
这将引导您进入您想要的页面。这应该有效,我已经在 struts2 上工作了 2 个月