您的位置:首页 > 其它

编辑数据-表单保存

2015-04-27 17:55 197 查看
上次是编辑数据,进行回显。这次继续说编辑数据。在编辑完成后,点击保存将数据保存到数据库。

之前的做法是通过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控件的值,留待后期的改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax 方法 表单