Struts2學習筆記系列之引數接收
阿新 • • 發佈:2018-12-10
1.前言
俗話說“打鐵要趁熱”,趁著自己的勁頭還未消失,關於struts2又學了一點點(囧),就再寫一篇部落格吧,就當鞏固自己剛學的這一點點知識了。
2.struts2結果跳轉方式
方式1:轉發
<result name="success" type="dispatcher">/jsp/hello.jsp</result>
方式2:轉發到Action
<result name="success" type="chain">
<!-- 轉發到的Action名稱 -->
<param name="actionName">Demo1Action</param>
<!-- 轉發到的Action所在的名稱空間 -->
<param name="namespace">/</param>
</result>
方式3:重定向
<result name="success" type="redirect">/jsp/hello.jsp</result>
方式4:重定向到Action
<result name="success" type="redirectAction" >
<!-- 重定向到的Action名稱 -->
<param name="actionName">Demo1Action</param>
<!-- 重定向到的Action所在的名稱空間 -->
<param name="namespace">/</param>
</result>
3.struts2訪問Servlet api的方式
方式1:通過ActionContext(常用,推薦)
//獲取request域,不推薦使用
Map<String,Object > requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
//ActionContext.getContext()等同於獲取了request,推薦使用
ActionContext.getContext().put("name", "zhangsan");
//獲取session域
Map<String, Object> sessionScope = ActionContext.getContext().getSession();
sessionScope.put("name", "Name");
//獲取Application域
Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
applicationScope.put("name", "lisi");
方式2:通過ServletActionContext
//獲取原生request
HttpServletRequest request = ServletActionContext.getRequest();
//通過request獲取原生session
HttpSession session = request.getSession();
//獲取原生response
HttpServletResponse response = ServletActionContext.getResponse();
//獲取原生ServletContext
ServletContext servletContext = ServletActionContext.getServletContext();
方式3:通過實現介面方式
public class Hello2Action extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
public String execute() throws Exception {
System.out.println("Hello2 Action!");
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
- 介面說明
4.struts2獲得引數
方式1:屬性驅動獲得引數
- jsp頁面程式碼
<form action="${pageContext.request.contextPath}/Hello2Action.action">
使用者名稱:<input type="text" name="name"/><br/>
密碼:<input type="text" name="password"/><br/>
<input type="submit" value="提交">
</form>
- Action類接收引數程式碼
public class Hello2Action extends ActionSupport {
//屬性名必須與前端引數名保持絕對一致,新增getset方法
//有getset方法稱之為屬性,沒有則稱之為變數
private String name;
private String password;
public String execute() throws Exception {
System.out.println("name=" + name + ";pass=" + password);
return SUCCESS;
}
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;
}
}
2.物件驅動
- jsp頁面程式碼
<form action="${pageContext.request.contextPath}/Hello2Action.action">
使用者名稱:<input type="text" name="user.name"/><br/>
密碼:<input type="text" name="user.password"/><br/>
<input type="submit" value="提交">
</form>
- Action類接收引數程式碼
public class Hello2Action extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
}
3.模型驅動
- jsp頁面程式碼
<form action="${pageContext.request.contextPath}/Hello2Action.action">
使用者名稱:<input type="text" name="name"/><br/>
密碼:<input type="text" name="password"/><br/>
<input type="submit" value="提交">
</form>
- Action類程式碼
public class Hello2Action extends ActionSupport implements ModelDriven<User>{
private User user = new User();
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
5.struts2集合型別引數封裝
- jsp頁面程式碼
<form
action="${pageContext.request.contextPath}/Hello2Action.action" method="post">
list:<input type="text" name="list"/><br/>
list:<input type="text" name="list[3]"/><br/>
map:<input type="text" name="map['haha']" /><br>
<input type="submit" value="提交">
</form>
- Action類程式碼
public class Hello2Action extends ActionSupport{
private List<String> list;
private Map<String,String> map;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public String execute() throws Exception {
System.out.println("list:" + list);
System.out.println("map:" + map);
return SUCCESS;
}
}
6.擴充套件
1.strutsMVC:filter(c)、Action(m)、Result(v)
2.Action生命週期:
- 每次請求到來時,都會建立一個新的Action例項
- Action是執行緒安全的.可以使用成員變數接收引數
7.總結
以上就是本人學習的關於struts2引數接收方面個人認為比較重要的知識點了,能力有限,歡迎批評指正,謝謝!