struts2.0获取各种表单的数据

转自:http://www.2cto.com/kf/201107/97883.html

后台代码:

View Code
  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>

测试效果:
测试效果

 

 

posted @ 2011-11-14 18:04  爱上飞飞的面码  阅读(3655)  评论(0)    收藏  举报