上一篇: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爲這個類起了兩個動道別名:test和my。在動做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以前,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>,代碼以下:
若是
<action>
中沒有相應的
<result>
,
Struts2
就會使用全局的
<result>。
下一篇:Struts2教程4:使用validate方法驗證數據