How to check conditionally an empty validation error message from Spring in JSP? - spring

I have a simple question. We can display error messages generated by validators of Spring something like the following.
<form:errors path='edId' cssStyle='font-weight: normal; color: red;'/>
I'm displaying such messages using some HTML like <ul><li></li></ul> and CSS. Therefore, even though there is no error, a blank bullet is displayed unnecessarily.
I therefore need to check conditionally on the JSP page whether the message is actually blank or not, something like,
<c:if test="${not empty edIdError}">
<form:errors path='edId' cssStyle='font-weight: normal; color: red;'/>
</c:if>
But testing this if condition requires storing the error message into some variable on JSP like <c:set var="edIdError"/> (as an example) which doesn't seem possible to me. Is it possible? Is there any other way to do this?
Anyway, I want to show the message with some HTML and CSS only when it is not empty. I'm using Spring 3.2.0.
And yes, I have a different scenario and cannot use this approach.

I have forgotten a simple thing about the <c:set> JSTL tag. It's a container tag. Therefore, it is obviously possible to enclose the <form:errors> tag within it like the following.
<c:set var="edIdError"><form:errors path='edId'/></c:set>
It sets the variable edIdError to the actual error message from Spring. So, it is now possible to check this conditionally whether it is null (and empty) or not like,
<c:if test="${not empty edIdError}">
<form:errors path='edId' cssStyle='font-weight: normal; color: red;'/>
</c:if>
There is no need to use the value attribute of the <c:set> tag in this scenario. I could use HTML and apply CSS within the <c:if> conditional block as I needed.

Related

How to mix href within jstl code

