Spring binding errors: display only some messages in JSP - spring

In a JSP I'm listing all the binding/validation errors together:
<ul>
<c:forEach items="${status.errorMessages}" var="error">
<li><c:out value="${error}"/></li>
</c:forEach>
</ul>
However, I have some custom behavior and would like to ignore all the "typeMismatch" errors. Is there a way to do something like the following?
<c:if test="${not error.isTypeMismatch}">
<li><c:out value="${error}"/></li>
</c:if>
Since ${error} is just a plain old String, I don't know how I could determine something like this.
Thanks for your ideas!

Since errors are just strings, I don't see a way to separate type mismatch errors from others other then checking if the error actually contains some text expected to be in type mismatch errors.
${not error.contains('[some type mismatch text]')}

Related

In MVC passing a list from JSP to the Controller

I'm trying to get an example of passing a list from JSP to the Controller to work:
http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
the problem I have with this example is that I do not get the JSP /WebContent/WEB-INF/jsp/add_contact.jsp to work. He has the line:
<c:foreach items="${contactForm.contacts}" var="contact" varstatus="status">
and i'm getting errors with the varstatus variable. From where is he getting the values for this variable? He is using it as a list row index, but from where should the values come? I get the warning in eclipse that it is a not defined variable varstatus and if i still deploy it to tomcat, then i get the error that jstl foreach tag does not support more than one variable (as i already have the contact there).
The forEach tag and attributes are case sensitive.
<c:forEach items="${contactForm.contacts}" var="contact" varStatus="status">
<c:out value="${status.index}" />: <c:out value="${contact}" />
</c:forEach>
It's possible the error messages generated by Eclipse are misleading. It's also possible the error is originating from somewhere else on the page. If this does not resolve the problem, post the full JSP and stacktrace for better understanding.

How to put conditional statements in .jsp pages

I need to have a conditional statement added on a .jsp page that identifies all authenticated pages in order to insert a specific tracking code in place. I am not accustomed writing any jstl but I have this so far
<c:if test='${not empty authenticatedUser}'>
my tagging code
</c:if>
I would like to an extra condition in place that exclude a specific url
Use this -
<c:if test="${not empty authenticatedUser && url ne 'someUrl.html'}">
my tagging code
</c:if>
Single quotes is coming two times. That might be creating an issue.
EL expressions (the ${not empty authenticatedUser}) can contain logical expressions like:
<c:if test='${not empty authenticatedUser and url ne "someUrl.html"}'>
my tagging code
</c:if>

Spring message in JSTL Tag

According to this post from 3 years ago the only way to display a spring message in a jstl tag is to wrap it in a <c:set var="someVar"> which does "work" but it seems very far from ideal.
Fast forward 3 years, is this still the only way to handle this?
Here is my code
Works, but not "ideal"
<c:set var="closeMessage">
<spring:message code='lman.ilr.closeItemDetail'/>
</c:set>
<dsg:sidePanelContent closePanelText="${closeMessage}">
Doesn't work, returns a string of <spring:message code='lman.ilr.closeItemDetail'/>
<dsg:sidePanelContent closePanelText="<spring:message code='lman.ilr.closeItemDetail'/>">
The spring message tag, just as fmt:message, has a var attribute that can be used to store the message instead of displaying it.
It always helps to read the documentation.
Also, your wrong message probably comes from forgettin to declare the spring taglib at the top of your JSP.
In case for reference,
<c:choose>
<c:when test="${serviceVO.id eq 0}">
<spring:message code="label.service.createservice" var="buttonName"/>
</c:when>
<c:otherwise>
<spring:message code="label.updateservice" var="buttonName"/>
</c:otherwise>
</c:choose>
<c:out value="${buttonName}"> //Prints the desired value...
I think what you wanna do is.
<spring:message code='lman.ilr.closeItemDetail' var="closeMessage"/>
Then
<dsg:sidePanelContent closePanelText="${closeMessage}">
As above mentioned,
<spring:message code='lman.ilr.closeItemDetail' var="closeMessage"/>
<dsg:sidePanelContent closePanelText="${closeMessage}">
works, since tag has the attribute "var" for above purpose.
tested with spring boot app and it works.

Spring Framework - Display Warning Message as opposed to Error

I have Use Cases that require the User to be show a Warning message as opposed to an Error message. The user will be shown once and then they can proceed. My problem is since it's not a Validation error the flow goes thru and the operation is performed without displaying the message. Therefore the user performs an action, he is warned once and then let thru.
Am I missing something? I looked around but there doesn't seem to be the concept of a warning message in Spring. Thanks for your help or suggestion.
In my ignorance of Spring and Spring-MVC, I created a Messages class that contains lists of errors and messages. I store an object of this type in the request scope and reference it in my JSP somewhat like this:
<c:if test="${Messages.hasMessages}">
<c:forEach items="${Messages.messages}" var="message">
${message}<br/>
</c:forEach>
</c:if>
<c:if test="${Messages.hasErrors}">
<c:forEach items="${Messages.errors}" var="error">
${error}<br/>
</c:forEach>
</c:if>
In my actual JSP, I style it as needed and generally dump messages and errors in a <ul> list.

Struts2 ognl execute request method

I'm trying to use OGNL to evaluate if the session is valid, in order to show some information. For that I've the following JSP
[...]
<s:if test="request.isRequestedSessionValid()">
[...] (show user name, etc)
</s:if>
But it doesn't work. I've also tried "#request.isRequestedSessionValid()","%{request.isRequestedSessionValid()}" and "{request.isRequestedSessionValid()}", but I always get an error message target java.lang.NullPointerException: target is null for method isRequestedSessionValid or [OgnlValueStack] Could not find method [#request.isRequestedSessionValid()]. What am I doing wrong?
Thanks!
Okay, now that I've had a chance to look deeper into this I am tossing out my old answer. #request won't work because that is basically the equivalent of the JSP EL expression ${requestScope.requestedSessionIdValid}, but that property isn't a request scope property, its an actual property of the HttpServletRequest.
You can do this easily with JSP EL using:
<c:if test="${pageContext.request.requestedSessionIdValid}">
...
</c:if>
I don't know that there is an equally concise way to get at this with OGNL.

Resources