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

上一篇:Struts2教程2:处理一个form多个submit
html

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

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

在Struts2中提供了一个默认的struts.xml文件,但若是package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。所以,就须要将struts.xml文件分红多个配置文件,而后在struts.xml文件中使用<include>标签引用这些配置文件。这样作的优势以下:
jsp

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

配置文件能够复用。若是在多个Web程序中都使用相似或相同的配置文件,那么可使用<include>标签来引用这些配置文件,这样能够减小工做量。 spa

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


<? 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文件中。 orm

2.        action的别名 xml

 

     在默认状况下, 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为这个类起了两个动道别名:testmy。在动做my中,使用了method属性指定要要运行的方法名为my htm

     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以前,param1param2的值就已是相应参数的值了,所以,在execute方法中能够直接使用param1param2

4.        选择result类型

 

在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward)。开发人员能够根据本身的须要指定不一样的类型,如redirectstream等。以下面代码所示:

<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>。


下一篇:Struts2教程4:使用validate方法验证数据
相关文章
相关标签/搜索