歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2的動態方法調用DMI

Struts2的動態方法調用DMI

日期:2017/3/1 10:15:01   编辑:Linux編程

在寫Struts的Action類的時候,經常遇到不希望每次調用的都是execute方法,希望能動態的調用一些其他的方法,這裡Struts提供了兩種方式,第一種是在strust.xml中進行method這個屬性的配置,但是這樣每次只能配置一個,而且是死值,不方便動態的更換和調用,所以這裡介紹DMI,動態的方法的調用。

下面我們先寫出來Action

  1. package com.bird.test;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class IndexAction extends ActionSupport{
  4. private static final long serialVersionUID = 1L;
  5. @Override
  6. public String execute() throws Exception {
  7. return SUCCESS;
  8. }
  9. public String test(){
  10. return ERROR;
  11. }
  12. }

這個action裡面既有execute方法,也有test方法,我們的目的就是去動態的調用它的test方法

下面看一下它的對應的struts.,xm文件的配置

  1. <constant name="struts.devMode" value="true"/>
  2. <package name="front" namespace="/front" extends="struts-default">
  3. <action name="index" class="com.bird.test.IndexAction">
  4. <result name="success">/Hello.jsp</result>
  5. <result name="error">/test.jsp</result>
  6. </action>
  7. </package>

沒有什麼特殊的配置,最重要的就是訪問的時候的url地址的寫法

這裡寫為

http://localhost:8080/Struts2/front/index!test

分別的意思是,調用命名空間/front下面的indexaction裡面的test方法,特別注意的就是感歎號!!!index!test

這就是DMI動態方法調用,一個簡單的非常實用 的功能。

Copyright © Linux教程網 All Rights Reserved