Struts2 - Can an action name be repeated in the struts.xml using the same action class but for different methods?

守給你的承諾、 提交于 2019-12-11 22:18:19

问题


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.?



来源:https://stackoverflow.com/questions/24791793/struts2-can-an-action-name-be-repeated-in-the-struts-xml-using-the-same-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!