1. 程式人生 > >使用structs2進行ognl進行各種html元素輸入資料

使用structs2進行ognl進行各種html元素輸入資料

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>