Reputation: 741
Using spring and hibernate. How can I also send the messages in model too, with the response of that ajax method? I mean whats a better and most practiced way. Any sample code for the three said cases?:
1 returning String
@RequestMapping (value = "/task/delete/{taskId}", method=RequestMethod.GET)
public @ResponseBody String deleteTask(ModelMap model,
@PathVariable(value="taskId") String taskId
){
result = taskService.deleteTask(Long.parseLong(taskId));
if (result.getIsSuccessful()) {
model.put("successMessages", result.getMessageList());
System.out.println("task deleted ------------------");
return "success";//queued - send model message also (if needed)
}else{
System.out.println("task deletion failed ---------------");
model.put("errorMessages", result.getMessageList());
return "failure";//queued - send model message also (if needed)
}
}
2 returning null (no return object required)
@RequestMapping (value = "/attachment/download/{attachmentId}", method=RequestMethod.GET)
public String downloadFile( HttpServletResponse response,
@PathVariable(value = "attachmentId")Long attachmentId
)throws IOException{
result = taskService.getAttachmentById(attachmentId);
if(result.getIsSuccessful()){
Attachment attachment = (Attachment)result.getObject();
File file = new File(attachment.getPath());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
byte[] ba = FileUtils.readFileToByteArray(file);
response.getOutputStream().write(ba);
}
return null;
//queued - send model message also (if needed)
}
3 returning a full object.
@RequestMapping (value = "/task/create/{parentBoxId}/{taskTitle}/{taskDescription}", method=RequestMethod.GET)
public @ResponseBody Tasks createTask(ModelMap model,
@PathVariable(value="parentBoxId") String parentBoxId,
@PathVariable(value="taskTitle") String taskTitle,
@PathVariable(value="taskDescription") String taskDescription
){
Tasks taskToBeReturned = null;
Tasks task = new Tasks();
task.setTitle(taskTitle);
task.setDescription(taskDescription);
Boxes parentBox = (Boxes)( boxService.getBoxById(Long.valueOf(parentBoxId)) ).getObject();
taskService.setParent(task, parentBox);
result = taskService.save(task);
if(result.getIsSuccessful()){
model.put("successMessages", result.getMessageList());
Tasks savedTask = (Tasks)result.getObject();
System.out.println("box saved title was " + savedTask.getTitle());
taskToBeReturned = new Tasks();
taskToBeReturned.setTitle(savedTask.getTitle());
taskToBeReturned.setId(savedTask.getId());
taskToBeReturned.setDescription(savedTask.getDescription());
}else{
model.put("errorMessages", result.getMessageList());
}
return taskToBeReturned; //queued - send model message also (if needed)
}
Upvotes: 2
Views: 647
Reputation: 10649
I think that the best way to do this is to use a structure like an Hashmap
. I'm not sure I really understand what you are trying to do in your "returning null" example, but I think you should probably not returning null from an AJAX
call, you should probably return something like the error, it depend on the use case. This can be done in the same way as in the following examples for the other use cases.
Returning a String
@RequestMapping (value = "/task/delete/{taskId}", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> deleteTask(ModelMap model, @PathVariable(value="taskId") String taskId) {
Map<String, Object> result = new HashMap<String, Object>();
result = taskService.deleteTask(Long.parseLong(taskId));
if (result.getIsSuccessful()) {
model.put("successMessages", result.getMessageList());
System.out.println("task deleted ------------------");
result.put("result", "success");
result.put("model", model); // Add the model if needed
}else{
System.out.println("task deletion failed ---------------");
model.put("errorMessages", result.getMessageList());
result.put("result", "failure");
result.put("model", model); // Add the model if needed
}
return result;
}
Returning an Object
@RequestMapping (value = "/task/create/{parentBoxId}/{taskTitle}/{taskDescription}", method=RequestMethod.GET)
public @ResponseBody Map<String, Object> createTask(ModelMap model,
@PathVariable(value="parentBoxId") String parentBoxId,
@PathVariable(value="taskTitle") String taskTitle,
@PathVariable(value="taskDescription") String taskDescription) {
Map<String, Object> result = new HashMap<String, Object>();
Tasks taskToBeReturned = null;
Tasks task = new Tasks();
task.setTitle(taskTitle);
task.setDescription(taskDescription);
Boxes parentBox = (Boxes)( boxService.getBoxById(Long.valueOf(parentBoxId)) ).getObject();
taskService.setParent(task, parentBox);
result = taskService.save(task);
if(result.getIsSuccessful()){
model.put("successMessages", result.getMessageList());
Tasks savedTask = (Tasks)result.getObject();
System.out.println("box saved title was " + savedTask.getTitle());
taskToBeReturned = new Tasks();
taskToBeReturned.setTitle(savedTask.getTitle());
taskToBeReturned.setId(savedTask.getId());
taskToBeReturned.setDescription(savedTask.getDescription());
}else{
model.put("errorMessages", result.getMessageList());
}
result.put("task", taskToBeReturned);
result.put("model", model); // Add the model if needed
return result;
}
Upvotes: 1