分模块配置:在使用Struts中,有时候将所有action全部写在struts.xml文件中会显得难以管理所以我们可以使用分配模块的方式管理。
<struts>
<include file="hello.xml"></include>
</struts>
hello.xml文件中进行配置即可。
<struts>
<package name="first" extends="struts-default">
<action name="hello_*" class="com.wood.action.HelloWorld01" method="{1}">
<result name="success">helloWorld.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
通配符:通配符的使用是为了让我们在访问action的同时进行匹配,可大量减少重复的action,并且可以在method中调用通配的方法。例如在上一片模块的action配置中,*代表这匹配前面后面字段,后面的方法则是代表执行匹配通过的方法名。例如hello_add匹配能通过,并且会执行add方法。
动态方法调用:在action中加入如下一行代码,然后在访问action的时候加上感叹号和方法名,就能在访问action的时候调用制定的方法。
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
访问hello这个action的时候,使用 hello!add ,那么就能在访问hello的同时调用add这个方法,当然这个方式不怎么安全,官网建议不使用,开发中多使用通配符比较好。