後臺代碼:
Java代碼
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
* struts2.0獲取各種表單的數據
* 獲取下拉框的值,和復選框的值可以用一個數組或者集合來保存,變量名要和表單的name屬性值一致
* @author 夠潮
*
*/
@SuppressWarnings("unchecked")
public class GetParametersAction extends ActionSupport {
/**
* 表單:用戶名
*/
private String userName ;
/**
* 隱藏表單:密碼:
*/
private String userPassword;
/**
* 單選框:性別:
*/
private String sex;
/**
* 復選框:愛好,用集合來接收數據
*/
private List hobby;
/**
* 用數組接收復選框的數據
*/
private String hobbyArray[];
/**
* 下拉框單選:年齡
*/
private String userAge;
/**
* 下拉框多選:學校:
*/
private List college;
/**
* 版本號
*/
private static final long serialVersionUID = 1L;
/**
* 獲取前臺所有表單數據
* @return
*/
public void getAllParametersAction(){
System.out.println("文本框:userName: "+this.getUserName());
System.out.println("隱藏文本框:userPassword: " +this.getUserPassword());
System.out.println("單選框:sex: "+this.getSex());
System.out.println("復選框:hobby長度: "+this.getHobby().size());
System.out.print("復選框的值:");
/**
* 遍歷復選框的值
*/
for(int i = 0 ; i <this.getHobby().size();i++){
System.out.print(" "+this.getHobby().get(i));
}
System.out.println();
System.out.println("獲取單選下拉框的值:userAge:"+this.getUserAge());
System.out.println();
System.out.println("獲取多選下拉框的值:college:"+this.getCollege());
/**
* 遍歷多選下拉框的值
*/
for(int i = 0 ;i < this.getCollege().size();i++){
System.out.print(" " +this.getCollege().get(i));
}
this.getCheckBox();
}
/**
* 用數組接受checkbox的數據
*/
public void getCheckBox(){
System.out.println("用數組接受復選框數據: "+this.getHobbyArray());
for(int i = 0 ; i < this.getHobbyArray().length;i++){
System.out.print(" "+this.getHobbyArray()[i]);
}
}
/**
* 獲取用戶名
* @return
*/
public String getUserName() {
return userName;
}
/**
* 設置用戶名
* @param userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 獲取密碼
* @return
*/
public String getUserPassword() {
return userPassword;
}
/**
* 設置密碼
* @param userPassword
*/
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
/**
* 獲取性別
* @return
*/
public String getSex() {
return sex;
}
/**
* 設置性別
* @param sex
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* 獲取興趣
* @return
*/
public List getHobby() {
return hobby;
}
/**
* 設置興趣
* @param hobby
*/
public void setHobby(List hobby) {
this.hobby = hobby;
}
/**
* 獲取版本號
* @return
*/
public static long getSerialVersionUID() {
return serialVersionUID;
}
/**
* 獲取年齡
* @return
*/
public String getUserAge() {
return userAge;
}
/**
*設置年齡
* @param userAge
*/
public void setUserAge(String userAge) {
this.userAge = userAge;
}
/**
* 獲取多選下拉框的值
* @return
*/
public List getCollege() {
return college;
}
/**
* 設置多選下拉框的值
* @param college
*/
public void setCollege(List college) {
this.college = college;
}
/**
* 獲取興趣
* @return
*/
public String[] getHobbyArray() {
return hobbyArray;
}
/**
* 設置興趣
* @param hobbyArray
*/
public void setHobbyArray(String[] hobbyArray) {
this.hobbyArray = hobbyArray;
}
}
配置文件:
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>
前臺代碼:
Java代碼
<%@ 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>
測試效果: