首页 > 代码库 > Action中动态方法的调用 Action中通配符的使用 Result的配置

Action中动态方法的调用 Action中通配符的使用 Result的配置

                                                                  Action中动态方法的调用

动态方法调用(Dynamic Method Invocation,DMI

标识符:!

一、通过以下选中的文件来查看是否禁止调用动态方法

技术分享

二、在我们自定义的action类中,我们不再单一的继承来自父类的方法,我们可以自定义自己的方法

 1 package cn.jbit.booklist; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class BookList extends ActionSupport { 6     @Override 7     public String execute() throws Exception { 8         System.out.println("booklist==========execute"); 9         return SUCCESS;10     }11 12     public String edit() throws Exception {13         System.out.println("booklist==========edit");14         return "edit";15     }16 17     public String list() throws Exception {18         System.out.println("booklist==========list");19         return "list";20     }21 22 }

三、配置我们抽离出来的booklist.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6  7   <package name="booklist" namespace="/" extends="main"> 8    9      <action name="booklist" class="cn.jbit.booklist.BookList">10         <result name="success">/WEB-INF/myinter/list.jsp</result>11         <result name="login">/WEB-INF/myinter/login.jsp</result>12         13      </action>14      15       <action name="booklist" class="cn.jbit.booklist.BookList">16         <result name="edit">/WEB-INF/myinter/edit.jsp</result>17      </action>18      19     20      <action name="booklist" class="cn.jbit.booklist.BookList">21         <result name="list">/WEB-INF/myinter/list.jsp</result>22      </action>23     24   </package>25 </struts>

四、将抽离出来的booklist.xml被struts.xml引用

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <!--开发模式,以后对Struts。xml做的任何修正,都不需要重新启动服务器  --> 7 <constant name="struts.devMode" value="true"></constant> 8 <!-- <constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"></constant> --> 9 <package name="main" namespace="" extends="struts-default">10 11     <interceptors>12         <interceptor name="myinter" class="cn.jbit.intercepter.MyTimerIntercepter"/>13         14         <interceptor-stack name="myStack">15         <interceptor-ref name="myinter"></interceptor-ref>16         <interceptor-ref name="defaultStack"></interceptor-ref>17         </interceptor-stack>18      </interceptors>19      <default-interceptor-ref name="myStack"/>20      21   <default-action-ref name="index2"></default-action-ref>22   <action name="index2">23   <result>/WEB-INF/myinter/login.jsp</result>24   </action>25 26 </package>27 28 <include file="cn/jbit/strusdemo/login.xml"></include>29 <include file="cn/jbit/booklist/booklist.xml"></include>30 </struts>

 

 

 

 

 

 

Action中通配符的使用

技术分享

 1 package cn.jbit.action; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class BookAction extends ActionSupport{ 6      7     /** 8      * 添加 9      * */10      public String add() throws Exception{11          System.out.println("add");12          return "add";13      }14     15      /**16       * 修改17       * */18      public String update() throws Exception{19          System.out.println("update");20          return "update";21      }22      23      /**24       * 查询25       * */26      public String list() throws Exception{27          System.out.println("list");28          return "list";29      }30     31 }

 

 1 package cn.jbit.action; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class UserAction extends ActionSupport{ 6  7     /** 8      * 添加 9      * */10      public String add() throws Exception{11          System.out.println("add");12          return "add";13      }14     15      /**16       * 修改17       * */18      public String update() throws Exception{19          System.out.println("update");20          return "update";21      }22      23      /**24       * 查询25       * */26      public String list() throws Exception{27          System.out.println("list");28          return "list";29      }30     31 }

 

技术分享

在地址栏输入

技术分享

 

Result的配置

技术分享

默认情况下是转发

下面我们来一道测试

技术分享

 

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <!--开发模式,以后对Struts。xml做的任何修正,都不需要重新启动服务器  --> 7 <constant name="struts.devMode" value="true"></constant> 8 <!-- <constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"></constant> --> 9 <package name="default" namespace="/" extends="struts-default">10     11      <action name="userAction" class="cn.jbit.action.UserAction" method="list">12         <result name="list">/User/list.jsp</result>13         <result name="success">/User/update.jsp</result>14      </action>15     16     <action name="bookAction" class="cn.jbit.action.BookAction">17        <result name="add">/Book/add.jsp</result>18     </action>19 20 </package>21 22 </struts>

 

 

 1 package cn.jbit.action; 2  3 import javax.servlet.http.HttpServletRequest; 4  5 import org.apache.struts2.ServletActionContext; 6  7 import com.opensymphony.xwork2.ActionSupport; 8  9 public class UserAction extends ActionSupport{10 11     /**12      * 添加13      * */14      public String add() throws Exception{15          System.out.println("add");16          return "add";17      }18     19      /**20       * 修改21       * */22      public String update() throws Exception{23          System.out.println("update");24          return "update";25      }26      27      /**28       * 查询29       * */30      public String list() throws Exception{31          System.out.println("list");32          HttpServletRequest request = ServletActionContext.getRequest();33          request.setAttribute("uname", "我是转发,我可以携带数据");34          35          return "list";36      }37     38 }

 

在地址栏中输入请求地址

技术分享

重定向到视图页面我们就不做展示,下面我们直接介绍重定向到一个action地址

技术分享

在地址栏请求

技术分享

 

再来说转发到一个action

技术分享

 

Action中动态方法的调用 Action中通配符的使用 Result的配置