Spring MVC表单提交

时间:2020-12-13 17:43:17

实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式。

 <form id="form1" class="form-horizontal chukong_form" method="post" action="${ctx}/channel/sdkversion/postedit.do">
<form:hidden path="sdkversion.id" />
<div class="form-group">
<label class="col-sm-3 control-label">版本号</label>
<div class="col-sm-6">
<form:input path="sdkversion.sdkVersion" value="${sdkversion.sdkVersion}" class="form-control phone1" id="m_version" />
</div>
</div> <div class="form-group">
<label class="col-sm-3 control-label">加密密钥</label>
<div class="col-sm-6">
<form:input path="sdkversion.itemMapperKey" value="${sdkversion.itemMapperKey}" class="form-control phone1" id="m_itemkey" />
</div>
</div> <div class="form-group">
<label class="col-sm-3 control-label">版本描述</label>
<div class="col-sm-6">
<form:input path="sdkversion.desc" value="${sdkversion.desc}" class="form-control phone1" id="m_desc" />
</div>
</div> <div class="modal-footer" style="border:none;">
<div class="col-sm-offset-2 col-sm-6">
<!-- <span style="margin-right: 30px;" class='label alert-warning' id="msgflag"></span> -->
<button type="button" class="btn searchB" id="btn_submit"
data-loading-text="Loading..." autocomplete="off">提&nbsp;&nbsp;交</button>
<a class="btn searchA" href="javascript:history.go(-1);" style="margin:2px 1.6% 0;">返&nbsp;&nbsp;回</a>
</div>
</div>
</form>

form标签
    和使用任何JSP扩展标签一样,在使用Spring表单标签之前,你必须在JSP页面中添加一行引用Spring表单标签的声明,如下所示:

服务器端

 1 @RequestMapping(value = "/sdkversion/postedit", method = RequestMethod.POST)
2 public String sdkVersionModify(final HttpServletRequest request,
3 final Model model, final SdkVersion sdkversion) {
4 sdkversion.setModifyDatetime(new Date());
5 final User currentUser = SessionManager.getCurrentUser(request.getSession());
6 sdkversion.setModifyBy(new Long(currentUser.getId()).intValue());
7 try {
8 sdkversionFacade.sdkVersionModify(sdkversion);
9 } catch (AppException appEx) {
10 DEBUG_LOGGER.error("edit SdkVersion fail.", appEx);
11 throw new SystemException(appEx);
12 } catch (Exception ex) {
13 final String error = "edit SdkVersion fail.";
14 DEBUG_LOGGER.error(error, ex);
15 throw new SystemException(error, ex);
16 }
17
18 return "redirect:/channel/sdkversion/query";
19 }

其中,form表单中的SdkVersion 对象的值以对象的形式传入后台进行操作,不用按原始的以表单中的name获取各个属性值,代码简洁,容易操作。

另外:

1.复选框标签的使用

1    兴趣:
2 <form:checkbox path="favorites" value="1"/>篮球
3 <form:checkbox path="favorites" value="2"/>足球
4 <form:checkbox path="favorites" value="3"/>阅读
5 <form:checkbox path="favorites" value="4"/>旅游

2.输入框标签的使用

<form:input path="sdkversion.itemMapperKey" value="${sdkversion.itemMapperKey}" />

3.单选框标签的使用

1  性 别:<form:radiobutton path="obj.sex" value="0"/>男
2 <form:radiobutton path="obj.sex" value="1"/>女

4.下拉框标签的使用

1 <form:select path="obj.city">
2 <form:option value="" label="--请选择--"/> ①提示性的选项
3 <form:options items="${cityMap}" itemValue="key" itemLabel="value"/>②真实的选项数据
4 </form:select>