(Struts2学习系列二)Struts2动态方法调用:指定method属性

  紧接着上一篇,在HelloWorldAction.java中添加add和update方法:

  

public class HelloWorldAction extends ActionSupport{

    public String add(){
        return SUCCESS;
    }
    
    public String update(){
        return SUCCESS;
    }
    
    @Override
    public String execute() throws Exception{
        // TODO Auto-generated method stub
        System.out.println("执行Action");
        return "success";
    }

}

  在src/struts.xml中添加action,指定method方法名:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="default"  extends="struts-default">
           <action name="helloworld" class="action.HelloWorldAction"> 
               <result name="success">/result.jsp</result>
           </action>
           
           <action name="addAction" class="action.HelloWorldAction" method="add">
               <result name="success">/add.jsp</result>
           </action>
           
           <action name="updateAction" class="action.HelloWorldAction" method="update">
               <result name="success">/update.jsp</result>
           </action>
   </package>
</struts>    

  然后在WebRoot路径下添加对应的add.jsp和update.jsp页面。

  启动tomcat服务器,在浏览器地址栏输入url:

  http://localhost:8080/struts2Demo/addAction.action

  http://localhost:8080/struts2Demo/updateAction.action

posted @ 2015-12-06 14:13  lindaZ  阅读(1898)  评论(0编辑  收藏  举报