Struts2筆記2
一.請求參數封裝
1.屬性驅動:
(1).無實體類情況:屬性和動作類在一起
a.編寫jsp頁面,提交數據,例如name和age
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用戶名:<input type="text" name="name"/><br/> 3 年齡:<input type="text" name="age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.編寫動作類
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 //它要想封裝成功,有如下要求: 表單元素的name屬性取值必須和動作類中的屬性一致(get/set方法後面的部分) 4public class UserAction extends ActionSupport { 5 private String name; 6 private Integer age;// struts2會自動轉為String.表單可以提交的數據類型包括:String/String[]/資源 7 8 public String demo1() { 9 System.out.println("name=" + name + ",age=" + age); 10return this.SUCCESS; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public Integer getAge() { 22 return age; 23 } 24 25 public void setAge(Integer age) { 26 this.age = age; 27 } 28 29 } 30
c.編寫struts.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 導入dtd約束 --> 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(2).有實體類情況
動作類和模型數據分開
* 使用要求:
* 首先動作類中需要提供模型的get/set方法
* 模型中要提供私有成員變量的get/set方法
* 並且這些get/set方法必須和表單的name屬性取值能對應上。
a.編寫jsp--註意OGNL語語
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用戶名:<input type="text" name="user.name"/><br/> 3 年齡:<input type="text" name="user.age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.編寫實體類
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.編寫動作類
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 import huguangqin.com.cnblogs.entity.User; 4 5 //使用要求:首先動作類中需要提供模型的get/set方法; 6 //模型中要提供私有成員變量的get/set方法 7 //並且這些get/set方法必須和表單的name屬性取值能對應上 8 public class UserAction extends ActionSupport { 9 // 提供模型的實例化對象 10 private User user = new User(); 11 12 // 提供get/set方法 13 public User getUser() { 14 return user; 15 } 16 17 public void setUser(User user) { 18 this.user = user; 19 } 20 21 public String demo1() { 22 System.out.println(user); 23 return this.SUCCESS; 24 } 25 26 } 27
d.編寫struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 導入dtd約束 --> 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(3)封裝到list
a.編寫jsp--註意模型屬性取值
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用戶名1:<input type="text" name="users[0].name"/><br/> 3 年齡1:<input type="text" name="users[0].age"/><br/> 4 用戶名2:<input type="text" name="users[1].name"/><br/> 5 年齡2:<input type="text" name="users[1].age"/><br/> 6 用戶名3:<input type="text" name="users[2].name"/><br/> 7 年齡3:<input type="text" name="users[2].age"/><br/> 8 <input type="submit" value="提交"/> 9 </form>
b.編寫實體類
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.編寫動作類
1 package huguangqin.com.cnblogs.action; 2 import java.util.List; 3 import com.opensymphony.xwork2.ActionSupport; 4 import huguangqin.com.cnblogs.entity.User; 5 //使用要求: 首先動作類中需要提供模型的get/set方法; 模型中要提供私有成員變量的get/set方法 6 //並且這些get/set方法必須和表單的name屬性取值能對應上 7 public class UserAction extends ActionSupport { 8 private List<User> users; 9 10 public List<User> getUsers() { 11 return users; 12 } 13 14 public void setUsers(List<User> users) { 15 this.users = users; 16 } 17 18 public String demo1() { 19 System.out.println(users); 20 return this.SUCCESS; 21 } 22 23 } 24
d.編寫Struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 導入dtd約束 --> 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
(4)封裝到map
a.編寫jsp--註意模型屬性
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用戶名1:<input type="text" name="users[‘one‘].name"/><br/> 3 年齡1:<input type="text" name="users[‘one‘].age"/><br/> 4 用戶名2:<input type="text" name="users[‘two‘].name"/><br/> 5 年齡2:<input type="text" name="users[‘two‘].age"/><br/> 6 用戶名3:<input type="text" name="users[‘three‘].name"/><br/> 7 年齡3:<input type="text" name="users[‘three‘].age"/><br/> 8 <input type="submit" value="提交"/> 9 </form>
b.編寫實體類
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.編寫動作類
1 package huguangqin.com.cnblogs.action; 2 import java.util.Map; 3 import com.opensymphony.xwork2.ActionSupport; 4 import huguangqin.com.cnblogs.entity.User; 5 // 使用要求: 首先動作類中需要提供模型的get/set方法; 模型中要提供私有成員變量的get/set方法 6 // 並且這些get/set方法必須和表單的name屬性取值能對應上 7 public class UserAction extends ActionSupport { 8 private Map<String, User> users; 9 10 public Map<String, User> getUsers() { 11 return users; 12 } 13 14 public void setUsers(Map<String, User> users) { 15 this.users = users; 16 } 17 18 public String demo1() { 19 System.out.println(users); 20 return this.SUCCESS; 21 } 22 23 } 24
d.編寫struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 導入dtd約束 --> 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
2.模型驅動
a.編寫jsp
1 <form action="${pageContext.request.contextPath}/demo1.action" method="post"> 2 用戶名:<input type="text" name="name"/><br/> 3 年齡:<input type="text" name="age"/><br/> 4 <input type="submit" value="提交"/> 5 </form>
b.編寫實體類
1 package huguangqin.com.cnblogs.entity; 2 public class User { 3 private String name; 4 private Integer age; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public Integer getAge() { 15 return age; 16 } 17 18 public void setAge(Integer age) { 19 this.age = age; 20 } 21 22 @Override 23 public String toString() { 24 return "User [name=" + name + ", age=" + age + "]"; 25 } 26 27 } 28
c.編寫動作類
1 package huguangqin.com.cnblogs.action; 2 import com.opensymphony.xwork2.ActionSupport; 3 import com.opensymphony.xwork2.ModelDriven; 4 import huguangqin.com.cnblogs.entity.User; 5 6 //* 使用要求: 7 //* 1.動作類必須實現ModelDriven接口,其中泛型提供的是模型對象 8 //* 2.在動作類中需要定義一個模型對象,並且必須由我們自己來實例化 9 //* 3.提供接口中抽象方法的實現,方法的返回值是我們定義的模型對象 10 //* 模型驅動沒有要求生成get/set方法 11 12 public class UserAction extends ActionSupport implements ModelDriven<User> { 13 //實例化模型對象 14 private User user = new User(); 15 16 public String demo1() { 17 System.out.println(user); 18 return this.SUCCESS; 19 } 20 //實現抽象方法,返回模型對象 21 @Override 22 public User getModel() { 23 return user; 24 } 25 26 } 27
d.編寫struts
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 導入dtd約束 --> 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 <struts> 7 <package name="u" extends="struts-default"> 8 <action name="*" class="huguangqin.com.cnblogs.action.UserAction" method="{1}"> 9 <result name="success">/success.jsp</result> 10 </action> 11 </package> 12 </struts>
二.結果視圖的配置
1.result標簽:
name:邏輯視圖的名稱,對應著動作方法的返回值。默認值是success。
type:結果類型,指的就是用什麽方式轉到定義的頁面。默認是dispatcher。
type的常用取值:
dispatcher:(默認值)
使用請求轉發,轉向一個頁面。
redirect:
使用重定向,轉向一個頁面。它可以用於重定向到另外一個動作。
redirectAction:
使用重定向,前往指定的動作。
2.全局結果視圖:<global-results>
1 <package name="myDefault" extends="struts-default" abstract="true"> 2 <global-results> 3 <result name="login">/login.jsp</result> 4 </global-results> 5 </package>
三.訪問Servlet的API的三種方式
1.使用ServletActionContext
1 public class HelloAction extends ActionSupport{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 request = ServletActionContext.getRequest(); 10 response = ServletActionContext.getResponse(); 11 session = request.getSession(); 12 application = ServletActionContext.getServletContext(); 13 14 System.out.println(request); 15 System.out.println(response); 16 System.out.println(session); 17 System.out.println(application); 18 return SUCCESS; 19 } 20 } 21
2.使用ActionContext的方式(ActionContext 是一個map結構的對象)
1 public class Hello2Action extends ActionSupport{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 request = (HttpServletRequest) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletRequest"); 10 response = (HttpServletResponse) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.HttpServletResponse"); 11 session = request.getSession(); 12 application = (ServletContext) ActionContext.getContext().get("com.opensymphony.xwork2.dispatcher.ServletContext"); 13 14 System.out.println(request); 15 System.out.println(response); 16 System.out.println(session); 17 System.out.println(application); 18 return SUCCESS; 19 } 20 } 21
3.通過實現接口的方式
1 public class Hello3Action extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{ 2 3 private HttpServletRequest request; 4 private HttpServletResponse response; 5 private HttpSession session; 6 private ServletContext application; 7 8 public String sayHello(){ 9 10 session = request.getSession(); 11 12 System.out.println(request); 13 System.out.println(response); 14 System.out.println(session); 15 System.out.println(application); 16 return SUCCESS; 17 } 18 19 @Override 20 public void setServletRequest(HttpServletRequest request) { 21 this.request = request; 22 } 23 @Override 24 public void setServletResponse(HttpServletResponse response) { 25 this.response = response; 26 } 27 @Override 28 public void setServletContext(ServletContext application) { 29 this.application = application; 30 } 31 32 33 } 34
Struts2筆記2