1

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>

1 Answer 1

3

Finally referring to some articles, I could find a solution using RedirectView and redirectAttributes.setFlashAttributes().

Following is the Controller(Spring MVC) function:

@RequestMapping(value = "delete", method = RequestMethod.GET)
public RedirectView deleteForm544(@RequestParam("id") final Long inForm544Id,
        RedirectAttributes redirectAttributes) {

    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());
    }

    RedirectView redirectView = new RedirectView();
    redirectView.setContextRelative(true);
    redirectView.setUrl("filter_view");
    redirectAttributes.addFlashAttribute("alert", alertObj);

    return redirectView;
}

Following is the view (Thymeleaf) code. [NOTE: I am getting the alert response to a JavaScript variable]

<script th:inline="javascript">

     /*<![CDATA[*/
    var alertObj = ([[${alert}]]);
    /*]]>*/

</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.