Struts2中运用通配符访问action的方法

Struts2中使用通配符访问action的方法

 

struts.xml 定义
  1. <!-- 使用缺省的struts的配置文件,struts2的支持jar包: struts2-core-2.0.11.2.jar中 -->
  2.     <include file="struts-default.xml" />
  3.     <!--
  4.         Struts2常量配置 struts.devMode:开发模式,修改配置信息可以不必重新部署服务器
  5.         struts.i18n.encoding:国际化编码
  6.         struts.ognl.allowStaticMethodAccess:允许ognl访问静态方法
  7.     -->
  8.     <constant name="struts.devMode" value="true" />
  9.     <constant name="struts.i18n.encoding" value="UTF-8" />
  10.     <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
  11.     <constant name="struts.custom.i18n.resources" value="globalMessages" />
  12.     
  13.     <!--定义全局变量-->
  14.     <package name="global" namespace="/" extends="struts-default">
  15.         <global-results>
  16.             <result name="login">/login.jsp</result>
  17.             <result name="error">/WEB-INF/error.jsp</result>
  18.         </global-results>
  19.         <!-- 全局返回结果在上,全局异常定义在下,不要弄反了。 -->
  20.         <global-exception-mappings>
  21.             <exception-mapping result="error" exception="Exception"></exception-mapping>
  22.         </global-exception-mappings>
  23.     </package>
  24.     <package name="webconfig" namespace="/" extends="global" >
  25.         <action name="config" class="action.Webconfig">
  26.             <result name="success">/WEB-INF/pages/web/webinfo.jsp</result>
  27.         </action>
  28.        
  29.         <!-- 使用通配符匹配方法 -->
  30.         <action name="ajax_*" class="action.Ajax" method="{1}">
  31.             <result name="success">/WEB-INF/pages/web/webinfo.jsp</result>
  32.         </action>
  33.          
  34.     </package>

Java代码
  1. public class Ajax extends ActionSupport {
  2.     public String error() throws Exception{
  3.         ActionContext.getContext().getSession().put("message","error");
  4.         System.out.println("error");
  5.         throw new Exception();//直接产生异常验证全局异常设置       
  6.     }
  7.     public String success(){
  8.         ActionContext.getContext().getSession().put("message","success");
  9.         System.out.println("success");
  10.         return "login";
  11.     }
  12.     public String execute() throws Exception {
  13.         ActionContext.getContext().getSession().put("message","execute");
  14.         System.out.println("execute");
  15.         return "login";
  16.     }
  17. }
页面代码
  1. <s:action name="ajax!error" namespace="/" executeResult="true"></s:action>
  2.         <!--假如ajax!error中有session定义,那么session取值应在ajax!error执行后 -->
  3.         <div style="height: auto; width: auto; background-color: red">
  4.             <s:property value="#session.message" />
  5.         </div>

  6.         <s:form method="post" action="config.action">
  7.             <s:textfield name="drepName" value="%{drepName}" label="单位名称:" />
  8.             <s:submit></s:submit>
  9.         </s:form>
  10.         <%=session.getAttribute("message")%>