Lesson 1
參考網址:
Simple Example
http://struts.apache.org/release/2.1.x/docs/webxml.html
ActionSupport
http://struts.apache.org/release/2.0.x/struts2-core/apidocs/index.html?com/opensymphony/xwork2/ActionSupport.html
hello-world-using-struts-2
C:\WebAppTechCamp-Struts2\sdk\struts\struts-2.2.1\docs\WW\hello-world-using-struts-2.html
Struts Tag - url
C:\WebAppTechCamp-Struts2\sdk\struts\struts-2.2.1\docs\WW\url.html
EL
http://docs.oracle.com/javaee/6/tutorial/doc/bnaim.html
http://010cnc.net/subject/about/Struts2.xml%20%E4%B8%ADConstant%E7%9A%84%E9%85%8D%E5%82%99.html
有關Struts.xml 內constant 及strut 的tag規則可看上方這網址:
Ex:
1.便於排錯,列印出更詳細的錯誤信息
<constant name="struts.devMode" value="true">
2.上傳文件大小限制
<struts name="struts.multipart.maxSize" value="10241024"/>
今天課程主要再講Struts 2 架構的傳值及接值
首先前緒
講解 Struts 1 與 Struts 2 之間的差異
1x
中間是透過"Action Servelet" 去傳送命令給Action1 ..Action2 ...........
而2x
中間則透過Filter(web.xml) 及 struts.xml(可有可無) 傳送給 Action1 ..Action2 ...........
web.xml 通常是在專案中的 WebContent >> Web-INF 內
Jsp 頁面則在 WebContent下
Java檔則在src 下(通常在Packge下)
Struts 也會在 src第一層
// 關於Struts 2 的lib 我們可去官網下載 http://struts.apache.org/release/2.3.x/
Web.xml
有了以上的基本目錄架構就先來看 Web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
把已上Filter 範例放入 web.xml內(放在<web-app> 下) ,這邊暫時沒有我們要注意的
HelloWorldAction.jave
建立一個Class 名為HelloWorldAction去繼承ActionSupport (可透過SuperClass 去選取)
OK後 ActionSupport 須實做 execute() 方法如下
private String user;
public String execute() throws Exception {
if (user != null) {
return "dosomething";
} else {
return SUCCESS;
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
新增一個變數名為 user 並給予Get And Set 上面的user 就是這麼來的
Struts
接下來看 Struts ,新增至Src 下 放入已下範例Code
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="hello" class="my.test.struts2.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
<result name="dosomething">/HelloSomeone.jsp</result>
</action>
</package>
</struts>
看以上的Code 建立一個Action 名為hello ,並指為定執行的class 為 my.test.struts2.action.HelloWorldAction
method="execute" 不設也可以,因預設就是execute
下面的兩個result 指的就是 當接收到 "success" 時 則連到 HelloWorld.jsp 頁面
相對 接收到 "dosomething" 時 則連到HelloSomeone.jsp 頁面
index.Jsp
<a href="<s:url action='hello' />">Hello World</a>
</p>
<p>
<s:url action="hello" var="helloLink">
<s:param name="user">Kitty</s:param>
</s:url>
<a href="${helloLink}">Hello Link</a>
以上先看到第一行 <a href="<s:url action='hello' />">Hello World</a>
去呼叫struts.xml 的hello (Action) 若class回傳 "success" 則連結為HelloWorld.jsp
若回傳為 "dosomething" 時 則連結為HelloSomeone.jsp
<s:url action="hello" var="helloLink">
<s:param name="user">Kitty</s:param>
</s:url>
<a href="${helloLink}">Hello Link</a>
而上方這段則是 類似做一個路徑 <s:param name="user">Kitty</s:param> 然後左邊這就是
一個名為user參數 ,參數為Kitty ,則對應呼叫到class內的user ,連至下一頁,下一頁則用property來接
如<s:property value="%{user}" /> ps 這是屬於OGNL的方法
另var = "helloLink" 去接如 ${helloLink} , ${helloLink}這種接法是傳統的servlet方式
Ps: 如果說<s:url action="hello" var="helloLink"> 沒有設定var值,則會直接顯示此Action的回傳頁面
就不需要用 ${} 去接值