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

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).

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>

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="<p/>" />

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.

there's a way to reference the error like
instead of
<form:form commandName="object">
<form:errors path="field"/>
</form:form>
best

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" %>

Related

How to get values for dynamic form field names using JSTL and EL

I have 8 checkboxes and 1 button, when the user check any of the checkboxes and click the button, I want to check if any of the checkbox is checked and display it in another .jsp
I have already referred to few similar questions with no luck so far. So i tried to manage with my own logic
First.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<input type="checkbox" id="seat" name="seat${loop.index}" value="seat${loop.index}" >
<label for="seat">Seat${loop.index}</label>
</c:forEach> <br> <br>
<input type="submit" value="Save" name="savebtn">
Second.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<c:if test="${not empty param.seat[loop.index]}">
<c:out value="${param.seat1} is booked"/>
</c:of>
</c:forEach>
I have 2 problems regarding the code above :
i can't get loop.index value inside the param $param.seat[loop.index] doesn't work
And even if i try to do it manually, i can only get value from seat1. I can't get value from the rest ( seat2, seat3, etc).
${param.seat[loop.index]} implies that seat is a collection, which it is not (it probably does not even exist). You are after ${param.seatX}, where you can dynamically set X. You can do that by creating a variable containing the parameter name first:
<c:set var="seatVarName" value="seat${loop.index}"/>
Now you can use this variable to get the parameter value from the implicit EL object:
${param[seatVarName]}
See also:
JSP expression language and dynamic attribute names

Output not visible jstl c:out not showing on browser

Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

Thymeleaf How to check current URL?

i was familiar with JSTL before and thymeleaf is something new to me.
What i want is to check if the current URL is the index page (/index), if so then make visible a div.
Here is a JSTL equivalent example
<c:set var="url" value="${ pageContext.request.requestURI }" />
<c:if test="${url=='/example/WEB-INF/views/inbox.jsp'}">
...
</c:if>
Try this:
<div th:if="${#httpServletRequest.requestURI == '/example/WEB-INF/views/inbox.jsp'}">
some content
</div>

Need to Combine Spring Binding Errors and Custom Exceptions into Same DIV

I need to display SpringMVC's Form errors, as well as any custom "Exception" messages that I store in the request, in the same DIV on my page.
Right now they're in separate DIV's:
<!-- Validation Errors -->
<spring:hasBindErrors htmlEscape="true" name="model">
<c:if test="${errors.errorCount gt 0}">
<div class="errors_div clsErrorTblBorder">
<c:forEach items="${errors.allErrors}" var="error">
<div class="errors clsWhiteBack blue">
<myForm:message messageLinkClass="errorLink"
error="${error}" />
</div>
</c:forEach>
</div>
</c:if>
</spring:hasBindErrors>
<!-- Exceptions -->
<c:if test="${fn:length(requestScope.exceptions) > 0}">
<div class="errors_div clsMsgTblBorder">
<c:forEach items="${requestScope.exceptions}" var="exception">
<div class="messages clsWhiteBack">
${exception}
</div>
</c:forEach>
</div>
</c:if>
The shared condition for an Error DIV should be,
${fn:length(requestScope.exceptions) || errors.errorCount gt 0}
But it's impossible to test this condition, because:
You don't get the errors var. until you do <spring:hasBindErrors>
Anything inside <spring:hasBindErrors> only applies if you have
binding errors. If you don't (e.g. if you only have a custom
Exception message) its logic won't execute, so you can't move the
custom "exceptions" check inside it.
I got some clues from this thread: How to access Spring 3 MVC validator results in JSP without using form taglib , and here's the working result:
<c:if test="${fn:length(requestScope.exceptions) > 0 ||
requestScope['org.springframework.validation.BindingResult.model'].hasFieldErrors()}">
The 2nd part of this condition checks for Binding Errors even prior to the tag Spring:hasBindErrors.

passing jslt value to javascript

I trying to a pass a jslt value to javascript but the value is not getting rendered.
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status"> <a href=javascript:popPR('${pr.url}') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>
if directly type the pr.url value the popup window gets opened but if i pass the through jstl it does not call the popup.
Can anyone please suggest how to fix it.
Thanks
Try using <c:out> to output the url :
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status">
<a href=javascript:popPR('<c:out value="${pr.url}"/>') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>

Resources