struts2动作方法通配符
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 覆盖掉Struts2一些常量的默认值 --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action,,do"></constant> <constant name="struts.configuration.xml.reload" value="true"></constant> <package name="default" namespace="/hello/xxx" extends="struts-default"> <action name="test1"> <result>/index.jsp</result> </action> </package> <package name="p1" extends="struts-default"> <action name="test2" class="com.opensymphony.xwork2.ActionSupport" method="execute"> <result name="success" type="dispatcher">/index.jsp</result> </action> <action name="test3" class="com.itheima.actions.ActionDemo1" method="doSomething"> <result>/index.jsp</result> </action> <!-- <action name="testAdd" class="com.itheima.actions.ActionDemo1" method="addMethod"> <result>/index.jsp</result> </action> <action name="testUpdate" class="com.itheima.actions.ActionDemo1" method="updateMethod"> <result>/index.jsp</result> </action> --> <!-- 动作方法通配符 --> <!-- 约定优于配置 路径:http://localhost:8080/day24_03struts2_constant/CustomerAction_addCustomer.action 动作类:CustomerAction 动作方法:addCustomer 结果转向的页面:addCustomer.jsp --> <action name="*_*" class="com.itheima.actions.{1}" method="{2}"> <result>/{2}.jsp</result> </action> </package> </struts>
注意第十行,value="action,,do“ 中间两个逗号,这样配置可以在访问路径的时候省略后面的.action
CustomerAction.java:
package com.itheima.actions; import com.opensymphony.xwork2.ActionSupport; public class CustomerAction extends ActionSupport{ public String addCustomer(){ System.out.println("addCustomer"); return SUCCESS; } public String delCustomer(){ System.out.println("delCustomer"); return SUCCESS; } public String updateCustomer(){ System.out.println("updateCustomer"); return SUCCESS; } }