1.Action訪問ServletAPI
1.1 ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
1.2 實現ServletRequestAware 接口
原因: 在執行動作之前有攔截器給你注入這些實例 interceptor — servletcofig
2 結果視圖 Result
2.1 result元素的配置
屬性:name:指定配置邏輯視圖名,對應着動作方法的返回值。Name默認值:success。
type:到達目標的形式,默認值爲dispatcher 轉發
2.2 Struts2 提供的結果集類型
1、chain:用戶轉發到另一個動作
2、dispatcher:轉發到tsp頁面
3、redirect:用戶重定向到另外一個jsp頁面
4、redirectAction:用戶重定向到另外一個action
5、stream:用於想瀏覽器返回一個Inputstream的結果類型。用戶文件上傳下載。
6、plainText:用於顯示某個頁面的原始代碼的結果類型
2 .數據封裝
作爲 MVC 框架, 必須要負責解析Http請求參數, 並將其封裝到Model 對象中Struts2 提供了非常強大的類型轉換機制用於請求數據封裝到model對象.
2.1 靜態參數封裝(瞭解)
<package name="staticparam" extends="struts-default">
<action name="staticParam" class="StaticParamAction的路徑" method="login">
<param name="name">隔壁老王
</param>
<param name="password">222222</param>
</action>
</package>
//action中要寫set/get方法
//底層依靠 staticParams 攔截器, 將靜態參數放入值棧中,而action就在值棧中的棧頂, 自然就會找到該action中的對應屬性,然後進行賦值.
2.2 動態參數封裝
2.2.1 屬性驅動
2.2.1.1 普通屬性驅動,提供get/set方法
Action:
public class UserAction extends ActionSupport{
private String name;
private String password;
public String login(){
System.out.println("u:"+name+"p:"+password);
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
struts.xml:
<package name="param" extends="struts-default">
<action name="login1" class="UserAction的路徑" method="login">
</action>
</package>
jsp:
<form action="${pageContext.request.contextPath}/login1.action">
<label>姓名:</label>
<input type="text" name="name">
<br>
<label>密碼:</label>
<input type="password" name="password">
<br>
<input type="submit" value="提交">
</form>
//跟靜態參數封裝一樣,只不過這裏獲取的是表單中的參數,也就是請求發送過來的數 據。依靠的攔截器爲params. 其中該攔截器做的事有兩件,一是對提交的參數進行數據 校驗,判斷是否合法,判斷是否合法的標準就是攔截器中的excludeParams參數的正則 表達式的值。二是將其封裝到值棧中的棧頂元素中去,而當前action就在棧頂,所以能 夠將參數放入action中。
2.2.1.2 ognl表達式來封裝數據
Action:
public class PersonAction extends ActionSupport {
private Person person;
//person中有 name,password 及相應的get/set方法
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String login(){
System.out.println(person);
return null;
}
}
jsp:
<form action="${pageContext.request.contextPath}/login2.action">
<label>姓名:</label>
<input type="text" name="person.name">
//利用 ognl 表達式
<br>
<label>密碼:</label>
<input type="password" name="person.password">
<br>
<input type="submit" value="提交">
</form>
//在jsp頁面中的 person.username 和person.password 其實就是ognl表達式,代表着往根 (root,值棧valueStack)中存放值,而值棧中的棧頂元素也就是爲當前action,我們在 action中設置person的get、set屬性,即可以讓存進來的值匹配到,進而將對應屬性賦值成功
2.2.2模型驅動
Action:
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private Customer customer=new Customer();
//Customer 是一個普通的javaBean 裏面有 name,password對應的get/set方法
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String login(){
System.out.println(map.toString());
return ERROR;
}
@Override
public Customer getModel() {
//需要實例化
return customer;
}
}
jsp:<form action="${pageContext.request.contextPath}/login3.action">
<label>姓名:</label>
<input type="text" name="name">
<label>密碼:</label>
<input type="password" name="password">
<br>
<input type="submit" value="提交">
</form>
//必須實現 ModelDriven 接口,提供一個方法getModel(),初始化對象實例。 modelDriven 攔截器將getModel方法返回的結果壓入值棧,而我們的表單參數會從值棧中從上往下進行查找,自然就直接將參數封裝到對象中了。