By Beata


2020-05-20 13:38:18 8 Comments

I have a web page that contains form with two inputs for dates and button. After clicking a button: (method that validates dates and throws exceptions if something is wrong and generates csv file to download)

    @PostMapping(value = "/report", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public String generateCsvFile(@ModelAttribute("reportRequest") @Valid ReportRequest reportRequest,
                                  BindingResult result, Model model, HttpServletResponse response) {
        if (result.hasErrors()) {
            model.addAttribute("error", true);
            return "home";
        }
        try {
            policyService.writeToCSV(reportRequest, response);
            response.setHeader("Refresh", "1; url = index");
        } catch (Exception e) {
            model.addAttribute("error", true);
        }
        return "home";
    }

in home.html file:

            <p th:if="${error}==true" style="color:red" th:text="#{new.formError}"></p>

When something goes wrong I can see error info, but when I put correct data and get download file window I would like error paragraph to disappear. Also Thymeleaf has a problem with that when I am returning String - home page.

What I would like to achieve? When something goes wrong to show error info. When dates are correct and someone will download file I would like for form and error message to clean.

What I tried? generateCsvFile to return void, but when there is exception it is redirecting to /report page; adding header to response with error true; response.setHeader("Refresh", "1; url = home");; model.addAttribute("error", false);

2 comments

@Abby Howe 2020-05-22 23:50:30

You might try using the conditional like this

<div class="alert alert-danger alert-dismissible" th:if="${errormessage}">
     <button type="button" class="close" data-dismiss="alert">&times;</button>
          <strong th:text="${errormessage}">Success!</strong> 
</div>

Then in your controller method signature use RedirectAttributes redirectAttributes with your redirectAttribute message.

redirectAttributes.addFlashAttribute("successmessage", "Add message here")

@SorcererApprentice 2020-05-20 14:45:20

You have to remember to set to false the error attribute.

@Beata 2020-05-20 17:22:04

I already tried that but it doesn't work. Page doesn't refresh.

Related Questions

Sponsored Content

32 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

21 Answered Questions

[SOLVED] Java: when to use static methods

15 Answered Questions

12 Answered Questions

24 Answered Questions

5 Answered Questions

[SOLVED] What are the -Xms and -Xmx parameters when starting JVM?

11 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Errors when trying to handle templates with Thymeleaf

1 Answered Questions

[SOLVED] Redirect after downloading the pdf in spring

Sponsored Content