Struts2中數據封裝方式

一、通過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>

?

轉載于:https://www.cnblogs.com/rodge-run/p/6441636.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/372868.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/372868.shtml
英文地址,請注明出處:http://en.pswp.cn/news/372868.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

第五章、搭建S3C6410開發板的測試環境

通過對本章的學習&#xff0c;我對s3c6410開發板的測試環境有了一定的認識&#xff0c;并掌握了如下的知識點&#xff1a;一、對于s3c6410這款開發板&#xff0c;它是一款低功耗、高性價比的處理器&#xff0c;它是基于ARM11的內核。二、不同開發板的區別主要在燒錄嵌入式系統的…

IBM JVM調整– gencon GC策略

本文將向您詳細介紹從Java虛擬機&#xff08;例如HotSpot或JRockit&#xff09;遷移到IBM JVM時重要的Java堆空間調整注意事項。 該調整建議基于我為我的一個IT客戶端執行的最新故障排除和調整任務。 IBM JVM概述 正如您可能從其他文章中看到的那樣&#xff0c;IBM JVM在某些方…

mysql主從配置錯誤_mysql主從配置失敗,主從通訊失敗

配置mysql主從的時候&#xff0c;檢查slave狀態&#xff0c;發現報錯信息&#xff0c;Error The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement on query.mysql> show slave status\G*************************** 1. r…

echarts如何顯示在頁面上

echarts如何顯示在頁面上 1.引入echarts的相關.js文件 <script src"js/echarts.min.js"></script> 2.新建一個div&#xff0c;style自己定&#xff0c;但必須要有width和height <div id"history_state" style"width: 400px;height: 20…

懶惰的JSF Primefaces數據表分頁–第2部分

頁面代碼非常簡單&#xff0c;沒有復雜性。 檢查“ index.xhtml”代碼&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www…

二分匹配之最大權值匹配算法---KM模板

百科&#xff1a;http://baike.baidu.com/link?urlvbM3H4XmfrsWfP-epdlR2sVKSNzOq4hXnWDqm5uo8fd7VWsF2SmhDV35XyVUDvVjvrtf42RUITJuNCHn-7_x6K 大神總結&#xff1a;http://www.cnblogs.com/skyming/archive/2012/02/18/2356919.html 代碼&#xff1a; 1 #include<stdio.h…

java實現報表_用存儲過程和 JAVA 寫報表數據源有什么弊端?

用存儲過程和 JAVA 寫報表數據源有什么弊端&#xff1f;跟著小編一起來一看一下吧&#xff01;我們在報表開發中經常會使用存儲過程準備數據&#xff0c;存儲過程支持分步計算&#xff0c;可以實現非常復雜的計算邏輯&#xff0c;為報表開發帶來便利。所以&#xff0c;報表開發…

SpringMVC學習筆記整理

SpringMVC學習筆記 以下是我整理的SpringMVC學習筆記&#xff1a; 導入jar包 一&#xff1a;springmvc工作流程。 ①. servlet容器初始化一個request請求 ②. DispatcherServlet分發器負責發送請求到映射器. ③. despatcherServlet把請求交給處理器映射Mapping&…

Java EE重新審視設計模式:異步

盡管您可能找不到作為設計模式列出的異步方法調用&#xff0c;但我還是值得一提。 因此&#xff0c;這是我的JavaEE Revisits設計模式系列的最后一篇文章。 異步方法調用只不過是多線程。 基本上&#xff0c;它是指將在單獨的線程中運行的方法調用&#xff0c;因此主&#xff0…

am335x watchdog

am335x watchdog 內核文檔kernel/Documentation/watchdog Qtaplex:~/kernel/7109/linux-3.2.0/Documentation/watchdog$ ll total 88 drwxrwxr-x 3 Qt Qt 4096 Jun 8 15:11 ./ drwxrwxr-x 94 Qt Qt 12288 Apr 28 13:09 ../ -rwxrwxr-x 1 Qt Qt 576 Nov 20 2013 00-INDEX -rwxrw…

springboot2 使用hikaridatasource 并測試_基于Spring Boot 2.x的后端管理網站腳手,源碼免費分享...

