首页 > 代码库 > Struts2---动态方法调用

Struts2---动态方法调用

技术分享
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
              <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><!-- 指定可以采用的方法 -->

<!-- 为了减少action的定义常在一个action中定义多个方法,以相应不同的请求,-->
<!--  依据不同情况向url中指定方法->寻找方法返回值对应的result结果视图 -->
<!-- 
 1.通过通配符调用,_代表分隔符,*代表匹配符(匹配result结果的name值)调用delete方法,如url = "localhost:8080/HelloStruts2/delete_struts.action"
         
         <action name="*_struts" class="com.dunn.action.HelloStruts2" method="{1}" >
             <result >/result.jsp</result>
             <result name="delete">/{1}.jsp</result>
             <result name="hello">/{1}.jsp</result>
         </action>
  -->        
         
     
<!-- 
 2.通过方法名调用,指定constant标签元素struts.enable.DynamicMethodInvocation的值为true即可,
  调用delete方法,如url = "localhost:8080/HelloStruts2/exestruts!delete.action"
         <action name="exestruts" class="com.dunn.action.HelloStruts2"  >
             <result >/result.jsp</result>
             <result name="delete">/delete.jsp</result>
             <result name="hello">/hello.jsp</result>
         </action>
  -->       
               
<!-- 
3.通过action,name调用,调用delete方法的url为, url="localhost:8080/HelloStruts2/deletestruts.action"
   <action name="hellostruts" class="com.dunn.action.HelloStruts2" method="hello">
             <result >/result.jsp</result>
         </action>
        <action name="deletestruts" class="com.dunn.action.HelloStruts2" method="delete">
             <result >/result.jsp</result>
         </action> -->
 </package>
 </struts>
View Code

 

Struts2---动态方法调用