Struts2教程3:struts.xml常用配置解析

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:
在本文中将详细讲述struts.xml 文件的常用配置及注意事项。
1.         使用<include> 标签重用配置文件
在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签引用这些配置文件。这样做的优点如下:
结构更清晰,更容易维护配置信息。
配置文件可以复用。如果在多个Web 程序中都使用类似或相同的配置文件,那么可以使用<include> 标签来引用这些配置文件,这样可以减少工作量。
假设有一个配置文件,文件名为newstruts.xml ,代码如下:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
    
< package  name ="demo"  extends ="struts-default"   >
        
< action  name ="submit"   class ="action.MoreSubmitAction" >
            
< result  name ="save"   >
                /result.jsp
            
</ result >
            
< result  name ="print" >
                /result.jsp
            
</ result >
        
</ action >             
    
</ package >     
</ struts >

  struts.xml 引用 newstruts.xml 文件的代码如下:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
    
< include  file ="newstruts.xml" />
    
< package  name ="test"  extends ="struts-default" >
     
    
</ package >     
</ struts >

大家要注意一下,用<include> 引用的xml 文件也必须是完成的struts2 的配置。实际上<include> 在引用时是单独解析的xml 文件,而不是将被引用的文件插入到struts.xml 文件中。
2.         action 的别名
 
     在默认情况下, Struts2 会调用动作类的 execute 方法。但有些时候,我们需要在一个动作类中处理不同的动作。也就是用户请求不同的动作时,执行动作类中的不同的方法。为了达到这个目的,可以在 <action> 标签中通过 method 方法指定要指行的动作类的方法名,并且需要为不同的动作起不同的名子(也称为别名)。如下面代码所示:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
< package  name ="demo"  extends ="struts-default"   >
    
< action  name ="test"   class ="action.MyAction" >
        
    
</ action >             
    
< action  name ="my"   class ="action. MyAction"  method ="my" >
         
    
</ action >             
</ package >     
</ struts >
上面代码的两个动作的class 属性都指向同一个类,name 为这个类起了两个动作别名:test my 。在动作my 中,使用了method 属性指定要要运行的方法名为my
     MyAction 类中必须要有 my 方法,代码如下:

package  action;

import  com.opensymphony.xwork2.ActionSupport;

public   class  MyAction  extends  ActionSupport
{
     
    
public  String execute()  throws  Exception
    {
        
//  处理test动作的代码
    }
    
public  String my()  throws  Exception
    {
          
//  处理my动作的代码
    }
     
}


除了在struts.xml 中配置别名,还可以通过请求参数来描述指定动作(并不需要在struts.xml 中配置)。请求参数的格式如下:
http://localhost:8080/contextPath/actionName!method.action
关于通过请求指定动作的详细内容,请参阅笔者写的Struts2教程2:处理一个form多个submit
3.         action 指定参数
struts2 中还可以为action 指定一个或多个参数。大家还记着struts1.x 是如何设置的action 参数不?  struts1.x 中可以使用<action> 标签的parameter 属性为其指定一个action 参数,如果要指定多个,就只能通过逗号(, )或其他的分隔符将不同的参数隔开。而在struts2 中可以通过<param> 标签指定任意多个参数。代码如下:

< action  name ="submit"   class ="action.MyAction" >
< param  name ="param1" > value1 </ param >
< param  name ="param2" > value2 </ param >
    
< result  name ="save"   >
        /result.jsp
    
</ result >
     
</ action >         

    当然,在 action 中读这些参数也非常简单,只需要象获取请求参数一样在 action 类中定义相应的 setter 方法即可(一般不用定义 getter 方法)。如下面的代码将读取 param1 param2 参数的值:
package  action;

import  com.opensymphony.xwork2.ActionSupport;

public   class  MyAction  extends  ActionSupport
{
    
private  String param1;
    
private  String param2;

    
public  String execute()  throws  Exception
    {
        System.out.println(param1 
+  param2);
    }
    
public   void  setParam1(String param1)
    {
        
this .param1  =  param1;
    }
    
public   void  setParam2(String param2)
    {
        
this .param2  =  param2;
    }
     
}

struts2 在调用execute 之前,param1 param2 的值就已经是相应参数的值了,因此,在execute 方法中可以直接使用param1 param2
4.         选择result 类型
 
在默认时,<result> 标签的type 属性值是“dispatcher ”(实际上就是转发,forward )。开发人员可以根据自己的需要指定不同的类型,如redirect stream 等。如下面代码所示:

<result name="save" type="redirect">
       /result.jsp
