環境搭建
項目目錄
導入依賴jar包,如上圖lib目錄所示。
不同的版本可能會不一樣,沒關系在tomcat啟動時,如果報錯java.lang.ClassNotFoundException,我們可以按照錯誤提示添加相應jar包。
在web.xml中配置struts2過濾器
將Struts2所帶的過濾器org.apache.struts2.dispatcher.FilterDispatcher配置到工程的web.xml文件中,默認情況下,該過濾器攔截請求字符串中以.action結尾的請求,並將該請求委托給指定的Action進行處理。
在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元素中加入<init-param>子元素來設定struts.xml配置文件的路徑。若設置了<param-name>config</param-name>參數,那struts-default.xml等原來struts2默認加載的文件也要手動指定,否則不會自動加載。
struts-default.xml的路徑在struts2-core-2.x.x jar包底下
編寫一個struts小程序的步驟
1.創建一個類來存儲歡迎消息(模型)
2.創建一個服務器頁面呈現消息(視圖)
3.創建一個頁面提交用戶請求參數(表單)
4.創建一個Action類來控制用戶之間的互動,模型和視圖(控制器)
5.創建一個映射(struts.xml)來描述Action類和視圖的關系。
1.存儲消息MessageStore.java
package hello; public class MessageStore { private String message; public MessageStore() { setMessage("Hello Struts User"); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String toString(){ return message; } }
2.呈現消息Hello.jsp
在這里簡單運用一些struts標簽,取得action的屬性值,簡化信息的顯示。在這之前,我們需要引入struts標簽庫
<%@ taglib prefix="s" uri="/struts-tags" %>
struts2屬性標記為<s:property>,主要有下面一些用法。
1.顯示Action中的屬性值:<s:property value="屬性名" />
2.顯示字符串,使用單引號:<s:property value="'字符串'" />
3.顯示默認值,Action中找不到指定的屬性時,顯示default屬性的值:<s:property value="屬性名" default="默認值" />
4.解析HTML字符串,escape默認值為true,直接輸出字符串,escape設為false時,解析HTML字符串:<s:property value="'<font color=\"red\">Red</font>'" escape="false" />
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Hello World!</title> </head> <body> <h2><s:property value="MessageStore" /> ${sessionScope.user}</p></h2> <p>Number of visit this site: <s:property value="helloCount" /> times!</p> </body> </html>
3.提交請求index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Basic Struts 2 Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <s:form action="hello"> <s:textfield name="user" label="userName"/> <s:submit value="提交"></s:submit> </s:form> </body> </html>
4.處理用戶請求HelloAction.java
HelloAction繼承ActionSupport,充當控制器的角色,用於:
1.接受用戶的請求參數
2.處理用戶請求(或者調用邏輯處理)
3.返回String類型的結果,告訴Struts要呈現什么的視圖。
接受請求參數
在action中設置參數屬性,添加getter和setter方法,讓action自動填充,並做恰當的類型轉化。
處理請求
我們可以在execute方法中處理用戶的請求,直接進行處理請求參數或者調用service的方法。該方法有一個String類型的返回值,告訴Struts應該將什么視圖返回給用戶。
package hello; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport { private static final long serialVersionUID = 1L; private static int helloCount = 0; private MessageStore messageStore; private String user; public String execute() throws Exception { messageStore = new MessageStore() ; helloCount++; ActionContext.getContext().getSession().put("user", user); return SUCCESS; } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public int getHelloCount() { return helloCount; } public void setHelloCount(int helloCount) { this.helloCount = helloCount; } }
5.struts.xml配置
我們將在struts.xml定義處理結果字符串和資源之間的映射關系,即定義action的execute方法返回的字符串所對應的視圖。
在工程類路徑下創建struts.xml文件,這是Struts2的配置文件,在struts.xml文件中可以配置Action、Bean、Interceptor等組件。
<?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> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="hello" class="hello.HelloAction" method="execute"> <result name="success">/Hello.jsp</result> </action> </package> </struts>
注:WEB應用程序的類路徑是指WEB-INF/classes目錄,在Eclipse中,創建在src目錄下的文件最終發布后會自動復制到WEB-INF/classes目錄下。
代碼清單3中涉及到很多標簽,以下是簡單的解釋:
標簽名稱 |
說明 |
include |
包含其他xml文件,在示例中,這意味着struts.xml可以訪問定義在struts-default.xml文件中的組件。 該元素可以使得Struts2定義多個配置文件,“分而治之”。 要注意的是,任何一個struts2配置文件都應該和struts.xml有相同的格式,包括doctype,並且可以放在類路徑下的任何地方。 |
package |
為Action或截攔器分組。 name:名稱,必填項,名稱自定義,但不能相同。方便別的package引用。 extends:package能繼承其他的package,即通過該屬性實現,值為另一個package的name。 在示例中,extends =”struts-default”是從struts-default.xml中繼承的。 |
action |
定義Action,name屬性為訪問時用到的名稱,class屬性是Action的類名。 |
result |
根據Action的返回值定義頁面導航。 Action的預定義的返回值有: String SUCCESS = "success"; String NONE = "none"; String ERROR = "error"; String INPUT = "input"; String LOGIN = "login"; 比如,當Action返回SUCCESS時希望轉到ok.jsp頁面,則可以這樣寫: <result name="success">ok.jsp</result> 其中,name的缺省為success。 |
源碼下載
我已經將工程代碼打包(lib下沒有導入jar包)