Struts 2:如何正确地前进到其他动作?

发布于 2024-12-11 04:26:49 字数 1071 浏览 0 评论 0原文

我正在开发一个 Struts 2 项目,我需要在其中实现 2 个友好的 URL。由于我的 2 个网址需要类似于 URL/name/idURL/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 技术交流群。

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

发布评论

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

评论(2

若水般的淡然安静女子 2024-12-18 04:26:49

至于为什么你的 CSS 不起作用,我们无从得知——JSP 就是 JSP,遵循正常的 JSP/HTML 规则。

至于第一个问题:我建议反对动作链;它会被积极劝阻,而且通常只会把事情弄得一团糟。

如果不了解更多关于应用程序架构的信息,就很难给出有针对性的建议,但您可能会发现 命名空间中的参数很有帮助。

即使它不能立即适用,您也可以使用更适合您实际需求的 PatternMatcherActionMapper 来玩游戏 - 实现将取决于各种因素,特别是名称、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 or ActionMapper 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.

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