struts2.0获取各种表单的数据
生活随笔
收集整理的這篇文章主要介紹了
struts2.0获取各种表单的数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://www.2cto.com/kf/201107/97883.html
后臺代碼:
1 import java.util.List; 2 import com.opensymphony.xwork2.ActionSupport; 3 /** 4 * struts2.0獲取各種表單的數據 5 * 獲取下拉框的值,和復選框的值可以用一個數組或者集合來保存,變量名要和表單的name屬性值一致 6 * @author 夠潮 7 * 8 */ 9 @SuppressWarnings("unchecked") 10 public class GetParametersAction extends ActionSupport { 11 12 /** 13 * 表單:用戶名 14 */ 15 private String userName ; 16 /** 17 * 隱藏表單:密碼: 18 */ 19 private String userPassword; 20 /** 21 * 單選框:性別: 22 */ 23 private String sex; 24 /** 25 * 復選框:愛好,用集合來接收數據 26 */ 27 private List hobby; 28 /** 29 * 用數組接收復選框的數據 30 */ 31 private String hobbyArray[]; 32 /** 33 * 下拉框單選:年齡 34 */ 35 private String userAge; 36 /** 37 * 下拉框多選:學校: 38 */ 39 40 private List college; 41 /** 42 * 版本號 43 */ 44 private static final long serialVersionUID = 1L; 45 46 47 /** 48 * 獲取前臺所有表單數據 49 * @return 50 */ 51 public void getAllParametersAction(){ 52 53 System.out.println("文本框:userName: "+this.getUserName()); 54 System.out.println("隱藏文本框:userPassword: " +this.getUserPassword()); 55 System.out.println("單選框:sex: "+this.getSex()); 56 System.out.println("復選框:hobby長度: "+this.getHobby().size()); 57 System.out.print("復選框的值:"); 58 /** 59 * 遍歷復選框的值 60 */ 61 for(int i = 0 ; i <this.getHobby().size();i++){ 62 63 System.out.print(" "+this.getHobby().get(i)); 64 } 65 System.out.println(); 66 System.out.println("獲取單選下拉框的值:userAge:"+this.getUserAge()); 67 System.out.println(); 68 System.out.println("獲取多選下拉框的值:college:"+this.getCollege()); 69 /** 70 * 遍歷多選下拉框的值 71 */ 72 for(int i = 0 ;i < this.getCollege().size();i++){ 73 74 System.out.print(" " +this.getCollege().get(i)); 75 } 76 this.getCheckBox(); 77 } 78 79 /** 80 * 用數組接受checkbox的數據 81 */ 82 public void getCheckBox(){ 83 84 System.out.println("用數組接受復選框數據: "+this.getHobbyArray()); 85 for(int i = 0 ; i < this.getHobbyArray().length;i++){ 86 87 System.out.print(" "+this.getHobbyArray()[i]); 88 } 89 } 90 91 92 /** 93 * 獲取用戶名 94 * @return 95 */ 96 public String getUserName() { 97 return userName; 98 } 99 100 101 /** 102 * 設置用戶名 103 * @param userName 104 */ 105 public void setUserName(String userName) { 106 this.userName = userName; 107 } 108 109 110 /** 111 * 獲取密碼 112 * @return 113 */ 114 public String getUserPassword() { 115 return userPassword; 116 } 117 118 119 /** 120 * 設置密碼 121 * @param userPassword 122 */ 123 public void setUserPassword(String userPassword) { 124 this.userPassword = userPassword; 125 } 126 127 128 /** 129 * 獲取性別 130 * @return 131 */ 132 public String getSex() { 133 return sex; 134 } 135 136 137 /** 138 * 設置性別 139 * @param sex 140 */ 141 public void setSex(String sex) { 142 this.sex = sex; 143 } 144 145 146 /** 147 * 獲取興趣 148 * @return 149 */ 150 public List getHobby() { 151 return hobby; 152 } 153 154 155 /** 156 * 設置興趣 157 * @param hobby 158 */ 159 public void setHobby(List hobby) { 160 this.hobby = hobby; 161 } 162 163 164 /** 165 * 獲取版本號 166 * @return 167 */ 168 public static long getSerialVersionUID() { 169 return serialVersionUID; 170 } 171 172 173 /** 174 * 獲取年齡 175 * @return 176 */ 177 public String getUserAge() { 178 return userAge; 179 } 180 181 182 /** 183 *設置年齡 184 * @param userAge 185 */ 186 public void setUserAge(String userAge) { 187 this.userAge = userAge; 188 } 189 190 191 /** 192 * 獲取多選下拉框的值 193 * @return 194 */ 195 public List getCollege() { 196 return college; 197 } 198 199 200 /** 201 * 設置多選下拉框的值 202 * @param college 203 */ 204 public void setCollege(List college) { 205 this.college = college; 206 } 207 208 209 /** 210 * 獲取興趣 211 * @return 212 */ 213 public String[] getHobbyArray() { 214 return hobbyArray; 215 } 216 217 218 /** 219 * 設置興趣 220 * @param hobbyArray 221 */ 222 public void setHobbyArray(String[] hobbyArray) { 223 this.hobbyArray = hobbyArray; 224 } 225 226 }xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="admin" namespace="/" extends="struts-default"> <!-- getParametersAction --> <action name="getParameters" class="action.GetParametersAction"> </action> </package> </struts>前臺代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() +"://" + request.getServerName() +":"+ request.getServerPort() + path +"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>獲取文本框,下拉框,單選框,復選框的數據</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <form action="getParameters!getAllParametersAction.action" name="getAllParameter"> 用戶名:<input type="text" name="userName" id="userName"><br> 隱藏表單:<input type="hidden" name="userPassword" id="userPassword" value="gouchao1025126"><br> <h5>單選框</h5><br> 性別: <input type="radio" name="sex" value="male"> 男 <input type="radio" name="sex" value="female"> 女 <br /> <h5>復選框</h5><br> 興趣: <input type="checkbox" value="1" name="hobby"/> 籃球 <input type="checkbox" value="2" name="hobby"/> 足球 <input type="checkbox" value="3" name="hobby"/> 乒乓球 <br /> <h5>復選框(后臺用數組來接受數據)</h5><br> 興趣: <input type="checkbox" value="1" name="hobbyArray"/> 籃球 <input type="checkbox" value="2" name="hobbyArray"/> 足球 <input type="checkbox" value="3" name="hobbyArray"/> 乒乓球 <br />hobbyArray <h4>下拉框單選</h4><br> 年齡 <select name="userAge" id="userAge"> <option name="age" value="1"> 1 </option> <option name="age" value="2"> 2 </option> <option name="age" value="3"> 3 </option> </select> <br /> <h4>下拉框多選</h4><br> 學校 <select name="college" id="college" size="4" multiple="multiple"> <option name="collegeName" value="1"> 廣技師 </option> <option name="collegeName" value="2"> 中大 </option> <option name="collegeName" value="3"> 華師 </option> </select> <input type="submit" value="提交"> </form> </center> </body> </html> 測試效果:
總結
以上是生活随笔為你收集整理的struts2.0获取各种表单的数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2中Action访问Serv
- 下一篇: Struts2中表单与Action传递数