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

后台代码:

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

 

测试效果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值