当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Struts2动态方法调用

详解Struts2动态方法调用

2019年07月19日  | 移动技术网IT编程  | 我要评论

 动态方法就是一个action对应多个请求,减少action的数量

1、指定method属性

<action name="addaction" method="add" class="com.venn.action.helloworldaction">
<result>/jsp/add.jsp</result>
</action>

2、感叹号(!)方式(不推荐使用)

<action name="helloworld" class="com.venn.action.helloworldaction">
   <result>/jsp/test.jsp</result>
  <result name="add">/jsp/add.jsp</result>
  <result name="update">/jsp/update.jsp</result>
</action>

需要在struts.xml中加入如下常量:

<constant name="struts.enable.dynamicmethodinvocation" value="true"></constant>(加在package标签外面)

调用不同方法使用:

访问execute方法: http://localhost:8080/teststruts2/helloworld.action

访问update方法: http://localhost:8080/teststruts2/helloworld!update.action

访问add方法 http://localhost:8080/teststruts2/helloworld!add.action

3、通配符方式

action配置:

<action name="helloworld_*" method="{1}" class="com.venn.action.helloworldaction">
<result>/jsp/test.jsp</result>
<result name="add">/jsp/add.jsp</result>
<result name="update">/jsp/update.jsp</result>
</action>

访问execute方法: http://localhost:8080/teststruts2/helloworld.action 或http://localhost:8080/teststruts2/helloworld_execute.action

访问add方法 http://localhost:8080/teststruts2/helloworld_add.action

注:为简化struts.xml配置,可以将action配置为:

<action name="*_*_*" method="{2}" class="com.venn.{3}.{1}action">
<result>/jsp/test.jsp</result>
<result name="add">/jsp/{2}.jsp</result>
<result name="update">/jsp/{2}.jsp</result>
</action>

 

第一个*对应action,第二个*对应method

注意result标签的name属性不可以使用通配符

java类

public class helloworldaction extends actionsupport {
@override
public string execute() throws exception {
system.out.println("execute method");
return "success";
}
public string add(){
system.err.println("add method");
return "add";
}
public string update(){
system.out.println("update method");
return "update";
}
}

总结

以上就是本文关于详解struts2动态方法调用的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:   struts2修改上传文件大小限制方法解析等,有什么问题可以随时留言,小编会尽快回复大家。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网