xml中重复动作名称吗

xml中重复动作名称吗

本文介绍了Struts2-可以使用相同的动作类但针对不同的方法在struts.xml中重复动作名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是使用Struts2以以下方式使用单个Action类和其中的多个方法来执行操作:

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

查看角色操作:manage/roles.action?method%3Aview=View添加角色操作:manage/roles.action?method%3Aadd=Add

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

通过调用提交按钮来调用URL,如下所示,来自test.jsp:

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>

在struts.xml中,我配置了:

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>

不幸的是,当我按下View按钮时,它显示的是"add"方法的结果JSP.为什么?

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

推荐答案

因为您有两个具有相同名称roles的动作.

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

您如何区分... /roles/roles?

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

它们应具有不同的名称-不同的URL.由于它们是相同的URL,因此最后一个配置将获胜.您正在覆盖自己的定义.为它们赋予不同的名称(如/addrole/viewrole)还是命名它们的间距(如/roles/add/roles/view等)怎么样?

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

这篇关于Struts2-可以使用相同的动作类但针对不同的方法在struts.xml中重复动作名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:16