在JavaEye论坛中看到jiangweilou提问“Struts2中多行数据怎么提交”,mcikevin的回答。
Struts2中支持使用List在页面和Action之间直接传递表格数据。下面是一个示例:
public class Person {
int id;
String name;
int age;
float height;
}
这是一个POJO,getter和setting省略了。
action中可以这样使用:
public class MyAction {
public List getPeopleList() { … }
public void setPeopleList( List peopleList ) { … }
…
}
在我们使用Person类之前,需要添加一个配置文件,MyAction-conversion.properties,把这个文件和MyAction放在一起。
这个文件里只有一行内容:
Element_peopleList=Person
前缀Element_是一个常量,表明等号左边的表达式中跟在这个常量后面的是Action类中一个List类型的字段名。
等号右边的表达式是全类名(包含package)
下面是一个页面的代码片段:
<s:form action="update" method="post" >
<s:iterator value="peopleList" status="stat">
<s:hidden name="peopleList[%{#stat.index}].id" value="%{peopleList[#stat.index].id}"/>
<s:textfield label="Name" name="peopleList[%{#stat.index}].name" value="%{peopleList[#stat.index].name}"/>
<s:textfield label="Age" name="peopleList[%{#stat.index}].age" value="%{peopleList[#stat.index].age}" />
<s:textfield label="Height" name="peopleList[%{#stat.index}].height" value="%{peopleList[#stat.index].height}"/>
<br/>
</s:iterator>
<s:submit value="Update"/>
</s:form>
使用这段代码,Struts2会创建一个Person类的ArrayList,并且用setPersonList这个方法把页面表格中的值传递回Action。
如果你是想从用户界面中动态创建列表值,需要允许Struts2给列表中类的实例。那么在配置文件MyAction-conversion.properties中添加一行:
CreateIfNull_peopleList = true
分享到:
相关推荐
<package name="struts2" extends="struts-default"> <result name="add_success" type="redirect">/common/pub_add_success.jsp <result name="del_success" type="redirect">/common/pub_del_success...
Apache Struts 2 is an elegant, extensible framework for building enterprise-ready Java web applications. Distributions of Struts 2 are available as a free download under the Apache License. For more ...
8.3.1 Struts2中单个Java对象的输入校验形式 166 8.3.2 Struts2对象集合即批量输入的校验形式 169 8.4 Struts2输入校验器大全 171 第9章 Struts2国际化 178 9.1 Struts2国际化基础应用 178 9.1.1 国际化基础使用方式...
所谓正反序列化json数据就是后台把Java对象序列化成json格式字符串及前台把json格式字符串传到后台序列化成对应的Java对象,最重要的是这个例子有简单和复杂(List对象)的正反序列化的例子,里面虽然有纯ajax请求...
Apache Struts 2 is an elegant, extensible framework for building enterprise-ready Java web applications. Distributions of Struts 2 are available as a free download under the Apache License. For more ...
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { // 上传文件存放路径 private final static String ...
如果对象是当前页面的,则要使用# 1. <s:bean id="locales" name="com.turorial.Locales" >... <s:property value="#locales"/>
<?xml version="1.0" encoding="UTF-8"?... org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter <!-- 拦截所有的url --> <filter-name>struts2 <url-pattern>/* </web-app>
作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...
前台checkbox提交的数据,Struts2在后台自动封装成数组/List。 but 前台向后台批量提交对象数据,struts2就显得力不从心了,本案例着重介绍,批量向后台提交对象数据,源码,有图有真相哦! 欢迎交流、吐槽、指导...
java通过struts2框架实现excel导出,传入参数为一个List
ajax struts 无刷新访问后台返回json数据,返回list、map,java类对象等json数据,前台js解析json数据,内有jar包,代码类。是一个完整的项目实例。
使用struts2表单传值,可以传一个或者是作为一个对象的各个属性传,都非常灵活便捷。但是如果我们需要传一个数组并希望struts正确接收,该怎么处理呢? 下面我将通过普通表单和ajax两种方式讲解。首先我们有如下一个...
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter-name>struts org.apache.struts2.dispatcher.FilterDispatcher ...
本系列文章将详细分析 Java 流行框架 Struts2 的历史漏洞,今后关于 Struts2 新的漏洞分析,也将更新于 项目上。该系列仅是笔者初学 Java代码审计 的一些记录,也希望能够帮助到想学习 Java代码审计 的朋友 。如有...
全书分4篇,共24章,其中,第1篇为技能学习篇,主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e...
书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。书中...
2. 模糊查询时,因同时需要进行where筛选,SQL语句无法执行,只好分开查询,先进行模糊查询,查询结果存放到list表中,再通过遍历list表进行判断是否符合where的条件,若符合,则存入另一个list表中。这种方法虽然...