现在的位置: 首页 > 综合 > 正文

使用structs2进行ognl进行各种html元素输入数据

2014年02月08日 ⁄ 综合 ⁄ 共 4332字 ⁄ 字号 评论关闭

action类

package com.accountkeeper.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;



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 String execute(){ 
         
        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(); 
        return SUCCESS;
     } 
     
    /**
     * 用数组接受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; 
    } 
}

jsp网页:

<%@ 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="dataExchange" 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> 
 

然后只要在struts.xml中添加一个action就可以了

<action name="dataExchange" class="GetParametersAction">
</action>



抱歉!评论已关闭.