上次是編輯數據,進行回顯。這次繼續說編輯數據。在編輯完成後,點擊保存將數據保存到數據庫。
之前的做法是通過ajax,將表單上的數據異步傳動到後臺然後調用後臺方法,將數據更新到數據。那如果窗體上的數據很多,ajax調用後臺controller的方法是,就需要定義多個變量進行傳輸。
這裏對傳遞的變量,封裝了一個方法,利用map傳輸。
form.jsp-js=
function save(){
var parameterMap = getFormParameterMap();
var jobtypename = $.trim(parameterMap["jobtypename"]);
if(jobtypename == "" || jobtypename == null){
alert('<spring:message code="syspages.system.datajobtype.noNull"/>');
return;
}
$.ajax({
type: "POST",
url: getRootPath() + "/dataJobtype/save.do;"+ $.now(),
data:parameterMap,
dataType: "json",
success: function(data){
if(data.message=="<spring:message code="syspages.system.datajobtype.saveSuccess"/>"){
alert('<spring:message code="label.saveSuccess"/>');
}else{
// alert('<spring:message code="label.saveError"/>');
alert(data.message);
$('#jobtypename').val("");
$("input[name=jobtypename]").focus();
return;
}
try{
var indexContentWindow = getIndexContentWindow();
var modalWindowObj = indexContentWindow.modalWindowObj;
modalWindowObj.window('close');
}
catch(e){
}
var indexContentWindow = getIndexContentWindow();
try{
var modalWindowOpener = indexContentWindow.modalWindowOpener;
modalWindowOpener.location.reload();
}
catch(e){
}
}
});
}
function getFormParameterMap(){
var parameterMap = {};
parameterMap.visitURl = window.document.location.pathname;
var inputs = document.getElementsByTagName("INPUT");
for(var i=0; i<inputs.length; i++){
var input = inputs[i];
if(input.type.toUpperCase()=="TEXT" || input.type.toUpperCase()=="PASSWORD" || input.type.toUpperCase()=="FILE"){
parameterMap[input.name] = input.value;
}
else if(input.type.toUpperCase() == "CHECKBOX"){
parameterMap[input.name] = input.checked;
}
else if(input.type.toUpperCase() == "RADIO"){
if(input.checked){
parameterMap[input.name] = input.value;
}
}
}
後臺controller代碼:
@RequestMapping("/save.do")
@Override
@ResponseBody
public Object save(HttpServletRequest request) {
String id = CommonUtil.toNull(request.getParameter("id"));
String jobtypename = CommonUtil.toNull(request.getParameter("jobtypename"));
String description = CommonUtil.toNull(request.getParameter("description"));
String drStr = CommonUtil.toNull(request.getParameter("dr"));
Integer dr = null;
if (drStr != null) {
dr = Integer.valueOf(drStr);
}
String ts = CommonUtil.toNull(request.getParameter("ts"));
DataJobtype dataJobtype = null;
List<DataJobtype> dataJobtypeList= null;
dataJobtypeList = dataJobtypeService.queryByName(jobtypename);
try {
if (id == null) {
if(dataJobtypeList.size()>0){
ResourceBundleMessageSource messageSource=BeanFactory.getBean(ResourceBundleMessageSource.class);
String str = messageSource.getMessage("syspages.system.datajobtype.alreadyExist", null, Locale.getDefault());
return createSuccessMessage(str).toString();
}else{
dataJobtype = new DataJobtype();
dataJobtype.setJobtypename(jobtypename);
dataJobtype.setDescription(description);
}
} else {
if(dataJobtypeList.size()==1){
if(!id.equals(dataJobtypeList.get(0).getId()) ){
ResourceBundleMessageSource messageSource=BeanFactory.getBean(ResourceBundleMessageSource.class);
String str = messageSource.getMessage("syspages.system.datajobtype.nameConflict", null, Locale.getDefault());
return createSuccessMessage(str).toString();
}
}
dataJobtype = dataJobtypeService.queryEntityById(DataJobtype.class, id);
}
BeanUtils.populate(dataJobtype, request.getParameterMap());
dataJobtype.setId(id);
dataJobtypeService.saveEntity(dataJobtype);
// return createSuccessMessage("操作成功!").toString();
ResourceBundleMessageSource messageSource=BeanFactory.getBean(ResourceBundleMessageSource.class);
String str = messageSource.getMessage("syspages.system.datajobtype.saveSuccess", null, Locale.getDefault());
return createSuccessMessage(str).toString();
} catch (Exception e) {
e.printStackTrace();
return createErrorMessage(e.getMessage()).toString();
}
}
通過js封裝getFormParameterMap這樣一個方法,將編輯窗體中的數據拿到放到map裏,傳到後臺。不過值得注意的是,同樣不適用於easyui的控件,通過getFormParameterMap方法,可以看到,通過input拿到了各個控件的值。所以,如果想拿到easyui控件的值,留待後期的改進。