-->

Struts2 - Can an action name be repeated in the st

2019-08-21 12:37发布

问题:

My idea is to perform actions using Struts2 the below way using a single Action class and multiple methods in it:

View roles action: manage/roles.action?method%3Aview=View Add role action: manage/roles.action?method%3Aadd=Add

The URLs are called through invoking submit buttons as shown below from test.jsp:

<s:form namespace="/manage/" action="roles" method="get" >
    <s:submit method="view" value="View" />
    <s:submit method="add" value="Add" />
    <s:submit method="edit" value="Edit" />
    <s:submit method="delete" value="Delete" />
</s:form>

In struts.xml, I configured:

 <package name="portal" namespace="/manage/" extends="struts-default">
    <action name="home">
        <result>/WEB-INF/jsp/manage/roles/test.jsp</result>
    </action>

    <action name="roles" class="struts2.actions.RoleAction" method="view">
        <result name="success">/WEB-INF/jsp/manage/roles/viewRoles.jsp</result>
    </action>

    <action name="roles" class="struts2.actions.RoleAction" method="add">
        <result name="input">/WEB-INF/jsp/manage/roles/addRole.jsp</result>
        <result name="success">/WEB-INF/jsp/manage/roles/viewRoles.jsp</result>
    </action>

Unfortunately, when I'm pressing View button, it's showing the result JSP of "add" method. Why?

回答1:

Because you have two actions with the same name, roles.

How would you differentiate between... /roles and /roles?

They should have different names–different URLs. Since they're the same URL the last one configured will win; you're overwriting your own definitions. How about giving them different names, like /addrole and /viewrole, or namespacing them, like /roles/add and /roles/view, etc.?