实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式。
1 <form id="form1" class="form-horizontal chukong_form" method="post" 2 action="${ctx}/channel/sdkversion/postedit.do"> 3 <form:hidden path="sdkversion.id" /> 4 <div class="form-group"> 5 <label class="col-sm-3 control-label">版本号</label> 6 <div class="col-sm-6"> 7 <form:input path="sdkversion.sdkVersion" value="${sdkversion.sdkVersion}" class="form-control phone1" id="m_version" /> 8 </div> 9 </div> 10 11 <div class="form-group"> 12 <label class="col-sm-3 control-label">加密密钥</label> 13 <div class="col-sm-6"> 14 <form:input path="sdkversion.itemMapperKey" value="${sdkversion.itemMapperKey}" class="form-control phone1" id="m_itemkey" /> 15 </div> 16 </div> 17 18 <div class="form-group"> 19 <label class="col-sm-3 control-label">版本描述</label> 20 <div class="col-sm-6"> 21 <form:input path="sdkversion.desc" value="${sdkversion.desc}" class="form-control phone1" id="m_desc" /> 22 </div> 23 </div> 24 25 <div class="modal-footer" style="border:none;"> 26 <div class="col-sm-offset-2 col-sm-6"> 27 <!-- <span style="margin-right: 30px;" class=‘label alert-warning‘ id="msgflag"></span> --> 28 <button type="button" class="btn searchB" id="btn_submit" 29 data-loading-text="Loading..." autocomplete="off">提 交</button> 30 <a class="btn searchA" href="javascript:history.go(-1);" style="margin:2px 1.6% 0;">返 回</a> 31 </div> 32 </div> 33 </form>
form标签
和使用任何JSP扩展标签一样,在使用Spring表单标签之前,你必须在JSP页面中添加一行引用Spring表单标签的声明,如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> ①引入标签的声明 <html> … ②声明后,在页面中就可以使用任意Spring表单标签了 </html>
产生的HTML页面为:
1 <form id="form1" class="form-horizontal chukong_form" method="post" action="/manager/channel/sdkversion/postedit.do" novalidate="novalidate"> 2 <input id="id" name="id" type="hidden" value="1"> 3 <div class="form-group"> 4 <label class="col-sm-3 control-label">版本号</label> 5 <div class="col-sm-6"> 6 <input id="m_version" name="sdkVersion" value="1.0.1" class="form-control phone1 valid" type="text"> 7 </div> 8 </div> 9 10 <div class="form-group"> 11 <label class="col-sm-3 control-label">加密密钥</label> 12 <div class="col-sm-6"> 13 <input id="m_itemkey" name="itemMapperKey" value="111222" class="form-control phone1" type="text"> 14 </div> 15 </div> 16 17 <div class="form-group"> 18 <label class="col-sm-3 control-label">版本描述</label> 19 <div class="col-sm-6"> 20 <input id="m_desc" name="desc" value="test" class="form-control phone1" type="text"> 21 </div> 22 </div> 23 24 <div class="modal-footer" style="border:none;"> 25 <div class="col-sm-offset-2 col-sm-6"> 26 <!-- <span style="margin-right: 30px;" class=‘label alert-warning‘ id="msgflag"></span> --> 27 <button type="button" class="btn searchB" id="btn_submit" data-loading-text="Loading..." autocomplete="off">提 交</button> 28 <a class="btn searchA" href="javascript:history.go(-1);" style="margin:2px 1.6% 0;">返 回</a> 29 </div> 30 </div> 31 </form>
服务器端
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>
时间: 2024-10-12 07:46:48