Spring message in JSTL Tag - spring

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.

Related

spring form:form data-binding inside c:forEach

I would like to get some data-binding working the following way:
<c:forEach var="form" items="${forms}" varStatus="status">
<form:form modelAttribute="form">
<form:input path="key"/>
</form:form>
</c:forEach>
But the problem is, I think that the model attribute can't be a dynamic reference or at least I don't know how to express that.
forms is a collection of objects which have an attribute key.
I found many other solutions, which for instance use the status variable like
<form:input path="${forms[status.index].key}"/>
but i need to reference "key" directly instead of this array access prefix.
any ideas how to achieve that?
Try this,
<form:form modelAttribute="${form}">
<form:input path="${form.key}"/>
</form:form>
Also, if you are using the Map collection then you can access the value using ${form.value}.
In your code <form:form modelAttribute="form"> express string form as modelAttribute name and not the actual value which you wish to bind as modelAttribute.

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.

Spring binding errors: display only some messages in JSP

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]')}

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

Resources