一、通過ActionContext類獲取
public class ActionContextDemo extends ActionSupport {
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?//獲取ActionContext對象
?? ??? ?ActionContext context = ActionContext.getContext();
?? ??? ?//調用getParameters對象獲取參數
?? ??? ?Map<String, Object> map = context.getParameters();
?? ??? ?//遍歷打印map集合
?? ??? ?for (String key : map.keySet()) {
?? ??? ??? ?String[] val = (String[]) map.get(key);
?? ??? ??? ?System.out.println(key + " : " + Arrays.toString(val));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}
二、通過ServletActionContext類獲取request類然后獲取
public class ServletActionContextDemo extends ActionSupport {
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?//獲取request
?? ??? ?HttpServletRequest request = ServletActionContext.getRequest();
?? ??? ?//獲取參數
?? ??? ?String username = request.getParameter("username");
?? ??? ?String password = request.getParameter("password");
?? ??? ?String[] hobbies = request.getParameterValues("hobbies");
?? ??? ?System.out.println(username + " : " + password + " : " + Arrays.toString(hobbies));
?? ??? ?
?? ??? ?//操作域對象
?? ??? ?//request域
?? ??? ?HttpServletRequest request2 = ServletActionContext.getRequest();
?? ??? ?request2.setAttribute("request", "hello request");
?? ??? ?
?? ??? ?//獲取session域
?? ??? ?HttpSession session = request2.getSession();
?? ??? ?session.setAttribute("session", "hello session");
?? ??? ?
?? ??? ?//獲取servletcontext域
?? ??? ?ServletContext servletContext = request2.getServletContext();
?? ??? ?servletContext.setAttribute("servletContext", "application");
?? ??? ?return SUCCESS;
?? ?}
?? ?
}
三、屬性封裝
定義私有的成員變量,變量名稱與表單中name屬性值一致
提供成員變量的get和set方法(實際上,在數據封裝時,僅提供set方法即可。成員變量的屬性名也不一定非得跟name屬性值一致,但set方法跟的字段setXXX中的XXX必須跟name屬性名的首字符大寫一致)
public class DataPackagingAction extends ActionSupport {
?? ?private static final long serialVersionUID = 1L;
?? ?private String username;
?? ?private String password;
?? ?private String[] hobbies;
?? ?public String getUsername() {
?? ??? ?return username;
?? ?}
?? ?public void setUsername(String username) {
?? ??? ?this.username = username;
?? ?}
?? ?public String getPassword() {
?? ??? ?return password;
?? ?}
?? ?public void setPassword(String password) {
?? ??? ?this.password = password;
?? ?}
?? ?public String[] getHobbies() {
?? ??? ?return hobbies;
?? ?}
?? ?public void setHobbies(String[] hobbies) {
?? ??? ?this.hobbies = hobbies;
?? ?}
?? ?
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println("屬性驅動:? " + username + " : " + password + "? " + Arrays.toString(hobbies));
?? ??? ?return NONE;
?? ?}
?? ?
}
四、基于模型驅動的數據封裝方法
1.讓action類實現ModelDriven<T>接口
2.實現ModelDriven<T>接口中的getModel方法
3.在Action中創建私有的成員變量,并手動創建實體類
public class DataPackagingAction2 extends ActionSupport implements ModelDriven<User> {
?? ?private User user = new User();
?? ?@Override
?? ?public User getModel() {
?? ??? ?return user;
?? ?}
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println(user);
?? ??? ?return NONE;
?? ?}
?? ?
}
五、復雜數據的封裝方法
1.封裝數據到list集合中
第一步: 在action中聲明list成員變量,并手動創建實體類;
第二部:? 提供get和set方法;
第三部: 在jsp頁面中,提供基于list作為值得name屬性
?
public class ListAction extends ActionSupport {
?? ?private List<User> list = new ArrayList<User>();
?? ?public List<User> getList() {
?? ??? ?return list;
?? ?}
?? ?public void setList(List<User> list) {
?? ??? ?this.list = list;
?? ?}??
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?for (int i = 0; i < list.size(); i ++) {
?? ??? ??? ?System.out.println(list.get(i));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}
在jsp頁面中name屬性的賦值規則
??? <form action="${pageContext.request.contextPath}/list.action" method="post">
?? ??? ?username: <input type="text" name="list[0].username" /><br/>
?? ??? ?password: <input type="password" name="list[0].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="list[0].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="list[0].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="list[0].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?username: <input type="text" name="list[1].username" /><br/>
?? ??? ?password: <input type="password" name="list[1].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="list[1].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="list[1].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="list[1].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?<input type="submit" value="提交" />
?? ?</form>
2.封裝數據到map集合中
?
第一步: 在action中聲明map成員變量,并手動創建實體類;
?
第二部:? 提供get和set方法;
第三部: 在jsp頁面中,提供基于map作為值得name屬性
public class MapAction extends ActionSupport {
?? ?private Map<String, User> map = new HashMap<String, User>();
?? ?public Map<String, User> getMap() {
?? ??? ?return map;
?? ?}
?? ?public void setMap(Map<String, User> map) {
?? ??? ?this.map = map;
?? ?}
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?for (String key : map.keySet()) {
?? ??? ??? ?System.out.println(key + "? " + map.get(key));
?? ??? ?}
?? ??? ?return NONE;
?? ?}
}
在jsp頁面中name屬性的命名規則
<form action="${pageContext.request.contextPath}/map.action" method="post">
?? ??? ?username: <input type="text" name="map['one'].username" /><br/>
?? ??? ?password: <input type="password" name="map['one'].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="map['one'].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="map['one'].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="map['one'].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?username: <input type="text" name="map['two'].username" /><br/>
?? ??? ?password: <input type="password" name="map['two'].password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="map['two'].hobbies" value="basketball" />basketball
?? ??? ?<input type="checkbox" name="map['two'].hobbies" value="football" />football
?? ??? ?<input type="checkbox" name="map['two'].hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?
?? ??? ?<input type="submit" value="提交" />
?? ?</form>
3.使用屬性封裝數據到對象中
第一步: 在action中聲明實體類User的成員變量,可以不用實例化
第二步: 提供實體類的get和set方法
第三部: jsp中name屬性基于實體類賦值
public class UserAction extends ActionSupport {
?? ?private static final long serialVersionUID = 1L;
?? ?//聲明實體類
?? ?private User user;
?? ?//生成get和set方法
?? ?public User getUser() {
?? ??? ?return user;
?? ?}
?? ?public void setUser(User user) {
?? ??? ?this.user = user;
?? ?}
?? ?//數據打印
?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?System.out.println("~~~~~~" + user);
?? ??? ?return NONE;
?? ?}
}
jsp頁面中name屬性的命名規則
<form action="${pageContext.request.contextPath}/user.action" method="post">
?? ??? ?username: <input type="text" name="user.username" /><br/>
?? ??? ?password: <input type="password" name="user.password" /><br/>
?? ??? ?hobby: <input type="checkbox" name="user.hobbies" value="basketball" />basketball
?? ??? ? <input type="checkbox" name="user.hobbies" value="football" />football
?? ??? ? <input type="checkbox" name="user.hobbies" value="badminton" />badminton
?? ??? ?<hr/>
?? ??? ?<input type="submit" value="提交" />
?? ?</form>
?