`
baobeituping
  • 浏览: 1040869 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

struts2.0学习笔记六(struts2.0通过ognl实现一个表单多个对象保存)

阅读更多

以前,我们如果在一个FORM中要实现在这个FORM中对多个对象同时保存,是件很麻烦的事情,在struts2.0中,我们可以利用ognl方式来实现多个对象的同时保存.

1.ognlCollection.jsp

方式一:

<form action="ognlcollection.action" method="post">
    <input type="text" name="products[0].name" value="aaa" />
    <input type="text" name="products[1].name" value="bbb" />
     <input type="text" name="products2[0].name" value="ccc" />
    <input type="text" name="products2[1].name" value="ddd" />
    <input type="submit" value="submit" />
</form>

在JAVA中有一个procutcs的LIST,该属性名要和JAVA中的属性名称对应起来.

方式二:通过用纯OGNL表达式来做

<s:iterator value="new int[3]" status="stat">
                <tr>
                    <td><s:textfield name="%{'products['+#stat.index+'].name'}"/></td>
                    <td><s:textfield name="%{'products['+#stat.index+'].price'}"/></td>
                    <td><s:textfield name="%{'products['+#stat.index+'].dateOfProduction'}"/></td>
                </tr>
            </s:iterator>

通过iterator方式,NEW出三个对象

"%{'products['+#stat.index+'].name'}"通过索引来标记不同的对象,然后插入到后台的LIST当中#stat.index表示取出其中的索引.

 

2.OgnlCollectionAction.java

package com.ognl.action;

import java.util.List;
import java.util.Map;

import com.bean.Product;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.Element;
import com.vo.User;

public class OgnlCollectionAction extends ActionSupport {
 
    List<User> users;
 List<Product> products;
 @Element(value = Product.class)
 List products2;

//在这里通过两种方式来写LIST.第一种通过泛型,所以不用指出保存在LIST中的对象类型.第二中则是以前不用泛型的方式.必须要加上一个Annotation来表示保存在LIST中的对象名.

 

 public List<Product> getProducts() {
  return products;
 }

 public void setProducts(List<Product> products) {
  this.products = products;
 }
 @Override
 public String execute() throws Exception {
  System.out.println("------------------");
  for(Product p : products)
  {
   System.out.println("products name : "+p.getName());
  }
  System.out.println("products2[0] name : "+((Product)products2.get(0)).getName());
  System.out.println("products2[1] name : "+((Product)products2.get(1)).getName());
  return null;
 }

 public List<User> getUsers() {
  return users;
 }

 public void setUsers(List<User> users) {
  this.users = users;
 }

 public List getProducts2() {
  return products2;
 }

 public void setProducts2(List products2) {
  this.products2 = products2;
 }

 

 

 

 /*public List getUsers2() {
  return users2;
 }

 public void setUsers2(List users2) {
  this.users2 = users2;
 }

 public Map<String, User> getUsersMap() {
  return usersMap;
 }

 public void setUsersMap(Map<String, User> usersMap) {
  this.usersMap = usersMap;
 }

 public Map getUsersMap2() {
  return usersMap2;
 }*/

 /*public void setUsersMap2(Map usersMap2) {
  this.usersMap2 = usersMap2;
 }*/
 

}

3.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>
    <package name="struts2" extends="struts-default">
        <action name="ognl" class="com.ognl.action.OgnlAction">
            <result name="result">/ognl.jsp</result>
        </action>
        <action name="ognlcollection" class="com.ognl.action.OgnlCollectionAction">       
        </action>
    </package>
   
</struts>

 

分享到:
评论

相关推荐

    struts2.0中文教程

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    Struts 2.0系列(MAX)

    在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 ...

    struts2.0的特点

    struts2.0的特点

    Struts2.0中文教程权威版

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    struts2.0中的ognl

    基于WebWork核心的MVC开发与实践(ognl这一章)

    struts2.0 jar包

    struts2.0.12jar包最新版 一般放入下面几个就行commons-logging-1.0.4.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.12.jar xwork-2.0.6.jar

    struts2.0 jar包struts2.0jar包 struts2.0jar包源文件,下载文件包中是Struts2.0.11

    Struts 2中的表达式语言 ...OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言; Velocity,严格来说不是表达式语言,它是一种基于Java的模板匹配引擎,具说其性能要比JSP好。

    个人认为目前最完备的Struts2教程

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的...

    struts2中的OGNL的源码

    从官网checkout下来的源码,如果大家觉的区官网麻烦可以直接下载这个

    Struts2.0配置说明

    1.建立一个web Project; 2.导入struts2.0的核心包,注意全部导入全部的lib会引起错误; commons-fileupload-1.2.1.jar commons-io-1.3.2.jar freemarker-2.3.12.jar ognl-2.6.11.jar struts2-core-2.1.2.jar xwork-...

    Struts2.0jar包

    包含了struts2.0所需的5个jar包 commons-logging-1.1.jar freemarker-2.3.13.jar ognl-2.6.11.jar struts2-core-2.1.6.jar xwork-2.1.2.jar

    struts2 学习重点笔记

    这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用

    struts2.0 要用到的jar包

    struts2.0 要用到的 jar 包 commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-1.1.jar,freemarker-2.3.13.jar,ognl-2.6.11.jar,xwork-2.1.2.jar,struts2-core-2.1.6.jar 7个包 copy到WebRoot/WEB...

    Struts2.0培训课件

    学习Struts的前提 ...第一个Struts例子 Struts常用的标签 Struts的Action Struts的Result Struts的表单 Struts拦截器 国际化(i18n)的实现 Struts的文件上传 上传应用:学生多媒体信息管理 OGNL的应用

    struts2.0笔记+中文文档

    知识点: ready, tag, action, internationalization, converter, validation, interceptor,ioc,file upload, crud, ognl,struts2&ajax struts1&struts2

    struts2入门教程

    ·Struts 2.0中实现表单数据校验(Validation) ·拦截器(Interceptor) ·Struts 2中实现IoC ·Struts 2中实现文件上传 ·Struts 2中的OGNL ·Strus 2的新表单标签的使用 ·Struts 2与AJAX ·Struts2分页 ...

    struts2.0_jar和源文件

    该资源包括struts2.0所有的jar包,...其中最常用的jar包是commmons-logging-1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.jar和xwork-2.0.4.jar这5个包,这五个包在“常用五包”文件夹中。

    struts2.0漏洞补丁

    免费资源 ==================== struts2漏洞解决办法 commons-lang3-3.1.jar (保留commons-lang-2.6.jar...ognl-3.0.5.jar (替换旧版本) struts2-core-2.3.4.1.jar (替换旧版本) xwork-core-2.3.4.1.jar (替换旧版本)

    struts2 OGNL语言学习笔记

    本人在学习struts2框架时的学习笔记,主要是ognl表达式语言的运用。希望对大家有所帮助!

Global site tag (gtag.js) - Google Analytics