最近在项目中碰到一种情况,就是页面提交多行form表单数据,在网上查找了一下,发现两种方法挺好用的,供大家参考下.
第一种是运用struts框架来自动提交多行表单数据(在网上发现这篇文章介绍的很清楚了,直接转载下**)
文章出自:http://coderdream.javaeye.com/blog/263896
使用Struts提交多行数据
以下是使用struts提交多行数据的例子,提交多行数据至SurveyListForm:
提交页面form.jsp,用于提交两条数据,注意[]的使用:
- <FORM METHOD=POST ACTION="survey.jui">
- <INPUT TYPE="text" NAME="surveys[0].checkPerson">
- <INPUT TYPE="text" NAME="surveys[1].checkPerson">
- <INPUT TYPE="submit">
- </FORM>
响应页面index.jsp(survey.jui),用于获得数据:
- <logic:iterate id="survey" name="surveyListForm" property="surveys" indexId="index">
- <html:text name="survey" property="checkPerson" indexed="true"/>
- </logic:iterate>
struts-config.xml:
- <form-beans>
- <form-bean name="surveyForm" type="com.fenet.insurance.crm.web.form.SurveyForm" />
- <form-bean name="surveyListForm" type="com.fenet.insurance.crm.web.form.SurveyListForm" />
- </form-beans>
- <action path="/survey" parameter="method"
- type="com.fenet.insurance.crm.web.action.SurveyAction"
- name="surveyListForm" scope="request" validate="false">
- <forward name="method1" path="/index.jsp" />
- </action>
SurveyAction:
- public class SurveyAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) throws Exception {
- SurveyListForm sForm = (SurveyListForm)form;
- List list = sForm.getSurveys();
- for(int i=0; i<list.size(); i++){
- SurveyForm f = (SurveyForm)list.get(i);
- System.out.println(f.getCheckPerson());//后台打印多行数据
- }
- request.setAttribute("surveys", sForm);
- return mapping.findForward("method1");
- }
- }
SurveyListForm定义多行数据:
- public class SurveyListForm extends BaseForm{
- private List surveys = new AutoArrayList (SurveyForm.class);
- /**
- * @return Returns the surveys.
- */
- public List getSurveys() {
- return surveys;
- }
- /**
- * @param surveys The surveys to set.
- */
- public void setSurveys(List surveys) {
- this.surveys = surveys;
- }
- }
SurveyForm:
- public class SurveyForm extends BaseForm{
- private String checkPerson;
- /**
- * @return Returns the checkPerson.
- */
- public String getCheckPerson() {
- return checkPerson;
- }
- /**
- * @param checkPerson The checkPerson to set.
- */
- public void setCheckPerson(String checkPerson) {
- this.checkPerson = checkPerson;
- }
- }
AutoArrayList,用于自动创建List里面的对象:
- public class AutoArrayList extends ArrayList {
- private Class itemClass;
- public AutoArrayList(Class itemClass) {
- this.itemClass = itemClass;
- }
- public Object get(int index) {
- try {
- while (index >= size()) {
- add(itemClass.newInstance());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return super.get(index);
- }
- }
在Struts中使用JavaBean和List(多行数据)类型属性
http://tech.ccidnet.com/art/3741/20030227/533379_1.html
在 Strust 中,我们可能经常要在 ActionForm 中使用其他 JavaBean 作为属性类型,这里就存在着如何使用好这些属性与 HTML Form 之间的数据交换,下面我们就这些问题做一讲解。
如在用户注册界面中,我们通常会将用户的联系方式形成一个单独的新的 Class ,如 Contact ,包含以下属性:电话 (tel) ,手机(cell) , Email(email) , QQ(qq) ,通信地址 (adress) 等等,这样条理也比较清晰,以下是这个
ActionForm 的部分代码:
- public class RegisterForm extends ActionForm{
- private Integer id;
- private String logonName;
- private String realName;
- private Contact contact=new Contact();
- ………..
- }
在这里我们需要将 Contact 实例化(在 reset 函数中需要重新实例化),这主要因为 Struts 的机制:如我们将 HTML 的Form 元素(如 email )值传给 ActionForm , Struts 需要执行的操作是getContact().setEmail(String email) ,如果这时返回的 contact 对象为空的话,那么赋值如何进行,而且Struts 也不会知道如何去实例化 Contact ,有时这些 JavaBean 的类型有可能是接口,实例化更是未知,所以关于JavaBean 类型在 ActionForm 中的实例化,你需要自行完成,而且必须完成。至于在实际操作中判断 JavaBean是否被进行过相关操作(不再是初始状态),你需要自行判断,其实也很简单,如可在 Contact 类中编写一个函数检验一下即可。
ActionForm 创建完毕后,我们需要在 Jsp Form 中引用这些 JavaBean 类型的属性值,那就很简单啦,只需采用“ Form 的属性名称”+“ . ”+“ JavaBean 中的属性名称”结构构成的名称付给相关元素即可。如:
- <html:text property="contact.email"/>
通过这种方式我们就可以处理好 ActionForm 中的 JavaBean 类型的属性值了。
实际的情况可能更复杂些,我们有时可能要提交多行数据到后退,而且行内的数据是相关的,如我们需要提交多个用户的联系方式,这些行内的数据,如email ,电话,手机,这些数据都是和用户编码相关的,现在我们修改一下 Contact 类,添加一个用户编码 (userId)属性,这时我们构建的 ActionForm 中可能需要一个列表数据( List )类型来处理这种情况。以下是这个 ActionForm的部分代码:
- public class ModifyBatchContactForm extends ActionForm{
- private List contact =new AutoArrayList (Contact.class);
- …..
- }
在以上代码中,我们同样处理了 List 类型数据的初始化。 Struts 在给 List 中的对象赋值时,当然需要先获取 List
数据,然后在获取 List 中某一个对象(通过 Index),最后给对象赋值。在这里想我们可以更详细点分析浏览器端传过来数据,字段值形式如下:contact[0].email=linux_china@hotmail.com , Struts 获取 ActionForm 中的 List数据后,通过 Index (此时为 0 )来获取 List 中封装的 JavaBean 对象,然而此时 List是空的(尽管初始化了,但是没有数据),是无法取得封装的对象的,因此我们要在 Struts 获取 List中封装的对象的时候给它创建一个,这样就保证对象的获取和赋值的成功进行,所有我们新建一个 AutoArrayList 类,继承ArrayList ,只要重写 get(int index) 方法即可,其实很简单,代码如下:
- public class AutoArrayList extends ArrayList {
- private Class itemClass;
- public AutoArrayList(Class itemClass) {
- this.itemClass = itemClass;
- }
- public Object get(int index) {
- try {
- while (index >= size()) {
- add( itemClass.newInstance());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return super.get(index);
- }
- }
这样我们就完成可以多行数据提交的 ActionForm设计,这里还有点提醒,如果从浏览器端传过来的行索引是跳跃是的,如缺少中间行,而你不想要这些数据的话,且首尾行 index相差很大的话,这种方式可能不太适合。这种多行提交形式比较适合对固定行的数据处理。如果行数不固定的话,你可以参考使用 MapForm 方式实现。
下面我们就看看如何在 Jsp 中使用这个 ActionForm ,其实只需要执行一个循环即可:
- <logic:iterate id=" contact " name=" FormName " property=" contact " indexId="index">
- <html:text name=" contact " property="userId" indexed="true" />
- <html:text name=" contact " property="email" indexed="true"/>
- </logic:iterate>
这里我们再讲解一下,代码中出现的“ contact ”(红色)都是 ActionForm 中的 List数据类型变量名称,请确保一致,请不要更改名称,这也是方便提交到后台的数据接收。“ FormName ”为在 struts-config中声明的 ActionForm 名称。 indexed="true" 可以确保生成的 html 元素的名称唯一,不要缺少这个声明。以上的Jsp 代码,你也可以使用 JSTL 来完成,就看你习惯如何了。
通过以上步骤,我们完成了多行数据的提交所有环节,这样 Struts 就会完成其他所有的事情,我们的代码逻辑和实现也简单多了。
总结:通过以上两个例子,相以信对 ActionForm 中使用 JavaBean 和 List 类型数据不会再陌生,同时使用 Map类型,这样可创建更好的 ActionForm 设计,不过引入这种方式后,在Validator 方面你可能要花点脑筋啦(如通过 XDoclet 生成 Validator文件可能功能不全),可能你需要手写一些代码来完成相关工作。
第二种方法是封装成xml文章流,这个想必大家都很清楚了.
var root = doc.createElement("root"); //设置根节点
var tabelTag = document.getElementById("type_0_tb_0"); //读取table标签以便于获取表单数据
var obj = doc.createElement("obj"); //step 2 设置子节点
//step 3 设置
for (i=1; i <= tabelTag.rows.length; i++) { //循环行
var sub = doc.createElement("sub"); //创建子节点
for (j=1; j < 3; j++) { //循环列,在这里想要取哪几列数据就直接定义到哪列
if(j == 1){
sub.setAttribute("obj_name",document.getElementById("obj_name_"+i).value); //取数据
sub.setAttribute("obj_id",document.getElementById("obj_id_"+i).value);
}else if(j == 2){
sub.setAttribute("remark",document.getElementById("bug_description_"+i).value);
}
}
obj.appendChild(sub); // 将存储数据的节点加入到子节点中
}
root.appendChild(obj);//将子节点添加到根节点中
doc.appendChild(root);
//返回XML文档
return doc.xml;
var url = "filter.do?action=filter";
var xmlDocument = XmlDocument.create();//创建xml标签
var filterInfo = xmlDocument.createElement("filterInfo");
xmlDocument.appendChild(filterInfo);
appendSimpleChildNode(xmlDocument, filterInfo, "type", type);//填充数据
appendSimpleChildNode(xmlDocument, filterInfo, "condition", condition);
xmlHttp = XmlHttp.create();
xmlHttp.open("POST", url, false);
xmlHttp.send(xmlDocument.xml);
var xmlString = xmlHttp.responseText;
return xmlString;
后台获取
SAXBuilder saxb = new SAXBuilder();
Document doc = saxb.build(request.getInputStream());
Element root = doc.getRootElement();