th3an0maly
th3an0maly

Reputation: 3510

How can I display error in JSP without a <form:form>

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

In my Controller.java, I'm binding an error by doing:

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

But since I dont have a form in my jsp, I'm not able to access the error with:

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

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

Upvotes: 4

Views: 9445

Answers (5)

Ripudaman Singh
Ripudaman Singh

Reputation: 401

Check if Expression Language evaluation is enabled in JSP(By default it is disabled).If not add below code to enable it.

<%@ page isELIgnored="false" %>

Upvotes: 0

nasko.prasko
nasko.prasko

Reputation: 21

there's a way to reference the error like

instead of

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

best

Upvotes: 0

Puneet Garg
Puneet Garg

Reputation: 36

for any specific error set in your code like-

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

And show this error on jsp in this way:

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

This way you would get your error on jsp.

Upvotes: 0

sp00m
sp00m

Reputation: 48817

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>

Upvotes: 6

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

Associate global errors this way:

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

You can show the global errors in the jsp :

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

Upvotes: 3

Related Questions