在我当前的spring项目中,插入和更新事件在我的控制器中通过以下方法处理:
@RequestMapping(value="cadastra", method=RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public String cadastra(@ModelAttribute("object") E object, BindingResult result, @RequestParam(value="file", required=false) MultipartFile file, @RequestParam(value="icone", required=false) MultipartFile icone, @RequestParam(value="screenshot", required=false) MultipartFile screenshot[]) throws Exception {
if (serv.cadastra(object, file, icone, screenshot) != null)
return "";
else
throw new Exception();
}
@RequestMapping(value="altera", method=RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public String altera(@ModelAttribute("object") E object, BindingResult result, @RequestParam(value="file", required=false) MultipartFile file, @RequestParam(value="icone", required=false) MultipartFile icone, @RequestParam(value="screenshot", required=false) MultipartFile screenshot[]) throws Exception {
if (serv.altera(object, file, icone, screenshot) != null)
return "";
else
throw new Exception();
}
并通过此方法在服务类中:
@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")
@Transactional
public E cadastra(E e, MultipartFile file, MultipartFile icone, MultipartFile[] screenshot) {
return dao.persist(e);
}
@PreAuthorize("hasPermission(#user, 'altera_'+#this.this.name)")
@Transactional
public E altera(E e, MultipartFile file, MultipartFile icone, MultipartFile[] screenshot) {
return dao.merge(e);
}
我希望我的控制器返回视图,而不是空字符串,保存在数据库中的数据,以允许我使用此信息更新视图中的某些字段,即jquery。将使用此信息的代码之一是:
$(".permissao").each(function(){
var form = this;
var type = $(form).data("type");
$(form).ajaxForm(function(data){
if(data == "") {
$(form).find(".alert-success").find(".alert-text").text("Solicitação efetuada com sucesso");
$(form).find(".alert-success").show();
if(type == "cadastra") {
console.log('cadastrado');
var id = 0;
var nome = "...";
var group = $('<div class="input-group" id="grupo_'+id+'">');
group.append('<span class="input-group-addon"></span>');
group.append('<input type="text" class="form-control" disabled="disabled" value="'+nome+'">');
group.append('<span class="input-group-btn"></span>');
$(".column").append(group);
add_input(id, 'plus');
}
if(type == "altera") {
console.log("alterado");
var id = 0;
$("#detail-"+id).empty();
}
if(type == "remove") {
console.log('removido');
var id = $(form).find("input[name='id']").val();
$("#grupo_"+id).remove();
$("#altera-"+id).remove();
$("#remove-"+id).remove();
$("#detail-"+id).remove();
}
} else {
var $temp = $('<div/>', {html:data});
$(form).find(".alert-danger").find(".alert-text").text( $temp.remove('head').html() );
$(form).find(".alert-danger").show();
}
});
});
上面的变量id
和nome
应该接收对象中具有相同名称的字段的相应值。
任何人都可以指出我这样做的方向吗?