注:黃色字體爲網絡引用內容
一、Action和ActionSupport的區別
Actionsupport這個工具類在實現了Action接口的基礎上還定義了一個validate()方法,重寫該方法,它會在execute()方法之前執行,如校驗失敗,會轉入input處,必須在配置該Action時配置input屬性。
另外,Actionsupport還提供了一個getText(String key)方法還實現國際化,該方法從資源文件上獲取國際化信息.
這樣在自定義標籤時可以定義一個變量爲new actionsupport對象實現國際化。
二、Struts.xml的基礎配置
1、package:
name:規定了package的名字,沒有實質性的作用,但可以有效的區分代碼模塊,增加開發效率
namespace:規定了此package的命名空間,即在訪問此包Action的時候需要在Action路徑前加上命名空間namespace
extends:規定了此包繼承哪個包。(有待學習)
abstract:用來定義包爲抽象的,也就是不能包含Action的定義,但是抽象包可以被其他包繼承,因此裏面可以定義其他包需要的元素,比如ResultType、 Interceptor等等。struts-default包,它就是個抽象的包,
2、 Action:
name:規定了此Action的名字以及訪問路徑。
class:規定了Action所訪問的操作類
method:規定了此Action訪問操作類的哪個方法,默認爲excute方法
3、 result:
name:規定了此Action的返回值名字
xml-value:規定了此result的返回到哪個頁面 如result.jsp 在前面加上”${pageContext.request.contextPath}/” 是絕對路徑。
三、Action的javaBean匹配
1、單匹配
Action需要繼承implements ModelDriven<T> 前端頁面的參數名直接寫Bean的成員屬性 在接口的public User getModel() 裏 return bean
注意: 此方法需要在Action實例化Bean 如Private User user = new User(); 但無需寫Get和Set方法
2、多匹配
Action需要聲明匹配的Bean 如 private User user 再加入Get和Set方法。 前端頁面的參數名前需要加上Bean名稱 如:User.username/User.userpassowrd
四、Action的集合匹配(有待進一步學習)
1、String數組:聲明:Sting[] arr; 再加入Get和Set方法,前臺頁面如下:
<form action="hobby" method="post">
愛好:
<input type="checkbox" name="hobby" value="唱歌"/>唱歌
<input type="checkbox" name="hobby" value="跳舞"/>跳舞
<input type="checkbox" name="hobby" value="踢球"/>踢球
<input type="checkbox" name="hobby" value="睡覺"/>睡覺
<input type="checkbox" name="hobby" value="玩遊戲"/>玩遊戲
<input type="submit" value="提交"/>
</form>
操作方法裏如下
for(String h:hobby){
System.out.println(h);
}
2、List集合:聲明 List<Student> students; 再加入Get和Set方法 前臺頁面如下:
<form action="student" method="post">
<table>
<tr>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
</tr>
<tr>
<td><input type="text" name="students[0].name"/></td>
<td><input type="text" name="students[0].sex"/></td>
<td><input type="text" name="students[0].age"/></td>
</tr>
<tr>
<td><input type="text" name="students[1].name"/></td>
<td><input type="text" name="students[1].sex"/></td>
<td><input type="text" name="students[1].age"/></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
操作方法裏如下
for(Student s:students){
System.out.println(s.toString());
}