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

来源:互联网 发布:数控编程的步骤 编辑:程序博客网 时间:2024/05/29 11:05

action类

[java] view plaincopy
  1. package com.accountkeeper.action;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7.   
  8.   
  9. public class GetParametersAction extends ActionSupport{  
  10.   
  11.       /** 
  12.      * 表单:用户名 
  13.      */   
  14.     private String userName ;   
  15.     /** 
  16.      * 隐藏表单:密码: 
  17.      */   
  18.     private String userPassword;   
  19.     /** 
  20.      * 单选框:性别: 
  21.      */   
  22.     private String sex;   
  23.     /** 
  24.      * 复选框:爱好,用集合来接收数据 
  25.      */   
  26.     private List hobby;   
  27.     /** 
  28.      * 用数组接收复选框的数据 
  29.      */   
  30.     private String hobbyArray[];   
  31.     /** 
  32.      * 下拉框单选:年龄 
  33.      */   
  34.     private String userAge;   
  35.     /** 
  36.      * 下拉框多选:学校: 
  37.      */   
  38.        
  39.     private List college;   
  40.     /** 
  41.      * 版本号 
  42.      */   
  43.     private static final long serialVersionUID = 1L;   
  44.       
  45.     /** 
  46.      * 获取前台所有表单数据 
  47.      * @return 
  48.      */   
  49.     public String execute(){   
  50.            
  51.         System.out.println("文本框:userName:  "+this.getUserName());   
  52.         System.out.println("隐藏文本框:userPassword:  " +this.getUserPassword());   
  53.         System.out.println("单选框:sex:  "+this.getSex());   
  54.         System.out.println("复选框:hobby长度:  "+this.getHobby().size());   
  55.         System.out.print("复选框的值:");   
  56.         /** 
  57.          * 遍历复选框的值 
  58.          */   
  59.         for(int i = 0 ; i <this.getHobby().size();i++){   
  60.                
  61.             System.out.print(" "+this.getHobby().get(i));   
  62.         }   
  63.         System.out.println();   
  64.         System.out.println("获取单选下拉框的值:userAge:"+this.getUserAge());   
  65.         System.out.println();   
  66.         System.out.println("获取多选下拉框的值:college:"+this.getCollege());   
  67.         /** 
  68.          * 遍历多选下拉框的值 
  69.          */   
  70.         for(int i = 0 ;i < this.getCollege().size();i++){   
  71.                
  72.             System.out.print("  " +this.getCollege().get(i));   
  73.         }   
  74.         this.getCheckBox();   
  75.         return SUCCESS;  
  76.      }   
  77.        
  78.     /** 
  79.      * 用数组接受checkbox的数据 
  80.      */   
  81.     public void getCheckBox(){   
  82.            
  83.         System.out.println("用数组接受复选框数据: "+this.getHobbyArray());   
  84.         for(int i = 0 ; i < this.getHobbyArray().length;i++){   
  85.                
  86.             System.out.print(" "+this.getHobbyArray()[i]);   
  87.         }   
  88.     }   
  89.       
  90.     /** 
  91.      * 获取用户名 
  92.      * @return 
  93.      */   
  94.     public String getUserName() {   
  95.         return userName;   
  96.     }   
  97.     /** 
  98.      * 设置用户名 
  99.      * @param userName 
  100.      */   
  101.     public void setUserName(String userName) {   
  102.         this.userName = userName;   
  103.     }   
  104.    
  105.    
  106.     /** 
  107.      * 获取密码 
  108.      * @return 
  109.      */   
  110.     public String getUserPassword() {   
  111.         return userPassword;   
  112.     }   
  113.    
  114.       
  115.     /** 
  116.      * 设置密码 
  117.      * @param userPassword 
  118.      */   
  119.     public void setUserPassword(String userPassword) {   
  120.         this.userPassword = userPassword;   
  121.     }   
  122.    
  123.    
  124.     /** 
  125.      * 获取性别 
  126.      * @return 
  127.      */   
  128.     public String getSex() {   
  129.         return sex;   
  130.     }   
  131.    
  132.    
  133.     /** 
  134.      * 设置性别 
  135.      * @param sex 
  136.      */   
  137.     public void setSex(String sex) {   
  138.         this.sex = sex;   
  139.     }   
  140.       
  141.     /** 
  142.      * 获取兴趣 
  143.      * @return 
  144.      */   
  145.     public List getHobby() {   
  146.         return hobby;   
  147.     }   
  148.    
  149.    
  150.     /** 
  151.      * 设置兴趣 
  152.      * @param hobby 
  153.      */   
  154.     public void setHobby(List hobby) {   
  155.         this.hobby = hobby;   
  156.     }   
  157.    
  158.    
  159.     /** 
  160.      * 获取版本号 
  161.      * @return 
  162.      */   
  163.     public static long getSerialVersionUID() {   
  164.         return serialVersionUID;   
  165.     }   
  166.    
  167.     /** 
  168.      * 获取年龄 
  169.      * @return 
  170.      */   
  171.     public String getUserAge() {   
  172.         return userAge;   
  173.     }   
  174.    
  175.    
  176.     /** 
  177.      *设置年龄 
  178.      * @param userAge 
  179.      */   
  180.     public void setUserAge(String userAge) {   
  181.         this.userAge = userAge;   
  182.     }   
  183.    
  184.    
  185.     /** 
  186.      * 获取多选下拉框的值 
  187.      * @return 
  188.      */   
  189.     public List getCollege() {   
  190.         return college;   
  191.     }   
  192.       
  193.     /** 
  194.      * 设置多选下拉框的值 
  195.      * @param college 
  196.      */   
  197.     public void setCollege(List college) {   
  198.         this.college = college;   
  199.     }   
  200.    
  201.    
  202.     /** 
  203.      * 获取兴趣 
  204.      * @return 
  205.      */   
  206.     public String[] getHobbyArray() {   
  207.         return hobbyArray;   
  208.     }   
  209.    
  210.    
  211.     /** 
  212.      * 设置兴趣 
  213.      * @param hobbyArray 
  214.      */   
  215.     public void setHobbyArray(String[] hobbyArray) {   
  216.         this.hobbyArray = hobbyArray;   
  217.     }   
  218. }  

