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

开发 后端
本文为Struts2教程,本部分讲解struts.xml常用配置解析。Struts2虽然在大版本号上是第二个版本,但基本上在配置和使用上已经完全颠覆了Struts1.x的方式。

在本文中将详细讲述struts.xml文件的常用配置及注意事项。

1. 使用< include>标签重用配置文件

在Struts2中提供了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用< include>标签引用这些配置文件。这样做的优点如下:

结构更清晰,更容易维护配置信息。

配置文件可以复用。如果在多个Web程序中都使用类似或相同的配置文件,那么可以使用< include>标签来引用这些配置文件,这样可以减少工作量。

假设有一个配置文件,文件名为newstruts.xml,代码如下:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6.     < package name="demo" extends="struts-default" > 
  7.         < action name="submit"  class="action.MoreSubmitAction"> 
  8.             < result name="save" > 
  9.                 /result.jsp  
  10.             < /result> 
  11.             < result name="print"> 
  12.                 /result.jsp  
  13.             < /result> 
  14.         < /action>              
  15.     < /package>      
  16. < /struts> 

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

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

大家要注意一下,用< include>引用的xml文件也必须是完成的struts2的配置。实际上< include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。

2. action的别名

在默认情况下,Struts2会调用动作类的execute方法。但有些时候,我们需要在一个动作类中处理不同的动作。也就是用户请求不同的动作时,执行动作类中的不同的方法。为了达到这个目的,可以在< action>标签中通过method方法指定要指行的动作类的方法名,并且需要为不同的动作起不同的名子(也称为别名)。如下面代码所示:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6. < package name="demo" extends="struts-default" > 
  7.     < action name="test"  class="action.MyAction"> 
  8.           
  9.     < /action>              
  10.     < action name="my"  class="action. MyAction" method="my"> 
  11.            
  12.     < /action>              
  13. < /package>      
  14. < /struts> 

上面代码的两个动作的class属性都指向同一个类,name为这个类起了两个动作别名:test和my。在动作my中,使用了method属性指定要要运行的方法名为my。

在MyAction类中必须要有my方法,代码如下:

  1. package action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class MyAction extends ActionSupport  
  6. {  
  7.        
  8.     public String execute() throws Exception  
  9.     {  
  10.         // 处理test动作的代码  
  11.     }  
  12.     public String my() throws Exception  
  13.     {  
  14.           // 处理my动作的代码  
  15.     }  
  16.        
  17. }  

除了在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>标签指定任意多个参数。代码如下:

  1. < action name="submit"  class="action.MyAction"> 
  2. < param name="param1">value1< /param> 
  3. < param name="param2">value2< /param> 
  4.     < result name="save" > 
  5.         /result.jsp  
  6.     < /result> 
  7.        
  8. < /action> 

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

  1. package action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class MyAction extends ActionSupport  
  6. {  
  7.     private String param1;  
  8.     private String param2;  
  9.  
  10.     public String execute() throws Exception  
  11.     {  
  12.         System.out.println(param1 + param2);  
  13.     }  
  14.     public void setParam1(String param1)  
  15.     {  
  16.         this.param1 = param1;  
  17.     }  
  18.     public void setParam2(String param2)  
  19.     {  
  20.         this.param2 = param2;  
  21.     }  
  22.        
  23. }  

当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都在里面定义了。代码如下:

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

5. 全局result

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

  1. < struts> 
  2.     < package name="demo" extends="struts-default"> 
  3.         < global-results> 
  4.             < result name="print">/result.jsp< /result> 
  5.         < /global-results> 
  6.         < action name="submit" class="action.MoreSubmitAction"> 
  7.            
  8.         < /action> 
  9.         < action name="my" class="action.MoreSubmitAction" method="my"> 
  10.            
  11.         < /action> 
  12.     < /package> 
  13. < /struts> 

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

【编辑推荐】

  1. Struts2教程:处理一个form多个submit
  2. Struts2教程:第一个Struts2程序
  3. Struts2中POI在内存中生成文件并下载
  4. Struts2深入详解properties配置文件
  5. Struts2 iterator介绍及功能详解
责任编辑:yangsai 来源: BlogJava
相关推荐

2009-02-04 11:37:15

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-06-05 10:55:07

struts2 web

2009-07-29 09:54:34

struts2和str

2009-06-03 14:19:34

Struts2Guice

2009-06-25 16:04:30

2009-02-04 15:04:13

2009-06-25 15:54:42

Struts2教程拦截器

2009-06-25 15:50:03

Struts2教程上传任意多个文件

2009-06-25 15:33:12

Struts2教程使用validate验证数据

2009-06-25 15:37:12

Struts2教程Validation框

2009-02-04 14:19:38

2009-02-04 14:00:59

2009-07-03 09:35:57

Struts2 JSP

2009-06-05 10:52:45

struts2深入详解配置文件

2009-06-25 15:59:21

Struts2教程拦截器

2009-02-04 12:00:08

2009-06-05 10:05:50

struts menustruts2
点赞
收藏

51CTO技术栈公众号