Struts 2におけるDMI(動的メソッド呼び出し)

4815 ワード

 1  <package name="front" namespace="/front" extends="struts-default">

 2         <default-action-ref name="index" />

 3         <action name="helloword" class="struts.IndexAction">

 4             <result name="add">

 5                /hello.jsp

 6             </result>

 7             <result name="love">

 8                 /love.jsp

 9             </result>

10         </action>

11 </package>

上のプログラムを見て、actionのclass=「struts.Index Action」を指定しました.
IndexActionクラスを見てみましょう
 1 package struts;

 2 import com.opensymphony.xwork2.ActionSupport;

 3 

 4 

 5 public class IndexAction extends ActionSupport{

 6     public String add(){

 7      return "add";  //   result  

 8     }

 9     public String love(){

10      

11      return "love";

12     }

13 }

中にはexcute()メソッドはありませんが、この場合は皆さんがこのように配置します.
  
IndexActionクラスのaddメソッドの値を返すことができます.
しかし、この方法は推奨されません!推奨される方法は、動的呼び出し、すなわちDMIです.
例えばアドレスバーにURLを入力:http://localhost:8080/struts2/front/helloword!add(!後のaddは呼び出しのメソッド名で、デフォルトはexecute()を呼び出します.
ただし、このように入力すると、(There is no Action mapped for namespace[/front]and action name[helloword!add()]associated with context path[/Struts 2_10003])とエラーが表示されます.
したがって、プロファイルで開く必要があります. アドレスバーに動的に入力すると、予想されるページが得られます