0

From the thymeleaf guide, I have seen the following code snippet:

<ul th:if="${#fields.hasErrors('global')}">
<li th:each="err : ${#fields.errors('global')}" th:text="${err}">Input is incorrect</li>
</ul>

So, I m thinking if there is a way that I can pass error message from the Spring MVC controller and display the error somewhere on the page using the above code.

I have seen some online resources using BindingResult on the method signature as below.

bindingResult.rejectValue("username", "username.notvalid", "Username is not valid");

But what if the controller does not validate any objects? Will I still be able to use bindingResult?

I think I can also add error message as key pair into the model as below.

model.addAttributes("error","error message");

But, I want to know if there is some standard way of handling error in the Spring controller.

Should I use exception handling instead? But how can I pass error message back to the view?

Thanks

Updated use case:

For example, in method A, an error was detected as below.

public String methodA (Model m, RedirectAttributes model){
 if (error == true){
   model.addFlashAttribute("error.message","String");
   return "redirect:MethodB";
 }
}


public String methodB(Model m){
 if(m.containsAttribute("error.message"){
   //Way to pass the error to view page for display.
 }
}

Should the error be handled in such way? The reason it goes through the second action is because there are certain data needed to be displayed on the final view page.

mengjiann
  • 275
  • 3
  • 12

1 Answers1

0

Do not use exceptions for validation errors, use BindingResult, give a look to this question and javadoc.

You can also create an implementation like MapBindingResult, use it and set it in the request/model for the view.

A BindingResult is a standard Spring data structure that can be used to hold errors, is usually used when there could be more than one error or if you have view code for it; if you just have one single error you could also just set it in the model.

Since you use flash attributes, in methodB the error is already in the model, so it will be available to the view. See also Spring MVC: Passing a Model as an argument to a controller method VS instantiating it explicitly

public String methodB(Model m){
     ...
     return "myview";
}

Note that if you use a dot in attribute key (like error.message) you should use the right syntax in the view like, for JSP: model["error.message"] and not model.error.message

Community
  • 1
  • 1
Testo Testini
  • 2,200
  • 18
  • 29
  • what if the controller does not need validation, but it is to check if there is any flash attributes(String) from previous redirect controller? if the passed on flash attribute is not null, then the controller should include into bindingResult object and display it on the view as error message? – mengjiann Dec 04 '16 at 15:57
  • if there are validation errors I don't think you should redirect but just display the view http://stackoverflow.com/a/599105/1536382 – Testo Testini Dec 04 '16 at 18:28
  • It is not meant for validation errors but an error message passed from one controller to another and show it finally on the page. – mengjiann Dec 05 '16 at 04:59
  • Please check the above updated question for my use case. – mengjiann Dec 05 '16 at 06:48