• ongl(示例3-6 多值类型的数据处理)


    ongl(示例3-6 多值类型的数据处理)

    使用ognl 实现多值处理,处理的值比较多,如数组,集合等类型的数据

    //实体类省略

    //创建action,其中包括两个成员变量 分别是两个double,string类型的数组

    复制代码
    package cn.jbit.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class ArraysDataTransferAction extends ActionSupport {
        private String[] hobbies;
        private Double[] numbers=new Double[3];
        
        public String execute(){
            System.out.println("hobbies的长度是"+hobbies.length);
            for(int i=0;i<hobbies.length;i++){
                System.out.println("hobbies数组中第"+i+"个元素是:"+hobbies[i]);
            }
            System.out.println("numbers的长度是"+numbers.length);
            for(int i=0;i<numbers.length;i++){
                System.out.println("numbers数组中第"+i+"个元素是:"+numbers[i]);
            }
            
            return SUCCESS;
        }
        public String[] getHobbies() {
            return hobbies;
        }
        public void setHobbies(String[] hobbies) {
            this.hobbies = hobbies;
        }
        public Double[] getNumbers() {
            return numbers;
        }
        public void setNumbers(Double[] numbers) {
            this.numbers = numbers;
        }
    }
    复制代码

    //再次创建另外一个action,ListDataTransferAction

    复制代码
    package cn.jbit.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import cn.jbit.bean.*;
    import java.util.*;
    
    public class ListDataTransferAction extends ActionSupport {
        private List<String> hobbies;
        private List<Double> numbers;
        private List<User> users;
        
        public String execute(){
            System.out.println("hobbies的长度是"+hobbies.size());
            for(int i=0;i<hobbies.size();i++){
                System.out.println("hobbies数组中第"+i+"个元素是:"+hobbies.get(i));
            }
            System.out.println("numbers的长度是"+numbers.size());
            for(int i=0;i<numbers.size();i++){
                System.out.println("numbers数组中第"+i+"个元素是:"+numbers.get(i));
            }
            System.out.println("users的长度是"+users.size());
            for(int i=0;i<users.size();i++){
                System.out.println("users数组中第"+i+"个元素是:"+users.get(i).getName());
            }
            return SUCCESS;
        }
        public List<String> getHobbies() {
            return hobbies;
        }
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
        public List<Double> getNumbers() {
            return numbers;
        }
        public void setNumbers(List<Double> numbers) {
            this.numbers = numbers;
        }
        public List<User> getUsers() {
            return users;
        }
        public void setUsers(List<User> users) {
            this.users = users;
        }
    
    }
    复制代码
    复制代码
    package cn.jbit.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import cn.jbit.bean.*;
    import java.util.*;
    
    public class ListDataTransferAction extends ActionSupport {
        private List<String> hobbies;
        private List<Double> numbers;
        private List<User> users;
        
        public String execute(){
            System.out.println("hobbies的长度是"+hobbies.size());
            for(int i=0;i<hobbies.size();i++){
                System.out.println("hobbies数组中第"+i+"个元素是:"+hobbies.get(i));
            }
            System.out.println("numbers的长度是"+numbers.size());
            for(int i=0;i<numbers.size();i++){
                System.out.println("numbers数组中第"+i+"个元素是:"+numbers.get(i));
            }
            System.out.println("users的长度是"+users.size());
            for(int i=0;i<users.size();i++){
                System.out.println("users数组中第"+i+"个元素是:"+users.get(i).getName());
            }
            return SUCCESS;
        }
        public List<String> getHobbies() {
            return hobbies;
        }
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
        public List<Double> getNumbers() {
            return numbers;
        }
        public void setNumbers(List<Double> numbers) {
            this.numbers = numbers;
        }
        public List<User> getUsers() {
            return users;
        }
        public void setUsers(List<User> users) {
            this.users = users;
        }
    
    }
    复制代码

    struts.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>
        <!-- <constant name="struts.custom.i18n.resources" value="message"/>     -->
        <package name="default" extends="struts-default">    
            <action name="ArraysDataTransfer" class="cn.jbit.action.ArraysDataTransferAction">
                <result name="success">result2.jsp</result>
            </action>
            <action name="listDataTransfer" class="cn.jbit.action.ListDataTransferAction">
                <result>result2.jsp</result>
            </action>
            
    
        </package>
    </struts>
    复制代码

    首先需要做的是接受数据,创建出表单数据;

    array_input.jsp和list_input.jsp

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <%
    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>My JSP 'index.jsp' starting page</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>
        <s:form action="ArraysDataTransfer.action">
            <s:textfield name="hobbies" label="爱好:"/>
            <s:textfield name="hobbies" label="爱好:"/>
            <s:textfield name="hobbies" label="爱好:"/>
            
            <s:textfield name="numbers[0]" label="数字:"/>
            <s:textfield name="numbers[1]" label="数字:"/>
            <s:textfield name="numbers[2]" label="数字:"/>
            <s:submit value="提交"/>
        </s:form>
      </body>
    </html>
    复制代码
    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <%
    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>My JSP 'index.jsp' starting page</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>
        <s:form action="listDataTransfer">
            <s:textfield name="hobbies" label="爱好:"/>
            <s:textfield name="hobbies" label="爱好:"/>
            <s:textfield name="hobbies" label="爱好:"/>
            
            <s:textfield name="numbers[0]" label="数字:"/>
            <s:textfield name="numbers[1]" label="数字:"/>
            <s:textfield name="numbers[2]" label="数字:"/>
            
            <s:textfield name="users.name" label="姓名:"/>
            <s:textfield name="users.name" label="姓名:"/>
            <s:textfield name="users.name" label="姓名:"/>
            <s:submit value="提交"/>
        </s:form>
      </body>
    </html>
    复制代码

    之后分别访问http://localhost:8080/Struts2_OGNL/ArraysDataTransfer和http://localhost:8080/Struts2_OGNL/listDataTransfer

  • 相关阅读:
    python排序算法的实现-插入
    python排序算法的实现-选择
    python排序算法的实现-冒泡
    python数据结构之图深度优先和广度优先
    python数据结构之图的实现
    python数据结构之二叉树遍历的实现
    python数据结构之二叉树的实现
    python数据结构之栈、队列的实现
    让淘宝链接在微信中分享,GO
    解决git 不同branch 下node_moudes不同步的问题
  • 原文地址:https://www.cnblogs.com/lianceng/p/5959188.html
Copyright © 2020-2023  润新知