Struts.xml文件访问的配置方法

此篇文章介绍三种常见的控制Struts2框架访问

    Action 及 action中的方法的配置希望对读者有帮助

第一种:一般配置

Struts.xml代码:

<struts>

       <package name="Struts2" namespace="/" extends="struts-default" >

              <action name="login" class="org.cjit.Action.LoginAction">

                     <result name="index">Login/Login.jsp</result>

              </action>

       </package>

</struts>

此种方法是Struts2中简单的配置,上述代码配置说明了包的唯一标识名字叫Struts2,访问路径相对于项目是“/

继承了struts-default(只需要记住框架规定即可),包中存在一个action,名称为“login”,映射“org.cjit.Action.LoginAction”,

并在action中定义了result,令一旦调用index就跳转到Login/Login.jsp页面(值得说明的是/Login/Login.jspLogin/login.jsp代表着不同的意义,前者是相对于webroot,后者是相对于当前目录)。

这只是一个例子,如果用户访问loginaction就会调用org.cjit.Action.LoginAction执行execute()方法,

这是Struts默认的方法。当方法返回的值为“index”就会跳转Login.jsp页面。

但上面代码存在些不足,如果我想充分利用action类中的其他方法,那么我要做的配置就要如下:

<struts>

       <package name="Struts2" namespace="/" extends="struts-default" >

              <action name="login" class="org.cjit.Action.LoginAction" method="execute">

                     <result name="index">Login/Login.jsp</result>

              </action>
              <action name="validateclass="org.cjit.Action.LoginAction" method="validate">

                     <result name="succeed">Login/Succeed.jsp</result>

                     <result name="fail">Login/Fail.jsp</result>

              </action>

       </package>

</struts>

可是这样做会造成struts.xml文件存在大量的冗余代码。因此第二种方法就可以派上用途。

第二种:添加标签<constant>

Struts.xml代码:

<struts>

       <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

       <package name="Struts2" namespace="/" extends="struts-default" >

              <action name="login" class="org.cjit.Action.LoginAction">

                     <result name="index">Login/Login.jsp</result>

                     <result name="succeed">Login/Succeed.jsp</result>

                     <result name="fail">Login/Fail.jsp</result>

              </action>

       </package>

</struts>

下面是官方文档描述的:

### Set this to false if you wish to disable implicit dynamic method invocation

### via the URL request. This includes URLs like foo!bar.action, as well as params

### like method:bar (but not action:foo).

### An alternative to implicit dynamic method invocation is to use wildcard

### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">

struts.enable.DynamicMethodInvocation = true

这里说的很清楚,在请求路径时以 “action!方法名”格式进行访问,即可实现。例如:login!execute

意思就是调用名为loginactionexecute()方法。

这样就可以调用相应的action执行相应的方法。

第三种:利用通配符进行配置(这种配置相对于第二种灵活性得到了更大的提高)

Struts.xml代码:

<struts>

       <package name="Struts2" namespace="/" extends="struts-default" >

              <action name="*-*" class="org.cjit.Action.{1}Action" method="{2}">

                     <result name="index">Login/Login.jsp</result>

                     <result name="succeed">Login/Succeed.jsp</result>

                     <result name="fail">Login/Fail.jsp</result>

              </action>

       </package>

</struts>

此方法规定的格式为“ *-* ”,后面参数{1}代表第一个*{2}代表第二个*,这样通过两个*得到相应的action及其action的方法。但这种做法需要在表单提交页面中指明action的地址,例如:<form action="Login-validate" method="post">

以上三种常见的配置,渐渐的提高了struts.xml的灵活性。个人希望还有更好的奉献给大家,可是自己知识量优先,真诚以待,欢迎读者传授心得好用的方法。

--yrk

  2012.02.01