struts2的动态方法以及通配符使用

在struts2开发中,怎么实现与struts1的DisptchAction,在struts2中提供了两种方式去实现:

1、是动态的方法:例如我们action中有2个方法,一个add,和save,我们需要分别访问,只需要在action名称后加上一个!add或者save,就能进入具体的方法。但是在struts2的2.1文档中已经不推荐这种方式。

2、是通配符的方法:

     

<?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.0.dtd">

<struts>
   
   <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
   <constant name="struts.configuration.xml.reload" value="true"></constant>
   
   <package name="hello" namespace="/test" extends="struts-default">
    <action name="helloworld_*" class="com.strtus.demo.hellowrod" method="{1}" >
			<result name="succes">/WEB-INF/page/hello.jsp</result>
      </action>
        
    </package> 
    
</struts>

   我们只需要在action的名称后面加上一个

*,然后在method用{1}去获取到这个名称,就可以请求到具体的方法。列入helloword_add这样就可以到达action的add方法。注意的是{1}是可以在任何地方使用的。

猜你喜欢

转载自minzhenhua.iteye.com/blog/1848241