详解Struts2动态方法调用-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

详解Struts2动态方法调用

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

目前创新互联已为上千余家的企业提供了网站建设、域名、雅安服务器托管网站运营、企业网站设计、彭州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

1、指定method属性


/jsp/add.jsp

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


   /jsp/test.jsp
  /jsp/add.jsp
  /jsp/update.jsp

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

(加在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配置:


/jsp/test.jsp
/jsp/add.jsp
/jsp/update.jsp

访问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配置为:


/jsp/test.jsp
/jsp/{2}.jsp
/jsp/{2}.jsp

 

第一个*对应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开发流程及详细配置   Struts2修改上传文件大小限制方法解析等,有什么问题可以随时留言,小编会尽快回复大家。


分享名称:详解Struts2动态方法调用
本文URL:http://kswsj.cn/article/jgiiec.html

其他资讯