java 使用具有多种方法的 struts.xml 映射操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6401838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
map action with struts.xml with multiple methods
提问by coder25
I have seen many tutorials in which action has many methods other than execute
我看过很多教程,其中动作除了执行之外还有很多方法
public class UserAction {
public TestAction() {
}
public String execute() throws Exception {
System.out.println("hello i aam action");
return "success";
}
public String addUser(String name)
{
return "success";
}
public String listUser(String name)
{
return "success";
}
}
the action mapping is done like this
动作映射是这样完成的
<action name="addUser" method="addUser" class="com.vaannila.web.UserAction">
<result name="success" type="redirect">listUser</result>
</action>
<action name="listUser" method="listUser" class="com.vaannila.web.UserAction">
<result name="success">/register.jsp</result>
</action>
I a unable to understand it.How listUser is getting called?
我无法理解它。如何调用 listUser?
采纳答案by doctrey
I don't think this works because your mapping is pointing to methods (add, list) that do not exist. You have addUser
and listUser
in your action so your mapping have to point to the same methods.
Back to your question I have to say Struts2 has a feature that let's you pack a couple of methods into the same action. This is specially useful when you want to perform CRUD operations. One way is to explicitly predetermine the method you want to call in you mapping:
我认为这行不通,因为您的映射指向不存在的方法(添加、列表)。你有addUser
和listUser
在你的行动中,所以你的映射必须指向相同的方法。
回到你的问题,我不得不说 Struts2 有一个特性,可以让你将几个方法打包到同一个动作中。这在您想要执行 CRUD 操作时特别有用。一种方法是显式预先确定要在映射中调用的方法:
<action name="listUser" method="list" class="com.vaannila.web.UserAction">
<result name="success">/register.jsp</result>
</action>
If you point your browser to http://yourDomainHere/listUser
your list
method in your UserAction
action will be executed. You can also use wildcard mapping here like:
如果您将浏览器指向http://yourDomainHere/listUser
您的list
方法,您的UserAction
操作将被执行。您还可以在此处使用通配符映射,例如:
<action name="*User" method="{1}" class="com.vaannila.web.UserAction">
<result name="success">/register.jsp</result>
</action>
In this case you can replace asterisk (*) with any method in your action. This mapping will take first placeholder ({1}) in the mapping as a method and call it. So you can point your browser to: http://yourDomainHere/listUser
and http://yourDomainHere/addUser
knowing the same mapping will handle both and your respective methods will be called.
在这种情况下,您可以使用操作中的任何方法替换星号 (*)。此映射将映射中的第一个占位符 ({1}) 作为方法并调用它。因此,您可以将浏览器指向:http://yourDomainHere/listUser
并且http://yourDomainHere/addUser
知道相同的映射将处理这两种情况,并且将调用您各自的方法。
Edit for the comment
I think I got your point. In the example you provided listCustomer
is never called. It's the addCustomer
that populates the customerList
before sending success
and since it has provided a getter method for the list, back in the page you can access it and iterate it and show the values.
编辑评论
我想我明白你的意思。在您提供的示例listCustomer
中,从未调用过。它是在发送之前addCustomer
填充的customerList
,success
并且由于它为列表提供了一个 getter 方法,因此返回页面您可以访问它并迭代它并显示值。