Reputation: 3510
I have an application that displays, edits and deletes Accounts.
displayAccounts.jsp
and is invoked by /accounts
urleditAccount.jsp
and is invoked by /accounts/{id}/edit
where id
is the Primary Key to the Account to be edited.@RequestMapping
method in the Controller, and is invoked by /accounts/{id}/delete
Every action, on success, redirects to the Display page.
Once an operation (Edit/Delete) has been successfully performed, I want to send a success message that can be displayed on the Display page. How can I accomplish this?
Since I dont have a view for the Delete operation, I want to display the Errors in the Display page too.
Please help. Thanks in advance.
Upvotes: 2
Views: 7033
Reputation: 3889
Spring provide ModelMap type object, you can put your data in ModelMap type object.This object is accessible throughout the application. Here you can put a String type variable having value either Success or Failure (based on condition).Later on you can access this model object from jsp page.here is the example.
@RequestMapping(value="/accounts/{id}/edit ")
// public String yourMethod(ModelMap model)
public String yourMethod(HttpServletRequest request,
HttpServletResponse response,ModelMap model){
if(someCondition){
String result = "Success";
model.addAttribute("result", result);
}
else{
String result="failure";
model.addAttribute("result", result);
}
// return "displayAccounts";
return new YourControllerClass().yourMethodforAccountDisplay(request, response,model);//this is where you specify account display method with appropriate argument
}
you can access result from jsp page as below -
${result}
Please enable viewresolver in Spring configuration file.
Upvotes: 1
Reputation: 4483
Use of RedirectAttributes
is what you need to achieve this. I assume that from the POST methods of EDIT and DELETE operation you are using "redirect:" to redirect to the DISPLAY view through its controller.
To achieve this you need to have RedirectAttributes attributes
as a parameter in the controller function. Then before the return statement you should add following line of code.
attributes.addFlashAttribute("successMsg", "Account Edit/Delete successfully");
or
attributes.addFlashAttribute("errorMsg", "Edit/Delete account is unsuccessful");
in case of error message.
And then on the displayAccounts.jsp page you just need to display the message with ${successMsg}
or ${errorMsg}
Once the message will be displayed and if you refresh the page then the message will not come up. It will be displayed to the user just once.
This is perfect for your scenario. Even I use this.
Hope this helps you. Cheers.
Upvotes: 8
Reputation: 66
If you want to pass some message (error/success) to display page from controller than may below code helps,
@Controller
@RequestMapping("/controllerPath")
public class editDeleteController {
@RequestMapping(method = RequestMethod.GET)
public String methodName(ModelMap model) {
model.addAttribute("message", "Message1");
model.addAttribute("moremessage", "Message2");
return "viewName";//veiwName here in your case it is displayPage
}
}
In display page you will get it by,
<h1>Message : ${message}</h1>
Explain in details if this will not work for you.
Thank you
Okay I think i got your point do some think like this,
@RequestMapping(method = RequestMethod.GET)
public String editDelete(ModelMap model) {
if(success)
{
model.addAttribute("result", "Success");
return new ModelAndView("redirect:/displaytagView");//Chaining your controller so it will refresh your Accounts
}else{
model.addAttribute("result", "Error");
return "displayPage";
}
}
Chaining controller to displayPage Controller so it shows refreshed data. Try this hope its work
Upvotes: 1
Reputation: 8971
In Controller method in edit and delete method, write a message string into Model as
Edit
model.addAttribute("message", "Record Edited Sucessfully");
model.addAttribute("accountList", accountList);
Delete
model.addAttribute("accountList", accountList);
model.addAttribute("message", "Record Deleted Sucessfully");
and in your displayAccounts.jsp write code to display this message as
<div>
<span>${message}</span>
</div>
Upvotes: 2
Reputation: 6794
If your EDIT operation opens different view, then you can pass the message from Controller to the view using following
ModelAndView mav = new ModelAndView();
and then set the message object as
mav.addObject("message", "EDIT sucessfully completed");
and in last, set the view name
mav.setViewName("views/afterEditOperation/");
You can use the message directly on your view as ${mesasage}
Upvotes: 2
Reputation: 6794
You can use AJAX in this case . With this, you can send your action from the same page to Controller for delete operation and get the message from controller on the same page to show sucesss/error message of delete operation from controller.
Upvotes: 1