Struts 2:如何正确地前进到其他动作?
我正在开发一个 Struts 2 项目,我需要在其中实现 2 个友好的 URL。由于我的 2 个网址需要类似于 URL/name/id 和 URL/id/title,因此我发现管理此问题的唯一方法是使用第一步操作处理请求,然后转发到正确的 Action1 或 Action2,如下所示:
<action name="/*/*" class="web.ProcessRequestAction">
<param name="firstParam">{1}</param>
<param name="secondParam">{2}</param>
<result name="action1" type="chain">Action1</result>
<result name="action2" type="chain">Action2</result>
</action>
并定义两个操作,如下所示:
<action name="Action1" class="web.Action1" method="execute">
<result name="success">/WEB-INF/content/Action1.jsp</result>
<result name="input">/WEB-INF/content/Action1.jsp</result>
<result name="error">/WEB-INF/content/Action1.jsp</result>
</action>
但是,如果我调用 URL/参数/参数,然后在 ProcessRequestAction 上返回“Action1”,我'会得到Action1.jsp(按预期)但很混乱,似乎所有 CSS 都丢失了。 首先我的做法正确吗?如果是这样,我在这里做错了什么,我可以改进什么?
提前致谢!
I'm developing a Struts 2 project where I need to implement 2 friendly URLs. Since my 2 URLs need to be something like URL/name/id and URL/id/title, the only way I found to manager this was to use an 1st step action to process the request which then forwards to the correct Action1 or Action2, like this:
<action name="/*/*" class="web.ProcessRequestAction">
<param name="firstParam">{1}</param>
<param name="secondParam">{2}</param>
<result name="action1" type="chain">Action1</result>
<result name="action2" type="chain">Action2</result>
</action>
And have both actions defined as well like this:
<action name="Action1" class="web.Action1" method="execute">
<result name="success">/WEB-INF/content/Action1.jsp</result>
<result name="input">/WEB-INF/content/Action1.jsp</result>
<result name="error">/WEB-INF/content/Action1.jsp</result>
</action>
However, if I invoke URL/parameter/parameter, and then on ProcessRequestAction I return "Action1", I'll get the Action1.jsp (as intended) but all messy, seems like all the CSS is missing.
First of all, is my approach correct? If so, what I'm I doing wrong here, what can I improve?
Thansk in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于为什么你的 CSS 不起作用,我们无从得知——JSP 就是 JSP,遵循正常的 JSP/HTML 规则。
至于第一个问题:我建议反对动作链;它会被积极劝阻,而且通常只会把事情弄得一团糟。
如果不了解更多关于应用程序架构的信息,就很难给出有针对性的建议,但您可能会发现 命名空间中的参数很有帮助。
即使它不能立即适用,您也可以使用更适合您实际需求的
PatternMatcher
或ActionMapper
来玩游戏 - 实现将取决于各种因素,特别是名称、ID 和标题 URL 组件的格式。As to why your CSS isn't working, we have no way of knowing--a JSP is a JSP, and follows normal JSP/HTML rules.
As for the first issue: I'd recommend against action chaining; it's actively discouraged, and usually just makes a mess of things.
Without knowing more about your application architecture, it's difficult to give targeted advice, but you may find parameters in namespaces helpful.
Even if it's not immediately applicable, you may be able to play games with a
PatternMatcher
orActionMapper
that's more targeted to your actual needs--the implementation would depend on a variety of factors, particularly the format(s) of the name, id, and title URL components.以下是一些资源,可以帮助您获得所需的干净 URL:
Here are a few resources that may help you achieve the clean URLs you're looking for: