How can I show an error in JSP without <form: form>

I have a jsp page with List<Object> as @ModelAttribute . However, there are no <form:form> tags on the page. All I do is print the contents of the List .

In my Controller.java I bind an error by doing:

result.rejectValue("", "NOT_LOGGED_IN", "You should Login first") ;

But since I don't have a form in jsp, I cannot access the error with:

<form:errors path="" /> <br/>

Please tell me how to access the error (or what I'm doing wrong).

+4
source share
5 answers

In your controller:

 model.addAttribute("errors", result.getAllErrors()); 

In your JSP:

 <c:forEach items="${errors}" var="error"> <%-- do want you want with ${error} --%> <c:out value="${error.defaultMessage}" /> </c:forEach> 
+5
source

Associate global errors as follows:

 result.reject("NOT_LOGGED_IN", "You should Login first") ; 

You can show global errors in jsp:

 <form:errors cssClass="error" delimiter="&lt;p/&gt;" /> 
+3
source

for any specific error in your code, for example -

 model.addObject("errorMsg","username/password failed"); 

And show this error on jsp as follows:

 <c:out value="${errorMsg}"/> 

This way you get your error in jsp.

0
source

is there a way to refer to the error as

instead

 <form:form commandName="object"> <form:errors path="field"/> </form:form> 

it is better

0
source

Check if expression language evaluation is included in the JSP (it is disabled by default). If you have not added the code below, to enable it.

<% @page isELIgnored = "false"%>

0
source

Source: https://habr.com/ru/post/1435066/


All Articles