使用 Struts 2 的调试器中的操作名称/URL 名称 [英] Action name/URL name in debuggers using Struts 2

查看:15
本文介绍了使用 Struts 2 的调试器中的操作名称/URL 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我去年开始使用这个 web 应用程序以来,这一直困扰着我(我没有 web 开发经验)我不知道这个应用程序的结构是否不正确,或者这只是 它的方式 .

This has been bothering me since I started working with this webapp last year (I had no experience with web development) I don't know if this app is structured incorrectly or if this is just the way it is.

我的 welcome 页面实际上是一个登录页面,用户登录我的应用程序后会转到一个主页,在那里他们有一个菜单可以转到各种其他功能.

My welcome page is really a login page, and after the user logs in my app goes to a main page, where they have a menu to go to various other functions.

这是 struts.xml:

<action name="Welcome">
    <interceptor-ref name="newStack" />
    <result name="success">Login.jsp</result>
</action>

<action name="actLogin" class="actionLogin">
    <interceptor-ref name="newStack" />
    <result name="success">HomePage.jsp</result>
    <result name="error">Login.jsp</result>
</action>

<action name="actHomePage" class="actionHomePage">
    <interceptor-ref name="newStack" />
    <result name="viewUsers">  ViewUsers.jsp</result>
    <result name="addUser">    AddUser.jsp</result>
    <result name="viewWidgets">ViewWidgets.jsp</result>
    <result name="addWidget">  AddWidget.jsp</result>
</action>

JSP 文件如下所示:Login.jsp:

<s:form action="actLogin" name="formLogin" method="post">
    <s:textfield name="userId" id="txtUserId"/>
    <s:password name="password" id="txtPassword"/>
    .. login submit button ...
</s:form>

HomePage.jsp:

<s:form action="actHomePage" name="formHomePage" method="post">
. . . menu items that set values for action to pick up to route to the correct page
</s:form>

ViewUsers.jsp:

<s:form action="actViewUsers" name="formViewUsers" method="post">
    <h2>User List</h2>
    . . .
</s:form>

问题在于用户看到的 URL 以及在调试器(Chrome、IE 或 Firefox)中列出源文件的 URL 总是错误的.用户和调试器在登录页面上看到的 URL 是 Welcome(没问题),但在他们登录后,他们在 HomePage 上看到 actLogin.html 页面,他们会在 ViewUsers 页面上看到 HomePage.html.该 URL 始终位于您实际所在页面的后面,因为它使用操作名称.

The problem with this is the URL the user sees and the URLs listing the source files in a debugger (Chrome, IE or Firefox) is always wrong. The URL that the user and debugger sees is Welcome on the login page (which is OK), but after they log in they see actLogin.html on the HomePage page, and they see HomePage.html on the ViewUsers page. The URL is always one behind the page you are actually on because it uses the action name.

我的登录 JSP是否应该将其表单操作命名为 LOGIN,因为虽然这是正在执行的操作,但该操作的结果会转到 HomePage?

Should my login JSP not have its form action be named LOGIN because while that is the action being performed, the result of that action goes to HomePage?

这个应用的结构是否奇怪?有更好的方法吗?

Is this app structured weird and there's a better way to do this?

推荐答案

这是由于工作流程设计不当造成的.要更正它,您应该阅读 PRG 模式.你可以改变

This happens as a result of the bad workflow design. To correct it you should read about PRG pattern. You can change

<action name="actLogin" class="actionLogin">
    <interceptor-ref name="newStack" />
    <result name="success" type="redirectAction">actHomePage</result>
    <result name="error">Login.jsp</result>
</action>

<action name="actHomePage" class="actionHomePage">
    <interceptor-ref name="newStack" />
    <result name="success">HomePage.jsp</result>
</action>

不需要其他结果,但如果您需要在 URL 中使用相同的操作转发到不同的页面,则可以保留它们.

other results are not needed, but you can remain them if you need to forward to a different page with the same action in the URL.

这篇关于使用 Struts 2 的调试器中的操作名称/URL 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