</result>
这此result-type 可以在struts2-core-2.0.11.1.jar 包或struts2 源代码中的struts-default.xml 文件中找到,在这个文件中找到<result-types> 标签,所有的result-type 都在里面定义了。代码如下:

< result-types >
       
< result-type  name ="chain"  class ="com.opensymphony.xwork2.ActionChainResult" />
       
< result-type  name ="dispatcher"  class ="org.apache.struts2.dispatcher.ServletDispatcherResult"  default ="true" />
       
< result-type  name ="freemarker"  class ="org.apache.struts2.views.freemarker.FreemarkerResult" />
       
< result-type  name ="httpheader"  class ="org.apache.struts2.dispatcher.HttpHeaderResult" />
       
< result-type  name ="redirect"  class ="org.apache.struts2.dispatcher.ServletRedirectResult" />
       
< result-type  name ="redirectAction"  class ="org.apache.struts2.dispatcher.ServletActionRedirectResult" />
       
< result-type  name ="stream"  class ="org.apache.struts2.dispatcher.StreamResult" />
       
< result-type  name ="velocity"  class ="org.apache.struts2.dispatcher.VelocityResult" />
       
< result-type  name ="xslt"  class ="org.apache.struts2.views.xslt.XSLTResult" />
       
< result-type  name ="plainText"  class ="org.apache.struts2.dispatcher.PlainTextResult"   />
       
<!--  Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707  -->
       
< result-type  name ="redirect-action"  class ="org.apache.struts2.dispatcher.ServletActionRedirectResult" />
       
< result-type  name ="plaintext"  class ="org.apache.struts2.dispatcher.PlainTextResult"   />
</ result-types >

5.         全局result
有很多时候一个<result> 初很多<action> 使用,这时可以使用<global-results> 标签来定义全局的<result> ,代码如下:

< struts >
    
< package  name ="demo"  extends ="struts-default" >
        
< global-results >
            
< result  name ="print" > /result.jsp </ result >
        
</ global-results >
        
< action  name ="submit"  class ="action.MoreSubmitAction" >
         
        
</ action >
        
< action  name ="my"  class ="action.MoreSubmitAction"  method ="my" >
         
        
</ action >
    
</ package >
</ struts >

   如果<action>中没有相应的<result>Struts2就会使用全局的<result>。






 本文转自 androidguy 51CTO博客,原文链接:http://blog.51cto.com/androidguy/216509,如需转载请自行联系原作者

目录
打赏
0
0
0
0
348
分享
相关文章
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
78 1
阿里云特惠云服务器99元与199元配置与性能和适用场景解析:高性价比之选
2025年,阿里云长效特惠活动继续推出两款极具吸引力的特惠云服务器套餐:99元1年的经济型e实例2核2G云服务器和199元1年的通用算力型u1实例2核4G云服务器。这两款云服务器不仅价格亲民,而且性能稳定可靠,为入门级用户和普通企业级用户提供了理想的选择。本文将对这两款云服务器进行深度剖析,包括配置介绍、实例规格、使用场景、性能表现以及购买策略等方面,帮助用户更好地了解这两款云服务器,以供参考和选择。
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
51 15
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
DHCP与DNS的配置
通过这些步骤,您可以在Linux环境下成功配置和验证DHCP和DNS服务。希望这些内容对您的学习和工作有所帮助。
121 27
详细介绍SpringBoot启动流程及配置类解析原理
通过对 Spring Boot 启动流程及配置类解析原理的深入分析,我们可以看到 Spring Boot 在启动时的灵活性和可扩展性。理解这些机制不仅有助于开发者更好地使用 Spring Boot 进行应用开发,还能够在面对问题时,迅速定位和解决问题。希望本文能为您在 Spring Boot 开发过程中提供有效的指导和帮助。
97 12
2025年阿里云弹性裸金属服务器架构解析与资源配置方案
🚀 核心特性与技术创新:提供100%物理机性能输出,支持NVIDIA A100/V100 GPU直通,无虚拟化层损耗。网络与存储优化,400万PPS吞吐量,ESSD云盘IOPS达100万,RDMA延迟<5μs。全球部署覆盖华北、华东、华南及海外节点,支持跨地域负载均衡。典型应用场景包括AI训练、科学计算等,支持分布式训练和并行计算框架。弹性裸金属服务器+OSS存储+高速网络综合部署,满足高性能计算需求。
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
95 6
DNS是什么?内网电脑需要配置吗?
【10月更文挑战第22天】DNS是什么?内网电脑需要配置吗?
657 1

推荐镜像

更多