Struts 2:返回调用页面
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢通过特定操作引导用户的方式。
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.
您可以在 struts.xml 中使用动态结果。 例如:
然后在您的操作中,您创建一个名为 next 的字段。 因此,要调用该操作,您将传递要转发到的下一个页面的名称。 然后该操作返回“next”,struts 将知道要转到哪个页面。
这篇文章有更好的解释:Stack Overflow
You can use a dynamic result in struts.xml. For instance:
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
会成功的。 INPUT 常量在 Action 接口本身中定义。 它表明行动需要更多的输入。
通过调用页面,如果您指的是将您带到操作输入页面的页面,那么您的输入页面将必须在操作的请求范围中存储 HTTP 标头“Referer”。
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.
我的解决方案将涉及一个接口和一个拦截器。 您可以为您可能想要重定向到的所有操作实现以下接口:
拦截器只需确保目标已设置(如果需要):
从那时起,一旦这两个位就位并且您的操作实现了
TargetAware
接口(如果您希望必须重定向到它们),那么您可以在需要时访问 JSP 中的target
参数。 将该参数传递给您的 VisualizationAction(也可以实现 TargetAware 接口!),并在成功时按照 Vincent 的说明进行重定向Ramdhani:我并没有尝试这个策略的每一个细节。 特别要注意
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:
The interceptor simply ensures that the target is set, if required:
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 atarget
parameter in your JSPs whenever you need it. Pass that parameter on to yourVisualizationAction
(which might as well implement also theTargetAware
interface!), and onSUCCESS
, redirect as explained by Vincent Ramdhanie: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...).