1.spring的RequestParam註解接收的參數是來自於requestHeader中,即請求頭,也就是在url中,格式爲xxx?username=123&password=456,html
而RequestBody註解接收的參數則是來自於requestBody中,即請求體中。java
2.若是是從js經過ajax請求傳送json字符串到controller層,在接受數據的形參用@RequestBody註解時,ajax請求時內容類型爲contentType:"application/json",且將要傳送的數據從json對象轉爲json字符串data: JSON.stringify(param);ajax
3.若是在ajax的請求地址中帶有參數,則在controller層中能夠加@RequestParam註解獲取參數,經常使用於get請求,也能夠不加,加的話由於@RequestParam是key-value類型,那地址的參數中必須有形參中的key值spring
4.ajax請求參數中也能夠有數組類型,如例子中的taskTemplateValueListjson
5.遇到複雜的數據格式,通常來講仍是json比較好處理,即用@RequestBody接收數組
js:session
/**
* 點擊預覽按鈕
*/
function smsPreLook() {
var templateId = $('#templateId').val();
var templateContent = $('#templateContent').val();
if (templateContent == null || templateContent == '') {
msgInfoModal("提示",'請編輯文本內容');
return;
}
//關鍵字
var taskTemplateValueList=[];
var checkValue=true;
$("tr[name='taskTemplateValueListTr']").each(function(){
var paramName=$(this).find('input[name=paramName]').val();
var paramValue=$(this).find('input[name=paramValue]').val();
var taskTemplateValue={};
taskTemplateValue.paramName = paramName;
taskTemplateValue.paramValue = paramValue;
taskTemplateValueList.push(taskTemplateValue);
if (paramValue == null || paramValue == '') {
checkValue=false;
}
});
if (!checkValue) {
msgInfoModal("提示",'請輸入關鍵字的替換內容');
return;
}
var smilResourceId=$('#smilResourceId').val().trim();
var isdetail=false;
var param = {
smilResourceId: smilResourceId,
smsMsgContent:templateContent,
templateContent:templateContent,
templateId:templateId,
taskTemplateValueList:taskTemplateValueList//數組類型
};
$.ajax({
url: WEB_ROOT + "/mall/task/preLook?isdetail="+isdetail,
type: "html",//返回值類型
data: JSON.stringify(param),//將要傳送的數據從json對象轉爲json字符串
contentType:"application/json",//定義數據內容類型爲"application/json"
method: "post",
success: function (page) {
$("#preLook").html(page);
$('#preLookModal').modal('show');
}
});
}
java:app
/** * 預覽顯示 */ @RequestMapping(value = "/preLook")
//若是在isdetail參數前有@RequestParam(value="isdetail",required = true),則在從js傳來的請求地址後的參數必須有isdetail做爲key值的參數,required則控制該參數的value值是否必傳
public String examineInit(Model model, @RequestBody TaskVO taskVO, Boolean isdetail) throws Exception {
AuthStaffVO staff = StaffUtil.getStaffVO(session);
if (isdetail && taskVO.getId() != null) { taskVO = itTaskSV.selectTaskVOById(taskVO.getId()); } else { //校驗文本內容的權限(部分自定義的話,只能用模板;徹底自定義能夠不用模板) if (TaskConstant.TASK_RIGHT_1.equals(staff.getTaskRight())) { //必須使用模板 if (taskVO.getTemplateId() == null) { return null;// R.failure("請選擇模板~") } setTaskSmsMsgContentByTemplate(taskVO); } else if (TaskConstant.TASK_RIGHT_2.equals(staff.getTaskRight())) { //能夠不用模板 if (taskVO.getTemplateId() != null) { setTaskSmsMsgContentByTemplate(taskVO); } else { taskVO.setSmsMsgContent(taskVO.getTemplateContent()); } } else { return null;//R.failure("您當前沒有權限進行此操做哦~"); } } List<TaskVO> taskVOList = new ArrayList<TaskVO>(); try { taskVOList = itTaskSV.selectTaskVOList(taskVO); } catch (Exception e) { log.error("短信任務管理(後臺)--數字短信預覽異常", e); } model.addAttribute("taskVOList", taskVOList); return "homeMain/myCenter/task/mall-smsPreLook";}