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