spring mvc 用map的形式接收form表单的参数: 博客分类: springmvc
只要在@controller中有@RequestParam Map<String, Object> regUser;这个map会自动在jsp提交的时候,将name作为map的key,对应的值作为map 的value,
这种适用注册等字段复杂,但是重用性不高的情况(此时不是另建bo接收),另用map接收由于用单个参数接收url之后的参数对name是依靠数据库临时组合出来的form,
中的域值可以都接到,详见后续的不同name的多图片上传,就连url后面的参数“=”也可
不需要可以
param.remove("navTabId");
param.remove("callbackType");
param.remove("menuitemId");
param.put("PROVINCE_AREA_KEY", param.get("provinceAreaKey"));//替换成数据库字段
param.put("CITY_AREA_KEY", param.get("cityAreaKey"));
param.put("COUNTY_AREA_KEY", param.get("country"));
param.remove("provinceAreaKey");
param.remove("cityAreaKey");
param.remove("country");
param.remove("button2");
http://www.codes51.com/article/detail_114729.html
@RestController
@RequestMapping(value = "/mapParameter/")
public class MapParameter {
/**
* 用Map接收前端提交的Form Data或Query String,如果有相同的参数名,只接收第一个
* @author XuJijun
* @param params
* @return
*/
@RequestMapping(value = "map")
public Map<String, Object> test1(@RequestParam Map<String, Object> params) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap = params;
return resultMap;
}
/**
* 用MultiValueMap接收前端提交的Form Data或Query String,可以接收相同的参数名的值到同一个list中
* @author XuJijun
* @param params
* @return
*/
@RequestMapping(value = "multiValueMap")
public Map<String, List<Object>> test2(@RequestParam MultiValueMap<String, Object> params) {
Map<String, List<Object>> resultMap = new HashMap<>();
resultMap = params;
return resultMap;
}
/**
* 用Map接收前端提交的json格式的Request Payload,如果有相同的参数名,只接收最后一个
* @author XuJijun
* @param params
* @return
*/
@RequestMapping(value = "jsonParams")
public Map<String, Object> test3(@RequestBody Map<String, Object> params) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap = params;
return resultMap;
}
}
示例:
@RequestMapping("/account/tbCusFirmChg/edit")
public String add(@RequestParam(value = "customerKey", required = false) String customerKey,@RequestParam Map<String, Object> regUser,Model model) throws EsteelException {
}
form action="<c:url value='/account/tbCusFirmChg/edit?navTabId=userLiNav&callbackType=closeCurrent'/>" method="post" validate="true" onsubmit="return validForm();">
<div style="height: 645px; overflow:auto; ">
<input type="hidden" name="CUSTOMER_KEY" value="CUSTOMER_KEY">
<input type="hidden" name="menuitemId" value="menuitemId">
<fieldset style="margin: auto;">
<legend>公司信息</legend>
<dl style="width: 50%;float: left;margin-bottom: 3px;">
<dt style="float: left;text-align:right;width: 20%; ">会员编码:</dt>
<dd style="float: left;text-align:left;width: 30%;"><input name="CUSTOMER_ID" id="regUser_CUSTOMER_ID" class="required textInput" value="${CUSTOMER_ID}" maxlength="18" onkeydown="if (event.keyCode==13)event.keyCode=9;"/></dd>
</dl>
<dl style="width: 50%;float: left;margin-bottom: 3px;">
<dt style="float: left;text-align:right;width: 20%;">子账号席位:</dt>
<dd style="float: left;text-align:left;width: 30%;"><input name="SET_USER_NUM" id="regUser.SET_USER_NUM" class="input-longer" value="${voo.SET_USER_NUM}" maxlength="3" /></dd>
</dl>
<dl style="width: 50%;float: left;margin-bottom: 3px;">
<dt style="float: left;text-align:right;width: 20%;">公司类型:</dt>
<dd style="float: left;text-align:left;width: 30%;"> <label id="regUser_COMP_TYPEA" ><input type="radio" id="COMP_TYPEA" name="COMP_TYPE" value="Y"
<c:if test="${voo.COMP_TYPE=='Y' or voo.COMP_TYPE!='N'}">checked="checked"</c:if> />境内<input type="radio" name="COMP_TYPE" value="N" <c:if test="${voo.COMP_TYPE=='N'}">checked="checked"</c:if> />*</label></dd>
</dl>
<dl style="width: 50%;float: left;margin-bottom: 3px;">
<dt style="float: left;text-align:right;width: 20%;" ><span id="wmzzmc">外贸资质:</span></dt>
<dd style="float: left;text-align:left;width: 30%;" ><SPAN id="wmzzxz"><label id="regUser_TRADE_APTA"><input type="radio" id="TRADE_APTA" name="TRADE_APT" value="Y" <c:if test="${voo.tradeApt=='Y' or voo.tradeApt!='N' or voo.COMP_TYPE=='Y'}">checked="checked"</c:if> />具备<input type="radio" name="TRADE_APT" value="N" <c:if test="${voo.tradeApt=='N'}">checked="checked"</c:if> />不具备</label></SPAN></dd>
</dl>
</div>
</form>