Struts2小結
一、struts1 和struts2的區別
struts2每次訪問一個action時都會new一個新的對象出來,不會出現線程同步問題
struts1每次初始化時會將action new出來,相同請求會調用同一個action,會出現線程同步問題
二、配置相應環境
a) 下載struts2 相應jar包
b) 解壓此文件,將struts-2.1.8.1-all/struts-2.1.8.1/apps/struts2-blank-2.1.8.1中的文件解壓,注意app文件夾下的文件是struts的實例文件。
c) 將解壓的文件下的web-Inf/classes下的struts.xml copy到web項目的src下
d) 將類庫文件copy到lib目錄下 jar文件在解壓的文件夾的lib目錄下注意junit.jar
和spring-test-2.5.6.jar不需copy 到lib目錄下。
注意將commons-logging.jar copy到lib 下 有的struts版本此實例中沒後此jar包需從其他實例中copy入
e) 將實例中的web.xml 的有關struts2的fitler配置信息拷入
<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>
f) 如果是在開發階段建議在stuts.xml 中設置開發模式,這樣配置文件的改動會及時更新
<constant name="struts.devMode" value="true" />
g) 添加源碼
在相應jar包上右鍵Property 添加相應路徑
核心包的路徑 /struts-2.1.8.1-all/struts-2.1.8.1/src/core/src/main/java
h) 添加doc 文檔 右鍵相應jar包 添加相應路徑
核心包doc 文檔路徑:/struts-2.1.8.1-all/struts-2.1.8.1/docs/struts2-core/apidocs/
i) 添加struts.xml的提示
在window –preferences中搜索catalog 添加 選擇 uri 名稱寫stuts.xml 中的地址
如http://struts.apache.org/dtds/struts-2.0.dtd 然後將location 中填入core核心包中的相應DTD文件
j) 更改jsp文件的默認編碼
Window –preferences 搜索jsp 將encoding改爲 Chinese
三、hello struts 小程序
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/hello.jsp
</result>
</action>
</package>
解析 namespace 代表訪問時的命名空間只有在此命名空間下才能調用其下的action
<action>中的name屬性代表訪問路徑 result代表返回的頁面
訪問此action:
http://localhost:8080/項目名/action名
四、namespace
namespace 必須用/開頭 訪問對應的action時要加上相應的namespace名稱
若namespace爲空時在任何路徑都可訪問,可解決特殊問題
五、result
沒有名稱時就加代表success
六、自定義action
Struts2 可以將任何一個類的名稱爲execute 返回值爲String 的方法當做action
即一個普通java類 便於測試。
若在action中沒有寫class 則action會自動執行默認的action類。
Action類的定義方法
1、 隨便定義一個java普通類
定義返回值爲String 方法名爲execute的方法。
2、 定義一個類實現Action接口。
3、 定義一個類繼承ActionSupport 。
實際開發中採用第三種方法因爲它爲我們提供了一些工具方法,方便調用。
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="actions.Test1">
<result name="success">
/hello.jsp
</result>
</action>
</package>
類的代碼:
package actions;
import com.opensymphony.xwork2.ActionSupport;
public class Test1 extends ActionSupport{
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("hello");
return "success";
}
}
七、相對路徑問題
在jsp界面中加入
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
在<head></head>中加入
<base href="<%=basePath%>">
這樣所有的鏈接會自動定位到項目根路徑
Stuts2.0中的所有的鏈接界面對應的路徑爲映射路徑而不是實際路徑,所以在struts2.0中應儘量使用絕對路徑
八、Action執行的方法
Action執行的時候並不一定要執行execute方法
可以在配置文件中配置Action的時候用method 屬性來指定執行哪個方法
也可以在調用時使用同一個action然後加!號後加方法名
<a href="user/user!add">添加用戶</a>
一般使用後一種方法避免出現過多的action
九、通配符問題
使用通配符可以最少化代碼量
<package name="default1" namespace="/path2" extends="struts-default">
<action name="*_*" class="actions2.{1}" method="{2}">
<result name="success">/{1}_{2}.jsp</result>
</action>
</package>
可以根據統配符的內容自動找到相關類相關方法
注意 通配符的佔位符是從1開始的
十、得到參數值
在調用的Action中定義與參數名相同的String類型的參數,添加get set方
即可得到相應的參數值。
十一、通過域模型DomainMode得到參數
即寫一個pojo類 此類中含有相應的參數值,在傳值時採用對象.屬性名傳參,接受action類只需寫pojo類的get set 方法即可。
十二、ModelDriven接受參數
在傳參時寫字段名稱=值,在接收的Action類實現ModelDriven接口實現其getModel方法,返回值爲接受的pojo對象,即可得到封裝好的pojo對象。
例:
傳參:?name=aa&age=12"
接受類:implements ModelDriven<pojo.Student>
實現方法: public pojo.Student getModel() {
return stu;
}
返回值爲new 好的stu對象
pojo.Student stu=new pojo.Student();
十三、中文亂碼問題
在stucts的配置文件中加入:
<constant name="struts.i18n.encoding" value="GBK" />
國際化配置
有些版本設置後還會出現亂碼問題,應自己手寫一個filter來解決
在2.1.7版本後會解決
或將web.xml的Stuts配置換成2.0的 配置
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
十四、 簡單數據校驗
1、 this.addFieldError("name", "name is error");
在action中放入錯誤信息
2、 在前臺得到錯誤信息
a) 添加struts標籤
<%@taglib uri="/struts-tags" prefix="s" %>
b) 顯示帶樣式的錯誤信息
<s:fielderror fieldName="name" theme="simple"/>
Theme 代表主題名稱也就是系統定義的樣式名稱。
c) 僅得到錯誤信息
<s:property value="errors.name[0]"/>
從值棧中取出errors的值errors存儲了所有錯誤信息
取name這一錯誤信息的第一條錯誤信息。
errors.name[0]
d) 顯示調試信息
<s:debug></s:debug>
會出現debug鏈接 點擊鏈接出現調試信息。