** 請參考 Struts2 - The MVC / Struts2 Architecture @ StartingStruts2Online2.pdf**
Struts2中 大致上區分出了五個區塊
1. Actions
2. Interceptors
3. Value Stack / OGNL
4. result types
5. result view
<hr />
在struts2中,controller 是用 struts2 dispatcher servlet filter來實做,做成 interceptors
( interceptor 這個可以參考J2EE design pattern)
model 則是由 actions處理,view 則是由 result types 以及 result view來組成。
Value stacj and OGNL 提供了常見的 thread , linking 以及跟其他元件整合的彈性。
Configuration
首先要在web.xml中加入filter的設定。
<filter>
<filter-name><filter>
<filter-name>action2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></filter-name></filter><filter-mapping><url-pattern></url-pattern>
</filter-mapping>
接著是 struts.properties --> 作為設定運作環境
通常是不會去改它,除非你有需要更多的資訊進行debug!!
如果要找最新的strutsproperties 資料,可以找下面這個link.
<span style="font-weight: bold;">http://struts.apache.org/2.x/docs/strutsproperties.html</span><span style="font-weight: bold;"><span style="font-weight: bold;"><span style="font-weight: bold;"><span style="font-weight: bold;"></span></span></span></span>
在struts2- core jar dist中,有放了一個預設標準的properties file called default.properties,
可以提供user自行修改,並將該檔案放到classpath中即可。
以下是幾個常見在開發過程中,需要調整的props.
? struts.i18n.reload = true – enables reloading of internationalization files
? struts.devMode = true – enables development mode that provides more comprehensive debugging
? struts.configuration.xml.reload = true – enables reloading of XML configuration files (for the action)
when a change is made without reloading the entire web application in the servlet container
? struts.url.http.port = 8080 – sets the port that the server is run on
(so that generated URLs are created correctly)
接下來看的是struts.xml
其實可以不需要使用struts.xml,但前提是需要使用annotation方式將資訊綁定到 web.xml。
參考範例:
<struts>
<package name="struts2" extends="struts-default" namespace="/struts2">
…
</package>
</struts>
The <> <include> tag : 用來將已模組化的struts2 app載入
ex:
<struts><include file="billing-config.xml"><include file="admin-config.xml">
<include file="reports-config.xml"></include><struts>
<include file="billing-config.xml">
<include file="admin-config.xml">
<include file="reports-config.xml">
…
</include>
特別注意的是,當使用了include tag,則引入的順序是有影響的!
The <package> tag :
The attributes for this tag are:
name – a developer provided unique name for this package
extends – the name of a package that this package will
extend; all configuration information (including action
configurations) from the extended package will be
available in the new package, under the new namespace
namespace – the namespace provides a mapping from the URL to the package.
i.e. for two different packages,with namespace attributes defined
as “package1” and“package2”, the URLs would be something like
“/myWebApp/package1/my.action” and
“/myWebApp/package2/my.action”
abstract – if this attribute value is “true” the package is truly configuration grouping,
and actions configured will not be accessible via the package name
<hr />Actions and Single Result
EX:
class MyAction {
public void String execute() throws Exception {
return "success";
}
}
在struts2中,action不需要去繼承任何的class,也不需要實作任何的interface
只需要一個很簡單的pojo ! ~ 靠 idol wow ~~
而需要注意到的是,上述的method execute(),是預設被呼叫時轉換的method name,
如果想用其他method name,則必須要在 actions configuration file 修改。
不管你的 method name叫啥,最後的erturn type 都必須是 java.lang.String。
當然如果必要的話,這個action class method 也可以宣告Exceptions
以下提供一個action configuration file.
<action name="my" class="com.fdar.infoq.MyAction">
<result>view.jsp</result>
</action>
The attribute “name” provides the URL information to execute
the action, in this case a URL of “/my.action”. The extension
“.action” is configured in the “struts.properties”iv configuration
file. The attribute “class” provides the full package and class
name of the action to be executed.
<hr />
Multi-Results
如果action處理結果可能會有多種情形時,那嚜就必須提供不同的view link
參考以下action class
class MyAction {
public void String execute() throws Exception {
if( myLogicWorked() ) {
return "success";
} else {
return "error";
}
}
}
如上述的兩種結果,則 action config file如下
<action name="my" class="com.fdar.infoq.MyAction">
<result>view.jsp</result>
<result name="error">error.jsp</result>
</action>
This introduces a new “name” attribute of the result node. In
fact, it has always been there. The value (as in the first result
configuration) defaults to a value of “success” if not provided by
the developer.
沒有留言:
張貼留言