spring form提交对象数组
在日常的B/S开发中,经常遇到添加像主从表的数据到数据库,我以前的做法是先获取主表的信息,封装到bean,然后读取从表的信息在controller中封装到bean的子对象中。然后提交后台业务逻辑处理。在用到spring2.5 的mvc后,我想直接通过配置好bean,然后spring把页面数据装入bean中。
举例:数据表:orderhist (订单主表),orderdet (订单详表),一对多
代码结构
步骤:
1,首先建立web工程名称为spring。并导入spring mvc所需的jar包,大致如下:
2,配置web.xml文件,在web.xm文件中加入如下代码:
org.springframework.web.context.ContextLoaderListener
org.springframework.web.servlet.DispatcherServlet
3,在WebRoot下,新建 annomvc-servlet.xml 详细代码如下:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
>
4,src 下新建 applicationContext.xml
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
到此,我们的spring2.5 mvc模块基本配置完毕,接下来,编写实体bean
5,新建 OrderHist 类 代码片段如下:
private String id; //订单主键
private String consignee; //收货人
private String address; //地址
private String tel; //电话
private List
public List
return detList;
}
/*注意:list对象的 set 方法*/
public void setDetList(List
this.detList.clear();
this.detList.addAll(detList);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
……
6,新建 OrderDet 类
public class OrderDet {
private String orderId; //订单主表id
private String prodName; //产品名称
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getProdName() {
return prodName;
}
public void setProdName(String prodName) {
this.prodName = prodName;
}
}
7,重写 ArrayList ,主要是重写 get方法
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);
}
}
8, 控制层如下
package com.test;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bean.OrderDet;
import com.bean.OrderHist;
@Controller
public class MyController {
/**
* 添加订单
* @param request
* @param od
* @return
*/
@RequestMapping("/add.dox")
public String addOrder(HttpServletRequest request,@ModelAttribute("frmOrder") OrderHist od){
System.out.println("获取页面输入信息");
System.out.println("订单号:"+od.getId());
System.out.println("收货人: "+od.getConsignee());
System.out.println("地址: "+od.getAddress());
System.out.println("电话: "+od.getTel());
System.out.println("-------------------------");
//读取订单详表信息
List rsl = (List)od.getDetList();
for(int i = 0;i < rsl.size();i ++){
OrderDet odt = (OrderDet)rsl.get(i);
System.out.println("产品名称: -----"+odt.getProdName());
}
return "order";
}
}
9,jsp页面代码如下,(注意产品 path 的值)
订单号:
收货人:
地 址:
电 话:
产 品1:
产 品2:
到此,所以配置就完成了,接下来就是部署,然后输入访问地址测试。
http://localhost:8090/spring/add.dox
要完善此代码,应该在控制类中,调用业务层方法,然后业务层去调用dao连接数据库操作。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
