Spring标签提交List对象


前段时间工作中用到提交list对象的情况,项目框架是SSM,使用了不少的Spring标签,表单几乎都用的<form:form>、<form:input>..... 这样的,一定程度上减少了工作量,但也遇到了之前没用过的东西,比如 一个页面提交的是多个list的情况,这里总结一下。


使用<form:form>来提交List对象。

先说一下思路:
1.创建一个新的对象作为model,List作为对象的一个属性(可以有多个List)。例如 创建student的扩展对象studentExt,属性 有List<student>,List<teenager>两个属性。
2.提交的时候,name属性后边加索引,来区分同一类型的不同对象。例如:student[0].age,student[1].age,这样提交的就是Student类型的两个对象的相同属性。
3.后台直接使用扩展类的一个对象studentExt接收,然后创建List<Student> students,List<teenager> teens 来分别获取对应的list,对齐进行操作。


这里直接拿项目中的一些代码来演示、

创建的新的类的结构如下:

ComBaseInfoModel 这个类 继承了CombaseInfo,其他属性都是List。

public class ComBaseInfoModel extends ComBaseInfo {
	private static final long serialVersionUID = 1L;

	private List<ComController> controllerList;

	private List<ComExecutives> executivesList;

	private List<ComOwners> ownersList;

	public ComBaseInfoModel() {
	}

	public ComBaseInfoModel(String id) {
		super(id);
	}

	public List<ComController> getControllerList() {
		return controllerList;
	}

	public void setControllerList(List<ComController> controllerList) {
		this.controllerList = controllerList;
	}

	public List<ComExecutives> getExecutivesList() {
		return executivesList;
	}

	public void setExecutivesList(List<ComExecutives> executivesList) {
		this.executivesList = executivesList;
	}

	public List<ComOwners> getOwnersList() {
		return ownersList;
	}

	public void setOwnersList(List<ComOwners> ownersList) {
		this.ownersList = ownersList;
	}
}


jsp修改页面 这里使用了  modelAttribute ,modelAttribute属性指定该form绑定的Model,绑定model后,input标签上的path属性会解析成name和id,将表单数据提交到对应的model的属性上。

<form:form id="inputForm" modelAttribute="comBaseInfoModel" action="${ctx}/cominfo/comBaseInfo/saveAll" method="post" class="form-horizontal-2cols">

<form:hidden path="id"/>
<form:hidden path="companyid"/>
<form:hidden path="groupid"/>
<h4>企业基本信息</h4>
<table class="table table-striped table-bordered ">
	<tbody>
	<tr >
		<td class="span2">企业名称</td>
		<td class="span4"><form:input path="clientName" htmlEscape="false" maxlength="200" class="input-large required "/></td>
		<td class="span2">办公地址</td>
		<td class="span4"><form:input path="clientOffice" htmlEscape="false" maxlength="250" class="input-large required "/></td>
	</tr>
	</tbody>
</table>

<h4>股权结构</h4>
<table class="table table-striped table-bordered ">
	<thead>
	<tr>
		<th>名称</th>
		<th>类型</th>
		<th>认缴资本</th>

	</tr>
	</thead>
	<tbody class="stock_tbody">
	<c:forEach items="${comBaseInfoModel.controllerList}" var="item" varStatus="status">
			<form:hidden path="controllerList[${status.index}].companyid" />
			<form:hidden path="controllerList[${status.index}].groupid" />
			<td>
				<form:input path="controllerList[${status.index}].stockHolder" htmlEscape="false" maxlength="18" class="input-small required"/>
			</td>
			<td>

					<form:select path="controllerList[${status.index}].stockHolderType" class="input-small required">
						<form:option value="" label=""/>
						<form:options items="${fns:getDictList('Stock_holder_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
					</form:select>
			</td>
			<td>
				<div class="input-append">
					<fmt:formatNumber value="${item.capitalAnnouced}" var="annouced" pattern="#0.00#" />
					<form:input path="controllerList[${status.index}].capitalAnnouced" value="${annouced}" htmlEscape="false" maxlength="18" class="input-mini required"/>
					<span class="add-on">万元</span>
				</div>
			</td>

	</c:forEach>
	</tbody>
</table>


<h4>企业所有人</h4>
<table class="table table-striped table-bordered td_h20">
	<thead>
	<tr>
		<th class="span4">指标</th>
		<th class="span8">指标值</th>
	</tr>
	</thead>
	<tbody>
	<tr>
		<td>姓名</td>
		<td><form:input path="ownerName" htmlEscape="false" maxlength="18" class="input-large required"/></td>
	</tr>
	<tr>
		<td>身份证号码</td>
		<td><form:input path="ownerIdcard" htmlEscape="false" maxlength="18" class="input-large required card"/></td>
	</tr>



	</tbody>
</table>
<h4>从业经历</h4>
<table class="table table-striped table-bordered td_h20">
	<thead>
	<th>服务的公司</th>
	<th>最终职位</th>
	</thead>
	<tbody class="owner_tbody">

	<c:forEach items="${comBaseInfoModel.ownersList}" var="itema"  varStatus="statusa">

			<td><form:input path="ownersList[${statusa.index}].servComp" htmlEscape="false" maxlength="18" class="input-small required"/></td>
			<td><form:input path="ownersList[${statusa.index}].servPosition" htmlEscape="false" maxlength="18" class="input-small required"/></td>

	</c:forEach>
	</tbody>
</table>
</form:form>

 

在使用<c:forEach>时,items 是要遍历的list,varStatus是每次循环的状态, 里有index属性,这个是遍历的索引,要注意,有多个<c:forEach>时,这里的 varStatus 需要定义成不同的名字,这样在使用${}取值时才不会取错。这里的目的就是将每个对象使用index属性给区分开,提交的形式就是  student[0].name,student[0].age,student[1].name,student[1].age,这样才能区分开是不同的对象。

后台代码:

	@RequestMapping(value = "saveAll")
	public String saveAll(ComBaseInfoModel comBaseInfoModel, Model model, RedirectAttributes redirectAttributes) {

		comBaseInfoService.saveAll(comBaseInfoModel);
		ComBaseInfo bean = new ComBaseInfo();
		BeanUtils.copyProperties(comBaseInfoModel, bean);
		redirectAttributes.addFlashAttribute(bean);
		addMessage(redirectAttributes, "修改企业信息成功");
		return "redirect:"+Global.getAdminPath()+"/cominfo/comBaseInfo/detail?repage";
	}


这里可以直接从 comBaseInfoModel 上获取到页面提交的所有数据,只需获取每个List对象就能对其进行操作。

	public void saveAll(ComBaseInfoModel comBaseInfoModel) {
		ComBaseInfo comBaseInfo =  dao.get(comBaseInfoModel.getId());

		List<ComController> controllers = comBaseInfoModel.getControllerList();
		List<ComExecutives> executivess = comBaseInfoModel.getExecutivesList();
		List<ComOwners> comOwnerss = comBaseInfoModel.getOwnersList();
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值