struts2教程:10、动态方法调用和使用通配符定义action
2012-12-14 22:05
926 查看
动态方法调用:struts2有两种方式
如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。如下:
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "我的第一个struts2应用";
return "success";
}
public String other() throws Exception{
this.message = "第二个方法";
return "success";
}
}
假设访问上面action的URL路径为: /struts/test/helloworld.action
要访问action的other()
方法,我们可以这样调用:
/struts/test/helloworld!other.action(struts2.1后官方文档已不推荐使用)
如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
使用通配符定义action(推荐使用)
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld_*" class="cn.itcast.action.HelloWorldAction"
method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "我的第一个struts2应用";
return "success";
}
public String other() throws Exception{
this.message = "第二个方法";
return "success";
}
}
要访问other()方法,可以通过这样的URL访问:/test/helloworld_other.action
通配符还可以用于class result视图等都可以,只是不常用而已。
如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。如下:
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "我的第一个struts2应用";
return "success";
}
public String other() throws Exception{
this.message = "第二个方法";
return "success";
}
}
假设访问上面action的URL路径为: /struts/test/helloworld.action
要访问action的other()
方法,我们可以这样调用:
/struts/test/helloworld!other.action(struts2.1后官方文档已不推荐使用)
如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
使用通配符定义action(推荐使用)
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld_*" class="cn.itcast.action.HelloWorldAction"
method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "我的第一个struts2应用";
return "success";
}
public String other() throws Exception{
this.message = "第二个方法";
return "success";
}
}
要访问other()方法,可以通过这样的URL访问:/test/helloworld_other.action
通配符还可以用于class result视图等都可以,只是不常用而已。
相关文章推荐
- struts2-10动态方法调用和使用通配符定义action
- struts2动态方法调用和使用通配符定义action
- struts2动态方法调用和使用通配符定义action
- Struts2动态方法调用和使用通配符定义action
- struts2学习笔记--------动态方法调用和使用通配符定义action
- struts2中动态方法调用和使用通配符定义Action
- struts2中动态方法调用和使用通配符定义Action
- Struts2_动态方法调用和使用通配符定义action
- struct2动态方法调用和使用通配符定义action
- Struts2 Action中动态方法调用、通配符的使用
- action动态方法调用和使用通配符定义action
- Struts2学习笔记(七):动态方法调用和使用通配符定义action
- 第三章Struts2 Action中动态方法调用、通配符的使用
- 第11章 动态方法调用和使用通配符定义action
- struts2:动态方法调用与通配符定义action
- 动态方法调用和使用通配符定义action
- Struts2 Action中动态方法调用、通配符的使用
- struts2——(8)(①3种情况通配符的使用方法②动态方法调用 )
- 使用Struts2动态方法调用时(DMI)出现There is no Action mapped for namespace [/user] and action name [user!add] 问题
- struts2如何动态调用action的方法以及使用json插件范围json格式的javascript对象