Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
668 views
in Technique[技术] by (71.8m points)

jsp - Action name/URL name in debuggers using Struts 2

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.

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.

This is 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>

The JSP files look like this: 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>

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...