測試需要的jar包:spring 3.2.jar + jackson-all-1.8.5.jar。
寫代碼時碰到個需要將對象里的子明細一起傳遞到controller里去,當時就想直接將參數一起傳遞過來,貼下代碼:
controller:
@RequestMapping(params="save") @ResponseBody public CustomForeignKey save(@RequestBody CustomForeignKey customForeignKey,Model model,ModelAndView mv,HttpServletRequest req){ return customForeignKeyService.add(customForeignKey); }
參數對象:
public class CustomForeignKey { private Long id; private Long tableId; private Long foreignKeyTableId; private String foreignKeyTableName; private String name; private List<CustomForeignKeyRelation> customForeignKeyRelations; public Long getId() { return id; }
對象下子明細:CustomForeignKeyRelation :
public class CustomForeignKeyRelation { private Long id; private Long customForeignKeyId; private Long leftCustomColumnId; private String leftCustomColumnName; private String leftCustomColumnAlias; private Long rightCustomColumnId; private String rightCustomColumnName; private String rightCustomColumnAlias; public Long getId() { return id; }
js傳遞的代碼段:
var relations = []; $.each(rows,function(i,obj){ if(obj.leftCustomColumnId != 0 && obj.leftCustomColumnName && obj.leftCustomColumnName != '無') relations.push({leftCustomColumnId:obj.leftCustomColumnId, leftCustomColumnName:obj.leftCustomColumnName, rightCustomColumnId:obj.rightCustomColumnId, rightCustomColumnName:obj.rightCustomColumnName}); }) var dd = {tableId:selectRowOfTable.id, name:t_fk_name, foreignKeyTableId:$("#foreignKeyDialog_foreignTableId").combobox("getValue"), foreignKeyTableName:$("#foreignKeyDialog_foreignTableId").combobox("getText"), customForeignKeyRelations:relations }; /*$.post("customForeignKey.htm?add",dd,function(){ dgForeignKey.datagrid("reload"); foreignKeyDialog.dialog("close"); });*/ $.ajax({ url:"customForeignKey.htm?add", type:"post", dataType:"json", contentType:"application/json", data:JSON.stringify(dd), success:function(){ alert("success"); } });
按照網上所說,我將 ajax的 contentType:"application/json",將json對象改成json字符,在參數前面加上@RequestBody,可是傳遞到add方法里之后,卻發現還是個空,不僅里面的list沒值,連那些 long、String類型的也都沒有值,后經過幾個小時的研究發現,原來是配置spring 的json的類型里少寫了“application/json;charset=UTF-8”,導致沒有應用到json的類型轉換。
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
加上這個之后就ok了。
現在總結一下 springMVC傳遞參數的的方法。
1.ajax: 傳遞的參數請求頭里 Context-Type 的問題,我們若是用$.post的話,它默認是為application/x-www-from-urlencoded;charset=utf-8,網上說要改為“application/json”才能轉換,實際上你改成其他的也行,只要在xml的配置文件里包含它,那么它將會在轉換時會采用json包下的 MappingJacksonHttpMessageConverter類進行轉換;網上有人說spring只支持簡單類型的數組轉換,我想就是因為他們沒有配置MappingJacksonHttpMessageConverter類的使用,MappingJacksonHttpMessageConverter不屬於spring。
2.參數:平常我們使用訪問時,是直接將json對象傳遞給后台,而當我們的對象里包括數組時,就不能使用json對象,在jQuery里,post里的json對象會轉換成url參數模式傳遞,里面的對象的[、]、.也會被轉換成其他%%之類的,這種情況下spring 會將它們當做簡單的key=value值進行轉換,是不會進入到MappingJacksonHttpMessageConverter類中, 此時轉換時會報錯, 說 customForeignKeyRelations[0].id 沒有對應屬性,它們將 customForeignKeyRelations[0].id當成一個屬性來轉換;按照MappingJacksonHttpMessageConverter的要求,我們需要將json對象改為字符串傳遞,可以使用 JSON.stringify(jsonData)方法將json對象轉換成字符串(該方法在ie低級版本里沒有),此時我們在request的參數里可以看到它與我們平常傳遞的參數的不同。
太啰嗦了,簡單點:
1.定義Context-Typeapplication/json;charset=utf-8: ajax傳遞參數里更改,spring配置文件里 添加;
2.json對象傳遞時,改為字符串,通用方法為JSON.stringify(jsonData);
3.接收字符串對象:在參數前加 @RequestBody