基于Spring Boot 2.x 的 Material Design 的后端管理網站腳手架 &#xff1a;提供權限認證 用戶管理 菜單管理 操作日志 等常用功能去繁就簡 重新出發基于Spring Boot 集成一些常用的功能&#xff0c;你只需要基于它做些簡單的修改即可。功能列表&#xff1a;權限認證權限管理用…

測試驅動開發–雙贏策略

敏捷從業人員談論測試驅動開發 &#xff08;TDD&#xff09;&#xff0c;所以許多關心代碼質量和可操作性的開發人員也是如此。 我曾幾何時&#xff0c;不久前設法閱讀了有關TDD的文章。 據我了解&#xff0c;TDD的關鍵是&#xff1a; 編寫測試&#xff0c;但失敗 代碼&#x…

設計模式學習(三)——裝飾器模式

前言 距離上一次正兒八經地寫隨筆已經有一段時間了&#xff0c;雖然2月10號有一篇關于泛型的小記&#xff0c;但是其實只是簡單地將自己的學習代碼貼上來&#xff0c;為了方便后續使用時查閱&#xff0c;并沒有多少文字和理解感悟。之所以在今天覺得有必要寫點東西&#xff0c;…

swift - 導航欄設置

話不多&#xff0c;直接貼代碼&#xff1a; let nav UINavigationController.init(rootViewController: viewController) nav.topViewController?.title title// 設置導航欄的標題 nav.navigationBar.tintColor .whiteColor()// 設置push出的導航欄的返回顏色(箭頭及文字) …

mysql5.6主從復制(讀寫分離)方案_MySQL5.6主從復制(讀寫分離)方案

MySQL5.6主從復制(讀寫分離)方案一、前言&#xff1a;為什么MySQL要做主從復制(讀寫分離)&#xff1f;通俗來講&#xff0c;如果對數據庫的讀和寫都在同一個數據庫服務器中操作&#xff0c;業務系統性能會降低。為了提升業務系統性能&#xff0c;優化用戶體驗&#xff0c;可以通…

在實踐中使用延遲隊列

通常&#xff0c;在某些情況下&#xff0c;當您有某種工作或作業隊列時&#xff0c;有必要不立即處理每個工作項或作業&#xff0c;而是要延遲一些時間。 例如&#xff0c;如果用戶單擊一個按鈕來觸發要完成的某項工作&#xff0c;而一秒鐘后&#xff0c;用戶意識到他/她錯了&a…

PCL學習八叉樹

建立空間索引在點云數據處理中有著廣泛的應用&#xff0c;常見的空間索引一般 是自頂而下逐級劃分空間的各種空間索引結構&#xff0c;比較有代表性的包括BSP樹&#xff0c;KD樹&#xff0c;KDB樹&#xff0c;R樹&#xff0c;四叉樹&#xff0c;八叉樹等索引結構&#xff0c;而…

Android實現自定義帶文字和圖片的Button

在Android開發中經常會需要用到帶文字和圖片的button&#xff0c;下面來講解一下常用的實現辦法。 一.用系統自帶的Button實現 最簡單的一種辦法就是利用系統自帶的Button來實現&#xff0c;這種方式代碼量最小。在Button的屬性中有一個是drawableLeft&#xff0c;這個 屬性可以…

mysql語句中的注釋方法_MySQL語句注釋方式簡介

MySQL支持三種注釋方式&#xff1a;1.從‘#字符從行尾。2.從‘-- 序列到行尾。請注意‘-- (雙破折號)注釋風格要求第2個破折號后面至少跟一個空格符(例如空格、tab、換行符等等)。3.從/*序列到后面的*/序列。結束序列不一定在同一行中&#xff0c;因此該語法允許注釋跨越多行。…

aqlserver實用程序_sqlserver命令提示實用工具的介紹

除上述的圖形化管理工具外&#xff0c;SQL Server2008還提供了大量的命令行實用工具&#xff0c;包括bcp、dtexec、dtutil、osql、reconfig、sqlcmd、sqlwb和tablediff等&#xff0c;下面進行簡要說明。dtexec實用工具用于配置和執行SQL Server2008 Intgration Services包。用戶…