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.
Related
I am converting some plain HTML to using spring form tags.Although it looks like
spring form input does not have the attribute type. I was able to successfully
pass a hidden variable as follows:
<form:input type="hidden" name="displayId" id="displayIdentifier" path="displayIdentifier" value="${value1}"/>
Earlier the plain HTML was as follows:
<input type="hidden" name="displayId" id="displayIdentifier" value="${value1}"/>
I looked online and saw that the for:input does not have the type attribute, yet it seems to be working correctly.
The input tag is declared with
<dynamic-attributes>true</dynamic-attributes>
That allows passing dynamic attributes, not explicitely declared in the tag. The tag simply stores their name and value and writes them as is on the generated HTML input. That allows adding a type, or a data attribute, or any attribute you want to the tag.
See http://docs.oracle.com/javaee/6/api/javax/servlet/jsp/tagext/DynamicAttributes.html for more information.
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.
I have an HTML form bound to a Spring model to take in user data and add it to a database. This works fine. I have used Spring Freemarker macros for the fields to take input and validate before sending, e.g.
<#spring.formInput path="myForm.username"/>
<#spring.showErrors ", "/>
This also works fine for text input. What is causing me problems is rendering multiple checkboxes with the Spring macro. My original HTML was:
<input name="roleList" type="checkbox" value="1"/>Adminstrator
<input name="roleList" type="checkbox" value="2"/>Developer
<input name="roleList" type="checkbox" value="3"/>Customer
I created a Java Map<String, String> of this information with the keys as "1", "2", "3" in my controller method and added it to the model, then replaced the HTML with this macro in my ftl template:
<#spring.formCheckboxes path="quickForm.roleList" options="${roleMap}" separator="<br>"/>
But I get an error
Expecting a string, date or number here, Expression roleMap is instead a freemarker.template.SimpleHash
Why would it give that message if it requires a Map? (as in the Spring Docs for FreeMarker macros) Can anyone explain how I should be providing the checkbox data?
Finally worked this out, so for anyone who's interested: it wasn't as straightforward as just creating a HashMap of values and adding them with model.addAttribute("roleMap", myHash) - I had to create a service class to do this instead: it retrieved my list of roles from a database table and then converted them into a HashMap<String, String>. I then called this in my controller method, added it to my model (called "roleMap" here) and used it within my FreeMarker template without the usual formatting, like this:
<#spring.formCheckboxes path="quickForm.roleList" options=roleMap separator="<br>"/>
Having the data converted in a service method was key for Spring to use it as checkbox options.
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.
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.