I am using Spring MVC and thymeleaf in my web application.
In one of the scenario I have a controller which do a redirecting at end of its functionality. I need to pass a parameter (actually a success/fail alert message) to my front end. How can I implement this behavior.
I could know that model.addAttribute() cannot be implemented when there is a redirection.
Following is the controller code.
@RequestMapping(value = "delete", method = RequestMethod.GET)
public ModelAndView deleteForm544(@RequestParam("id") final Long inForm544Id) {
logger.info("Hit the /Form544/delete ");
logger.info("Change Status Deleted of Form 544 ID : " + inForm544Id);
JSONObject alertObj = new JSONObject();
try {
form544Service.setStatusAsDeleted(inForm544Id);
alertObj.put("type", "success");
alertObj.put("msg", "Successfully deleted Form 544 with ID Number " + inForm544Id);
} catch (Exception e) {
logger.error("Error occured " + e);
alertObj.put("type", "fail");
alertObj.put("msg", "Form 544 deletion failed. Due to " + e.getMessage());
}
return new ModelAndView("redirect: filter_view");
}
Following is the view(thymeleaf) code. [Note: I need to get the value to a javascript variable]
<script th:inline="javascript">
/*<![CDATA[*/
var alertObj = ([[${alert}]]);
/*]]>*/
</script>