When I use the below jstl code
<a href="http://mysite.com?id="<c:out value="${myid}"/>/><c:out value="${myid}"/></a>
the output is :
"1234"
The value 1234 corresponds to the variable value of myid but the url being generated is
"http://mysite.com?id=" so no value for myid is being generated as part of the href.
How can I amend the href so that entire href is displayed :
"http://mysite.com?id=1234"
instead of :
"http://mysite.com?id="
Ultimately, JSP/JSTL generates HTML. You're familiar with basic HTML, right?
Look closer at the generated HTML output by rightclick, View Source in browser. You'll see:
<a href="http://mysite.com?id="1234/>1234</a>
Is that valid HTML? No, you're closing the attribute value too soon with " at wrong place and you're closing the tag too soon with />. Look, the Stack Overflow HTML syntax highlighter also got confused. Instead, it should have been:
1234
Fix the HTML generator (i.e. the JSP/JSTL code) accordingly so that it generates the desired HTML:
<c:out value="${myid}"/>
Unrelated to the concrete problem, the <c:out> is only helpful in preventing XSS attack holes when redisplaying user-controlled input and actually the wrong tool to inline URL parameters. If you can guarantee that ${myid} is always a number (because it's a Long or Integer), you can even just leave it entirely out, making the code prettier to read:
${myid}
If the ${myid} is however not a guaranteed to be a number (because it's a String), then you should use <c:url> and <c:param> to properly URL-encode it:
<c:url value="http://mysite.com" var="myURL">
<c:param name="id" value="${myid}" />
</c:url>
<c:out value="${myid}" />
<c:url> tag is used to create an url. It is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page.
<c:param> tag may used as a subtag of to add the parameters in the returned URL. Using these parameters encodes the URL.
<c:url value="http://mysite.com" var="myURL">
<c:param name="id" value="${myid}" />
</c:url>
<a href="${myURL}" />${myURL}</a>
Read more from here.

Spring MVC checkbox tag not posting changes made

I have trouble using spring mvc checkbox tag. I have a BookmarkMapping object which in turn has List object. Inside Folder Mapping there are three attributes like id, name and isMapped (boolean). It is this isMapped property based on which I intend to show the checkbox.
So in my page I get modelAttribute as 'bookmarkMapping'. inside which there is a list of folderMapping. Each of the item inside folderMapping is isMapped set as true or false for my checkbox checked attribute.
I am trying this
<c:forEach var="folderMapping" items="${bookmarkMapping.folderMapping}" varStatus="i">
<form:checkbox path="folderMapping[${i.count-1}].isMapped" label="${folderMapping.folderName}"/>
</c:forEach>
This displays the checkboxes fine in html but when posted it is nor reflecting changes of checkboxes as true /false in posted objects. It always shows false.
Set the value attribute of the checkbox tag.
<c:forEach var="folderMapping" items="${bookmarkMapping.folderMapping}" varStatus="i">
<form:checkbox path="folderMapping[${i.count-1}].isMapped"
value = "${folderMapping[${i.count-1}].isMapped}"
label="${folderMapping.folderName}"/>
</c:forEach>
Have you tried the <form:checkboxes /> tag? I think this is what you need here, maybe could solve your problem. Try something like this, replacing your <c:forEach /> tag:
<form:checkboxes path="folderMapping" items="${allCheckboxValues}" itemLabel="folderName" itemValue="isMapped"/>
You need to prepopulate allCheckboxValues in your controller with all possible values.
Check out spring documentation about this tag for more help and the TLD documentation.

How can I use Spring MVC "form" tag instead of my "input" tags?

What I have:
I have a generic JSP page that is used throughout my application for displaying certain entities. The code that I am interested in goes like this:
<form:form modelAttribute="object"/>
<core:forEach items="${sections}" var="section" varStatus="itemStat">
<core:forEach items="${section.fields}" var="fieldDef">
<form:input path="${fieldDef.fieldName}"/>
</core:forEach>
</core:forEach>
<form:form>
For each section, and for each field in that section, I have an input having the path fieldName, which is what I want to display from each field.
What I want:
I would like instead of the input to be a simple text, like a label.
What I have tried:
I am most certain that I can do it somehow with <form:label> but I can't really make it work. Making a <form:label path="${fieldDef.fieldName}" /> just tells the browser for which field I need the label, but doesn't get the actual value from it.
I have also tried something like ${object.fieldDef.fieldName}, but in order for this to work I would have to first analyze the value of ${fieldDef.fieldName}, which would give me the name of the column, and then do a ${object.column}, but column being a variable I haven't been able to make this work in any way.
Alternative:
An alternative would be to just make the inputs as disabled and remove the border with CSS, but that would be a dirty way and from what I saw it is also tricky for IE different versions. I am sure that I can handle it directly.
I am a little intrigued by the fact that <form:input path="..."> puts into the input what it finds corresponding to that path (same goes for other form elements), but with label it works different.
So, what I want is basically simple, but I haven't managed to find a way. If someone could shed some light, that would be great. Thanks in advance !
You could look into the spring bind tag. I haven't tried using it before but this may work for you, in place of the input tag
<spring:bind path="fieldDef.fieldName">
${status.value}
</spring:bind>
reference: http://static.springsource.org/spring/docs/1.1.5/taglib/tag/BindTag.html
Instead of
<form:input path="${fieldDef.fieldName}"/>
use
<c:out value="${fieldDef.fieldName}"/>
It would display whatever value is there instead of creating a input field. Hope this helps you. Cheers.
Using the spring form tab, one option would be to use
<form:input disabled="true" path="${fieldDef.fieldName}"/>
To further make it not look like an input you could use CSS to style it to your preference.
Some css styles you could use:
background-color:#EEEEEE;border: 0px solid;
Update:
You could look into the spring bind tag. I haven't tried using it before but this may work for you, in place of the input tag
<spring:bind path="fieldDef.fieldName">
${status.value}
</spring:bind>

Read-only (print) version of JSP form using Spring 2.0.x and form tags?

Is there a way to easily create a readonly version of JSP form in Spring?
i.e., I have a command object that's filled and if I show it as a form it works great, all the selects and radiobuttons get bound properly. However, my command object only holds id's of properties, not labels (i.e. and id from a select or a radiobutton list that gets bound on JSP load).
What I'd like to do is make a read only version where there'd be just a label - value list, without html objects such as inputs, selects and such.
So basically, in an edit version, there'd be something like
<form:select path="type.id" id="type">
<form:options items="${types}" itemLabel="name" itemValue="id"/>
</form:select>
but in the read only version I'd like to be able to automatically print only the exact type.name that got selected, i.e.
<c:out value="${commandName.type.name}"/>
Is there such a possibility, or do I have to mess with this in controller?
Ok so I guess there's no elegant way of matching IDs and values from model with IDs in command. Instead of doing extra work in controller, I matched the IDs on JSP, i.e.
<c:forEach var="type" items="${types}">
<c:if test="${type.id == commandName.type.id}">
<c:out value="${type.name}"/>
</c:if>
</c:forEach>
It's a bit extra work, but I'd rather do this than have a number of iterations over a List in my controller.

spring validation: cleanest way to makeup accompanying labels of the validated input

I'm validating the input field that's bound to path. I'm using hibernate-validator 4 for this.
Now I'd like to highlight the age label so it pops out of the page (bold, red colour etc.).
However I'm wondering what the cleanest way to do this is.
<spring:hasBindErrors name="*"/> seems to be for the whole form object instead of for a specific field. Any input is appreciated.
Spring provides special jsp tags for forms, which support this task (highlighing in case of error):
For example this jsp
...
<%# taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
...
<form:form method="post"
commandName="myCommand">
<form:input path="name"
cssClass="normalLayout"
cssErrorClass="normalLayout error"/>
<form:errors path="name"
cssClass="errorMessage"/>
</form:form>
...
In this case: the input field uses the css class "normalLayout" if every thing is ok, and the css classes "normalLayout" and "name" if there is a validation error for the field.
form:errors is to print the error message generated while validation.
#see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib

Resources