5.Struts.xml标签中的一些小技巧

1.为Action属性注入值
如给actions包下的test.java类中变量username赋值为"xingyun"
在struts.xml中添加下面这段代码:

<struts>
     <action name="one" class="actions.test" method="other">
      
       <param name="username">xingyun</param>
      
        <result name="other">/welcome.jsp</result>
    
      </action>
</struts>

2.浏览器访问文件后缀默认值

 http://pc2014092716pel:8080/Study_Struts2/test/user.action 

伪装访问后缀如:

http://pc2014092716pel:8080/Study_Struts2/test/user.php

伪装扩展名方法:
在struts.xml中添加下面这段代码:

<struts>
         <constant name="struts.action.extension" value="php,asp,aspx,c,cpp">
</struts>

3.当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),
开发阶段最好打开。
   在struts.xml中添加下面这段代码:

   <constant name="struts.configuration.xml.reload" value="true"/>

4.开发模式下使用,可以打印出更详细的错误信息。
  在struts.xml中添加下面这段代码:

<constant name="struts.devMode" value="true" />      

5.修改上传文件大小限制:默认最大2M
使用方法:
    在struts.xml中添加下面这段代码:

    <constant name="struts.multipart.maxSize" value="5242880"/>

6.配置多个struts.xml文件+全局视图
主配置struts.xml文件:

<struts>
   
    <package name="base" abstract="true" extends="struts-default">
            <global-results>
                              <result>/index.jsp</result>
            </global-results>
    </package>
    
    <#include file="struts-one.xml"/>
    <#include file="struts-one.xml"/>

</struts>

strut-one.xml配置如下:

<struts>
         <package name="one" namespace="/one" extends="base">
         <action name="user" class="actions.UserAction"/>
</struts>

strut-two.xml配置如下:

<struts>
         <package name="two" namespace="/two" extends="base">
         <action name="user2" class="actions.UserAction"/>
</struts>

7.若Action中存在多个方法,并且需要在请求时才可确定访问哪个方法,即不能使用action标签中
method属性提前指定时,可以使用“!+方法名”方式动态调用指定方法

public class HelloAction{
    public String first() { // action标签中method属性指定方法
        return "success";
    }
    public String other() {
        return "success";
    }
}

若访问上面action的URL路径为:…/test/hello.action,则访问的方法为action中method属性指定的方法first()。

若要访问other() 方法,则可以这样调用:       

                           …/test/hello!other.action

        不过,Struts2文档不建议使用动态方法调用,所以可以通过改变常量struts.enable.DynamicMethodInvocation的值来关闭动态方法调用功能:

禁止使用动态方法调用方法:
在struts.xml中添加下面这段代码:

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

可以使用通配符定义的action来实现类似动态方法调用的功能。
通配符定义action,主要体现在配置文件上。如下:

<package name="itcast" namespace="/test" extends="struts-default">
     <action name="hello_*" class="actions.HelloAction" method="{1}">
    <result name="success">hello.jsp</result>
     </action>
</package>
原文地址:https://www.cnblogs.com/xingyunblog/p/4007369.html