jsp网页:

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>   
  2. <%   
  3.     String path = request.getContextPath();   
  4.     String basePath = request.getScheme() + "://"   
  5.             + request.getServerName() + ":" + request.getServerPort()   
  6.             + path + "/";   
  7. %>   
  8.    
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  10. <html>   
  11.     <head>   
  12.         <base href="<%=basePath%>">   
  13.    
  14.         <title>获取文本框,下拉框,单选框,复选框的数据</title>   
  15.    
  16.         <meta http-equiv="pragma" content="no-cache">   
  17.         <meta http-equiv="cache-control" content="no-cache">   
  18.         <meta http-equiv="expires" content="0">   
  19.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  20.         <meta http-equiv="description" content="This is my page">   
  21.         <!--  
  22.     <link rel="stylesheet" type="text/css" href="styles.css">  
  23.     -->   
  24.    
  25.     </head>   
  26.    
  27.     <body>   
  28.     <center>   
  29.     <form action="dataExchange" name="getAllParameter">   
  30.     用户名:<input type="text" name="userName" id="userName"><br>   
  31.     隐藏表单:<input type="hidden" name="userPassword" id="userPassword" value="gouchao1025126"><br>   
  32.     <h5>单选框</h5><br>   
  33.         性别:   
  34.         <input type="radio" name="sex" value="male"> 男   
  35. <input type="radio" name="sex" value="female"> 女   
  36.    
  37.         <br />   
  38.         <h5>复选框</h5><br>   
  39.         兴趣:   
  40.         <input type="checkbox" value="1" name="hobby" />   
  41.         篮球   
  42.         <input type="checkbox" value="2" name="hobby" />   
  43.         足球   
  44.         <input type="checkbox" value="3" name="hobby" />   
  45.         乒乓球   
  46.         <br />   
  47.         <h5>复选框(后台用数组来接受数据)</h5><br>   
  48.         兴趣:   
  49.         <input type="checkbox" value="1" name="hobbyArray" />   
  50.         篮球   
  51.         <input type="checkbox" value="2" name="hobbyArray" />   
  52.         足球   
  53.         <input type="checkbox" value="3" name="hobbyArray" />   
  54.         乒乓球   
  55.         <br />hobbyArray   
  56.         <h4>下拉框单选</h4><br>   
  57.         年龄   
  58.         <select name="userAge" id="userAge">   
  59.             <option name="age" value="1">   
  60.                 1   
  61.             </option>   
  62.             <option name="age" value="2">   
  63.                 2   
  64.             </option>   
  65.             <option name="age" value="3">   
  66.                 3   
  67.             </option>   
  68.         </select>   
  69.            
  70.         <br />   
  71.         <h4>下拉框多选</h4><br>   
  72.         学校   
  73.         <select name="college" id="college" size="4" multiple="multiple">   
  74.             <option name="collegeName" value="1">   
  75.                 广技师   
  76.             </option>   
  77.             <option name="collegeName" value="2">   
  78.                 中大   
  79.             </option>   
  80.             <option name="collegeName" value="3">   
  81.                 华师   
  82.             </option>   
  83.         </select>   
  84.         <input type="submit" value="提交">   
  85.         </form>   
  86.         </center>   
  87.     </body>   
  88. </html>   
  89.    

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

[html] view plaincopy
  1. <action name="dataExchange" class="GetParametersAction">  
0 0
原创粉丝点击