struts2里的List类型的属性 赋值时系统是用的arraylist赋值还...

页面导航:
→ 正文内容 ajax传递json
ajax+json+Struts2实现list传递实例讲解
应付学习需要,需要通过ajax来获取后台的List集合里面的值,特做了一个实例并附上演示效果,希望本例对你有所帮助
由于实习需要,需要通过ajax来获取后台的List集合里面的值。由于前面没有接触过,所以今天就来研究下了。 一、首先需要下载JSON依赖的jar包。它主要是依赖如下: json-lib-2.2.2-jdk15 ezmorph-1.0.4 commons-logging-1.0.4 commons-lang-2.4 commons-collections-3.2.1 commons-beanutils 二、实例。 1、身份证错误信息Bean类(ErrorCondition.java)
代码如下: /** *@Project: excel *@Author: chenssy *@Date:
*@Copyright: chenssy All rights reserved. */ public class ErrorCondition { private S // 姓名 private String idC // 身份证 private S // 错误状态 private S // 错误信息 ErrorCondition(String name,String idCard,String status,String message){ this.name = this.idCard = idC this.status = this.message = } public String getName() {
} public void setName(String name) { this.name = } public String getIdCard() { return idC } public void setIdCard(String idCard) { this.idCard = idC } public String getStatus() {
} public void setStatus(String status) { this.status = } public String getMessage() {
} public void setMessage(String message) { this.message = } }
2、JSP页面(index.jsp)
代码如下: &%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%& &html& &head& &script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.7.2.js"&&/script& &/head& &body& &input type="button" value="点我显示数据" id="clickMe"& &table id="showTable" border="1"& &tr& &td&姓名&/td& &td&身份证&/td& &td&错误状态&/td& &td&错误信息&/td& &/tr& &/table& &script& $("#clickMe").click(function(){ var url = "json/jsonTest.action"; $.ajax({ type:'get', url:url, dataType: 'json', success:function(data){ $.each(data,function(i,list){ var _tr = $("&tr&&td&"+list.name+"&/td&&td&"+ list.idCard+"&/td&&td&"+list.status+ "&/td&&td&"+list.message+"&/td&&/tr&"); $("#showTable").append(_tr); }) } }) }) &/script& &/body& &/html&
3、Action处理类(JsonTest_01.java)
代码如下: /** *@Project: jsonTest *@Author: chenssy *@Date:
*@Copyright: chenssy All rights reserved. */ public class JsonTest_01 { public String execute() throws IOException{ ErrorCondition r1 = new ErrorCondition("张三", "1611", "L", "长度错误"); ErrorCondition r2 = new ErrorCondition("李四", "191112","X", "校验错误"); ErrorCondition r3 = new ErrorCondition("王五", "", "N", "身份证信息为空"); List&ErrorCondition& list = new ArrayList&ErrorCondition&(); list.add(r1); list.add(r2); list.add(r3); //将list转化成JSON对象 JSONArray jsonArray = JSONArray.fromObject(list); HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE); response.setCharacterEncoding("UTF-8"); response.getWriter().print(jsonArray);
4、struts.xml配置
代码如下: &?xml version="1.0" encoding="GBK"?& &!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"& &!-- 指定struts 2的配置文件的跟元素 --& &struts& &package name="json" namespace="/json" extends="struts-default"& &action name="jsonTest" class="com.json.action.JsonTest_01" method="execute"&&/action& &/package& &/struts&
三、运行结果 开始页面如下:
当点击按钮后
返回的结果如下:
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
123456789102904人阅读
今天遇到提交多行数据问题, 在网上找了一点资料:
WEB 应用中一般都会处理主从表的信息, 或者称之为头层与行层的一对多的关系数据,如订单头/订单明细. 对于这种关系数据提交到后台的 Struts 的 ActionForm 的话, 这个 ActionForm 就要好好的设计一下, 不然会给自已带来许多额外的代码. 比如有的人的处理方法就是把页面提交到后台的毫无关系的散装数据非常吃力的拼凑一对多的关系对象出来.
下面举一个如今非常现实的关于股票的例子, 简单的应用场景是: 记录某个帐户所持有的股票信息,提交到后台,然后显示出来. 输入页面如下图
帐户信息包括帐户名和资金帐号;持有股票的每一行信息包括股票代码, 股票名称, 成本价, 股票数量. 股票行可以动态增删.
为了简化不必要的代码, 我们要实现的终及目标是: 在输入页面上点击 &保存数据& 按钮, 由 Struts 的 RequestProcessor.processPopulate() 方法把页面提交的基本信息组装到 AccountStockForm 的 account 的对应属性中,股票行信息对应生成一个 Stock 实例加到 AccountStockForm的 List 属性 stocks 中, 后续在 AccountStockAction 中直接处理account和stocks属性就非常简单了. AccountStockForm在这里只作为一个壳.
下面从前台到后台说明关键性的代码, 完整的 MyEclipse 工程包可以点击 TestStruts135.zip下载到.
一: struts-config.xml 配置
&?xml&version=&1.0&&encoding=&UTF-8&?& &&&!DOCTYPE&struts-config&PUBLIC &&&&&&&&&&&&&-//Apache&Software&Foundation//DTD&Struts&Configuration&1.3//EN&&&&&&&&&&&&&&http://struts.apache.org/dtds/struts-config_1_3.dtd&& &&&struts-config& &&&&&&&form-beans& &&&&&&&&&&&form-bean&name=&accountStockForm&&&&&&&&&&&&&type=&com.unmi.form.AccountStockForm&/& &&&&&&&/form-beans& &&&&&&&action-mappings& &&&&&&&&&&&action&path=&/showStock&&name=&accountStockForm&&&&&&&&&&&&type=&com.unmi.action.AccountStockAction&&scope=&request&& &&&&&&&&&&&&&&&forward&name=&show&&path=&/show.jsp&/& &&&&&&&&&&&/action& &&&&&&&/action-mappings& &&&/struts-config&&&
&?xml&version=&1.0&&encoding=&UTF-8&?&&&&!DOCTYPE&struts-config&PUBLIC&&&&&&&&&&&&&-//Apache&Software&Foundation//DTD&Struts&Configuration&1.3//EN&&&&&&&&&&&&&&http://struts.apache.org/dtds/struts-config_1_3.dtd&&&&&struts-config&&&&&&&&form-beans&&&&&&&&&&&&form-bean&name=&accountStockForm&&&&&&&&&&&&&type=&com.unmi.form.AccountStockForm&/&&&&&&&&/form-beans&&&&&&&&action-mappings&&&&&&&&&&&&action&path=&/showStock&&name=&accountStockForm&&&&&&&&&&&&type=&com.unmi.action.AccountStockAction&&scope=&request&&&&&&&&&&&&&&&&&forward&name=&show&&path=&/show.jsp&/&&&&&&&&&&&&/action&&&&&&&&/action-mappings&&&&/struts-config&&&
二: 输入页面 input.jsp, 注意表单域命名
&html:form&action=&/showStock&& &&&&&&&&&h3&记录持有的股票&br&&/h3& &&&&&&&&&fieldset&s&legend&基本信息&/legend& &&&&&&&&&table&width=&100%&&border=0&&tr& &&&&&&&&&&&&&td&帐户名:&html:text&property=&account.name&/&&/td& &&&&&&&&&&&&&td&资金帐号:&html:text&property=&account.number&/&&/td& &&&&&&&&&/tr&&/table& &&&&&&&&&/fieldset& &&&&&&&&&br& &&&&&&&&&fieldset&&legend&持有股票&/legend& &&&&&&&&&table&width=<span style="color:#c0%&border=0&id=&stockTable&&
&&&&&&&&&tr& &&&&&&&&&&&&&td&&input&type=&checkbox&&onclick=&checkAll(this)&&&/td& &&&&&&&&&&&&&td&股票代码&/td& &&&&&&&&&&&&&td&股票名称&/td& &&&&&&&&&&&&&td&成本价&/td& &&&&&&&&&&&&&td&股票数量&/td& &&&&&&&&&/tr& &&&&&&&&&tr& &&&&&&&&&&&&&td&&input&type=&checkbox&&name=&check&&&/td& &&&&&&&&&&&&&td&&input&name=&stocks[0].code&&size=&15&&&/td& &&&&&&&&&&&&&td&&input&name=&stocks[0].name&&size=&15&&&/td& &&&&&&&&&&&&&td&&input&name=&stocks[0].price&&size=&15&&&/td& &&&&&&&&&&&&&td&&input&name=&stocks[0].quantity&&size=&15&&&/td& &&&&&&&&&/tr& &&&&&&&&&/table& &&&&&/html:form&&&
&html:form&action=&/showStock&&&&&&&&&&&h3&记录持有的股票&br&&/h3&&&&&&&&&&fieldset&s&legend&基本信息&/legend&&&&&&&&&&table&width=&100%&&border=0&&tr&&&&&&&&&&&&&&td&帐户名:&html:text&property=&account.name&/&&/td&&&&&&&&&&&&&&td&资金帐号:&html:text&property=&account.number&/&&/td&&&&&&&&&&/tr&&/table&&&&&&&&&&/fieldset&&&&&&&&&&br&&&&&&&&&&fieldset&&legend&持有股票&/legend&&&&&&&&&&table&width=100%&border=0&id=&stockTable&&&&&&&&&&&tr&&&&&&&&&&&&&&td&&input&type=&checkbox&&onclick=&checkAll(this)&&&/td&&&&&&&&&&&&&&td&股票代码&/td&&&&&&&&&&&&&&td&股票名称&/td&&&&&&&&&&&&&&td&成本价&/td&&&&&&&&&&&&&&td&股票数量&/td&&&&&&&&&&/tr&&&&&&&&&&tr&&&&&&&&&&&&&&td&&input&type=&checkbox&&name=&check&&&/td&&&&&&&&&&&&&&td&&input&name=&stocks[0].code&&size=&15&&&/td&&&&&&&&&&&&&&td&&input&name=&stocks[0].name&&size=&15&&&/td&&&&&&&&&&&&&&td&&input&name=&stocks[0].price&&size=&15&&&/td&&&&&&&&&&&&&&td&&input&name=&stocks[0].quantity&&size=&15&&&/td&&&&&&&&&&/tr&&&&&&&&&&/table&&&&&&/html:form&&&
例如输入框名 account.name 提交后能设置到 accountStockForm 的account的name属性
输入框名为 stocks[0].code 提交后会设置到 accountStockForm 的 List stocks的第一个元素的code属性.以此类推
在提交表单前要重排行层的索引,从 0 起, 否则到后右的 Form 会一些空数据.
三: AccountStockForm 的关键代码
private&Account&account&=&new&Account(); &&&&private&List&stocks&=&new&AutoArrayList(Stock.class);
&&&& &&&&public&void&setStocks(List&stocks) &&&&{ &&&&&&&&this.stocks.clear(); &&&&&&&&this.stocks.addAll(stocks); &&&&}&&
private&Account&account&=&new&Account();&&&&private&List&stocks&=&new&AutoArrayList(Stock.class);&&&&&&&&public&void&setStocks(List&stocks)&&&&{&&&&&&&&this.stocks.clear();&&&&&&&&this.stocks.addAll(stocks);&&&&}&&
定义了两个属性,分别是一个bean(Account,接受基本信息)和一个List(stocks,接受股票行信息),注意这两个属性必须初始化,不然在表单提交后会出现空指针错误. setStocks方法是让stocks属性永远保有持是一个 AutoArrayList 实例. 这样在表单提交后设置&#20540;是总能调用 AutoArrayList 的 get(int index) 方法.
四: 自定义的 AutoArrayList
public&class&AutoArrayList&extends&ArrayList&{
&&&&&& &&&&&&private&Class&itemC &&&&&& &&&&&&public&AutoArrayList(Class&itemClass)&{ &&&&&&&&&&this.itemClass&=&itemC &&&&&&} &&&&&& &&&&&&public&Object&get(int&index)&{ &&&&&&&&&&try&{ &&&&&&&&&&&&&&while&(index&&=&size())&{ &&&&&&&&&&&&&&&&&&add(itemClass.newInstance()); &&&&&&&&&&&&&&} &&&&&&&&&&}&catch&(Exception&e)&{ &&&&&&&&&&&&&&e.printStackTrace(); &&&&&&&&&&} &&&&&&&&&&return&super.get(index); &&&&&&} &&}&&
public&class&AutoArrayList&extends&ArrayList&{&&&&&&&&&&&&private&Class&itemC&&&&&&&&&&&&public&AutoArrayList(Class&itemClass)&{&&&&&&&&&&this.itemClass&=&itemC&&&&&&}&&&&&&&&&&&&public&Object&get(int&index)&{&&&&&&&&&&try&{&&&&&&&&&&&&&&while&(index&&=&size())&{&&&&&&&&&&&&&&&&&&add(itemClass.newInstance());&&&&&&&&&&&&&&}&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&&}&&&&&&&&&&return&super.get(index);&&&&&&}&&}&&
理解为什么要继承一个ArrayList, 覆写get(int index)方法要简单了解 Struts 处理提交数据的工作原理: 大致如下: 页面提交后, 由 ActionServlet交给RequestProcessor的processPopulate()方法,由processPopulate()方法收集请求数据,放在map中,key为表单域的name属性,如 name, account.name, stocks[0].code. 然后借助于 Common-beanutils 工具包设置到 ActionForm
的相应属性中
如果key是简单的'name',直接form.setName(map.get('name'));
如果key是'account.name', 执行的操作是 form.getAccount().setName(map.get('account.name');
如果key是'stocks[0].code', 它可以对应到数据或集合中,如对于数组 form.stocks[0].code=map.get('stocks[0].code'); 对于集合((List)form.getStocks()).get(0).setCode(map.get('stocks[0].code'))
从上也能理解为什么 form 中的那两个属性必须实始化,不然会出现空指针错. 而且为什么 stocks 要用 AutoArrayList 实例化, 避免出现索引越界的错误.
五: 在 AccountStockAction 中可以打印出提交的数据
AccountStockForm&asForm&=&(AccountStockForm) &&&&&& &&&&&&Account&account&=&asForm.getAccount(); &&&&&&System.out.println(&Account&Name:&&#43;account.getName()&#43; &&&&&&&&&&&&&&&&Number:&&#43;account.getNumber()); &&&&&& &&&&&&List&stocks&=&asForm.getStocks(); &&&&&&for&(int&i=0;&i&stocks.size()&;i&#43;&#43;)
&&&&&&{ &&&&&&&&&&Stock&stock&=&(Stock)stocks.get(i); &&&&&&&&&&System.out.println(&Stock[&&#43;i&#43;&]Code:&&#43;stock.getCode()&#43; &&&&&&&&&&&&&&&&&&&&Name:&&#43;stock.getName()&#43; &&&&&&&&&&&&&&&&&&&&Price:&&#43;stock.getPrice()&#43; &&&&&&&&&&&&&&&&&&&&Quantity:&&#43;stock.getQuantity()); &&&&&&} &&&&&& &&&&&&return&mapping.findForward(&show&);&&
AccountStockForm&asForm&=&(AccountStockForm)&&&&&&&&&&&&Account&account&=&asForm.getAccount();&&&&&&System.out.println(&Account&Name:&&#43;account.getName()&#43;&&&&&&&&&&&&&&&&Number:&&#43;account.getNumber());&&&&&&&&&&&&List&stocks&=&asForm.getStocks();&&&&&&for&(int&i=0;&i&stocks.size()&;i&#43;&#43;)&&&&&&{&&&&&&&&&&Stock&stock&=&(Stock)stocks.get(i);&&&&&&&&&&System.out.println(&Stock[&&#43;i&#43;&]Code:&&#43;stock.getCode()&#43;&&&&&&&&&&&&&&&&&&&&Name:&&#43;stock.getName()&#43;&&&&&&&&&&&&&&&&&&&&Price:&&#43;stock.getPrice()&#43;&&&&&&&&&&&&&&&&&&&&Quantity:&&#43;stock.getQuantity());&&&&&&}&&&&&&&&&&&&return&mapping.findForward(&show&);&&
在Action中就能直接取用提交来的数据了,不需要 getParameterValues()了.
六: 最后一步, 对于这样的 ActionForm 我们应该如何显示出来呢,我们用了 nested 标签 (show.jsp)
&html:form&action=&/showStock&& &&&&&&&h3&修改持有的股票&br&&/h3& &&&&&&&fieldset&&legend&基本信息&/legend& &&&&&&&table&width=&100%&&border=0&&tr& &&&&&&&nested:nest&property=&account&& &&&&&&&&&&&td&帐户名:&nested:text&property=&name&&readonly=&true&/&&/td& &&&&&&&&&&&td&资金帐号:&nested:text&property=&number&&readonly=&true&/&&/td& &&&&&&&/nested:nest& &&&&&&&/tr&&/table& &&&&&&&/fieldset& &&&&&&&br& &&&&&&&fieldset&&legend&持有股票&/legend& &&&&&&&table&width=<span style="color:#c0%&border=0&&id=&stockTable&&
&&&&&&&tr& &&&&&&&&&&&td&&input&type=&checkbox&&onclick=&checkAll(this)&&&/td& &&&&&&&&&&&td&股票代码&/td& &&&&&&&&&&&td&股票名称&/td& &&&&&&&&&&&td&成本价&/td& &&&&&&&&&&&td&股票数量&/td& &&&&&&&/tr& &&&&&&&nested:iterate&id=&stock&&property=&stocks&& &&&&&&&tr& &&&&&&&&&&&td&&input&type=&checkbox&&name=&check&&&/td& &&&&&&&&&&&td&&nested:text&property=&code&&size=&15&/&&/td& &&&&&&&&&&&td&&nested:text&property=&name&&size=&15&/&&/td& &&&&&&&&&&&td&&nested:text&property=&price&&size=&15&/&&/td& &&&&&&&&&&&td&&nested:text&property=&quantity&&size=&15&/&&/td& &&&&&&&/tr& &&&&&&&/nested:iterate& &&&&&&&/table& &&&/html:form&&&
&html:form&action=&/showStock&&&&&&&&&h3&修改持有的股票&br&&/h3&&&&&&&&fieldset&&legend&基本信息&/legend&&&&&&&&table&width=&100%&&border=0&&tr&&&&&&&&nested:nest&property=&account&&&&&&&&&&&&&td&帐户名:&nested:text&property=&name&&readonly=&true&/&&/td&&&&&&&&&&&&td&资金帐号:&nested:text&property=&number&&readonly=&true&/&&/td&&&&&&&&/nested:nest&&&&&&&&/tr&&/table&&&&&&&&/fieldset&&&&&&&&br&&&&&&&&fieldset&&legend&持有股票&/legend&&&&&&&&table&width=100%&border=0&&id=&stockTable&&&&&&&&&tr&&&&&&&&&&&&td&&input&type=&checkbox&&onclick=&checkAll(this)&&&/td&&&&&&&&&&&&td&股票代码&/td&&&&&&&&&&&&td&股票名称&/td&&&&&&&&&&&&td&成本价&/td&&&&&&&&&&&&td&股票数量&/td&&&&&&&&/tr&&&&&&&&nested:iterate&id=&stock&&property=&stocks&&&&&&&&&tr&&&&&&&&&&&&td&&input&type=&checkbox&&name=&check&&&/td&&&&&&&&&&&&td&&nested:text&property=&code&&size=&15&/&&/td&&&&&&&&&&&&td&&nested:text&property=&name&&size=&15&/&&/td&&&&&&&&&&&&td&&nested:text&property=&price&&size=&15&/&&/td&&&&&&&&&&&&td&&nested:text&property=&quantity&&size=&15&/&&/td&&&&&&&&/tr&&&&&&&&/nested:iterate&&&&&&&&/table&&&&/html:form&&&
可以查看生成的HTML源文件, 你就能更好理解 input.jsp 中的表单域为什么要那么命名了.
小结的内容是请注意以下几个重点:
1. 输入信息的页面 input.jsp 没有使用 Struts 标签,目的是让大家理解,表单域应如何命名才能对应上 ActionForm 中的哪一个属性
2. 显示数据的页面是用的 Struts 标签,并留意 nested 标签的应用. 可以从生成的 HTML 源文件中体会出什么
3. 提交数据前要重新编排行层中输入框 Name 属性的下标植.
4. 回味为什么要引入 ArrayList 的子类 AutoArrayList, 关键在 get(int index) 方法的覆写
5. 最后是 ActionForm 中 List 属性 stocks 的 setter 方法的实现, 保持那个 List 的运行时具体类型不变
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:58347次
排名:千里之外
转载:11篇
评论:16条
(1)(4)(2)(3)(8)(1)用心创造滤镜
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
。。。。。。
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1102)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'Struts2 参数 表单 提交 对象 POJO 方法大全',
blogAbstract:'在Struts2.0里面有一个非常牛*的功能就是支持更高级的POJO访问登陆页面login.jsp:&%@ page language=\"java\" contentType=\"text/ charset=utf-8\"%&&%@ taglib prefix=\"s\" uri=\"/struts-tags\" %&&!DOCTYPE html PUBLIC \"-//W 3C //DTD HTML 4.01 Transitional//EN\" \"',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:5,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'。。。。。。',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}struts2的标签generator生成的IteratorGenerator类型转换为ArrayList的方法 - CSDN博客
不知道有没有人遇到过这种问题,使用struts的标签generator生成字符串数组之后,好像必须需要内边内嵌一个iterator进行遍历。
这样经常有问题就是generator生成的是org.apache.struts2.util.IteratorGenerator类型,而不是List。
本人偶然的发现了个转换方法,下面直接上代码,分享一下:
stackContext中#questions的数据的json&#26684;式如下:
&&&&&&&&&aAnswer&:&&Davy&does&not&have&a&pet.##Davyls&pet&disappears.##Davyls&pet&turned&into&a&butterfly.&,
&&&&&&&&&aId&:&5464,
&&&&&&&&&opOptions&:&&19248-Davy&does&not&have&a&pet. _19250-Davyls&pet&turned&into&a&butterfly._19249-Davyls&pet&disappears.&,
&&&&&&&&&qId&:&5466,
&&&&&&&&&qQuestion&:&&Main&Characters:&Davy&and&Caty&Settings:&Kidsl&Club&,
&&&&&&&&&qtId&:&1195,
&&&&&&&&&qtInstruction&:&&qtInstruction.&,
&&&&&&&&&qtKnowId&:&740,
&&&&&&&&&qtStyle&:&8
&页面局部如下:
&s:property&value=&#questions[0].aAnswer&&/&&br/&
&s:generator&var=&answers&&separator=&##&&val=&#questions[0].aAnswer&/&
&s:property&value=&#answers&&/&--&&s:property&value=&#answers.getClass()&&/&&br/&
&s:property&value=&#answers.toString()&/&--&&s:property&value=&#answers.toString().getClass()&&/&&br/&
&s:property&value=&#answers.{#this}&&/&--&&s:property&value=&#answers.{#this}.getClass()&&/&&br/&
&!--&惊奇而又惊喜的发现,#answers虽然是org.apache.struts2.util.IteratorGenerator类型
但投影操作之后:&#answers.{#this}&
或者&#answers.{#this.toString()}&
&&却都是&java.util.ArrayList类型自己顶一个,求顶
放在request 到下一个action请求。
采用request.setAttribute();然后action接收是吗?
引用 1 楼 duguqiubaibenr 的回复:
自己顶一个,求顶
传参应该可以吧,比如;
&input type="button" value="跳转" onclick="location=''xxx.action?list=${你的list}"/&
在Action定义一个public List list=new ArrayList();
就可以接收到了。
引用 3 楼 duguqiubaibenr 的回复:
采用request.setAttribute();然后action接收是吗?
嗯,你试试。
如果仅是传递到下一个页面的话:
1)&input type="hidden" name="list" value="list" /&
在下个页面要用的时候用request.getParameter("list")取就可以了
2)form表单中的action="?list=list"
在下个页面要用的时候用request.getParameter("list")取就可以了
如果是想以后所有的页面都是有这个list,还是用session吧
引用 5 楼 rui888 的回复:
Quote: 引用 3 楼 duguqiubaibenr 的回复:
采用request.setAttribute();然后action接收是吗?
嗯,你试试。
接收不到。。。
引用 6 楼 hjnsunshine 的回复:
如果仅是传递到下一个页面的话:
1)&input type="hidden" name="list" value="list" /&
在下个页面要用的时候用request.getParameter("list")取就可以了
2)form表单中的action="?list=list"
在下个页面要用的时候用request.getParameter("list")取就可以了
如果是想以后所有的页面都是有这个list,还是用session吧
request.getParameter("list") 取出来的是字符串了吧,不是对象了
引用 6 楼 hjnsunshine 的回复:
如果仅是传递到下一个页面的话:
如果是想以后所有的页面都是有这个list,还是用session吧
struts2的话:
&input type="hidden" name="list" value="list" /&
在要用的action中添加一个list属性,并添加set方法,就可以自己取到了。
引用 4 楼 wlwlwlwl015 的回复:
Quote: 引用 1 楼 duguqiubaibenr 的回复:
自己顶一个,求顶
传参应该可以吧,比如;
&input type="button" value="跳转" onclick="location=''xxx.action?list=${你的list}"/&
在Action定义一个public List list=new ArrayList();
就可以接收到了。
public List list=new ArrayList();。。。然后呢
struts2不是可以自动封装的么,你在前台命名 name="xxx" 在后台写个getXxx();方法就可以获取了(什么类型都可以),然后再写一个setXxx();方法,跳转到另一个页面的时候用&s:property value="xxx"/& 就又可以把这个参数拿出来了。后台不作任何处理,就是原封不动。
引用 9 楼 hjnsunshine 的回复:
Quote: 引用 6 楼 hjnsunshine 的回复:
如果仅是传递到下一个页面的话:
如果是想以后所有的页面都是有这个list,还是用session吧
struts2的话:
&input type="hidden" name="list" value="list" /&
在要用的action中添加一个list属性,并添加set方法,就可以自己取到了。
取到的不是list了是一个XWorkList的东东。。。
引用 10 楼 duguqiubaibenr 的回复:
Quote: 引用 4 楼 wlwlwlwl015 的回复:
Quote: 引用 1 楼 duguqiubaibenr 的回复:
自己顶一个,求顶
传参应该可以吧,比如;
&input type="button" value="跳转" onclick="location=''xxx.action?list=${你的list}"/&
在Action定义一个public List list=new ArrayList();
就可以接收到了。
public List list=new ArrayList();。。。然后呢
你在action中生成set方法,它就可以自动将页面中的list赋值给action中的list了
引用 10 楼 duguqiubaibenr 的回复:
Quote: 引用 4 楼 wlwlwlwl015 的回复:
Quote: 引用 1 楼 duguqiubaibenr 的回复:
自己顶一个,求顶
传参应该可以吧,比如;
&input type="button" value="跳转" onclick="location=''xxx.action?list=${你的list}"/&
在Action定义一个public List list=new ArrayList();
就可以接收到了。
public List list=new ArrayList();。。。然后呢
没然后了,你在你的方法里打印一下list.size,应该能取到吧,前提是你前台的参数和你Action定义的集合名一致,而且你前台通过EL或者OGNL要保证你的List数据在页面上。
强悍 不错学习啊
不觉明厉!!
放在request 到下一个action请求。
其它技术资料
如果您喜欢IT行业或者对IT行业感兴趣,想开拓技术视野,欢迎加入本站官方QQ群:,在群里认识新朋友和交流技术^_^
Powered by && & 2013 &&&}

我要回帖

更多关于 arraylist赋值 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信