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文件中加入如下代码:
<!-- Spring 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring 容器启动监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的
配置文件,以启动Spring MVC模块-->
<servlet>
<servlet-name>annomvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>annomvc</servlet-name>
<url-pattern>*.dox</url-pattern>
</servlet-mapping>
3,在WebRoot下,新建 annomvc-servlet.xml 详细代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.test" />
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:annotation-config />
</beans>
到此,我们的spring2.5 mvc模块基本配置完毕,接下来,编写实体bean
5,新建 OrderHist 类 代码片段如下:
private String id; //订单主键
private String consignee; //收货人
private String address; //地址
private String tel; //电话
private List<OrderDet> detList = new AutoArrayList(OrderDet.class); //产品明细
public List<OrderDet> getDetList() {
return detList;
}
/*注意:list对象的 set 方法*/
public void setDetList(List<OrderDet> detList) {
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 的值)
<form:form action="add.dox" modelAttribute="frmOrder">
订单号: <form:input path="id" /><br/>
收货人: <form:input path="consignee"/><br/>
地 址: <form:input path="address"/><br/>
电 话: <form:input path="tel"/><br/>
产 品1: <form:input path="detList[0].prodName"/> <br/>
产 品2: <form:input path="detList[1].prodName"/> <br/>
<input type="submit" value="提交"/>
</form:form>
到此,所以配置就完成了,接下来就是部署,然后输入访问地址测试。
http://localhost:8090/spring/add.dox
要完善此代码,应该在控制类中,调用业务层方法,然后业务层去调用dao连接数据库操作。