Freemarker Checkboxes input in Spring form - spring

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.

Related

spring form input fields type attribute

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.

Spring MVC: Save form value to XML

I am trying to save form in a xml format. There is no model class defined for this form. Form structure is something like this.
- Section1
Field1
Field2
Field3
and I want to save it to
<section1>
<Field1></Field1>
<Field2></Field2>
<Field3></Field3>
</section1>
How would I do that?
To track which html field belongs to which section I can name them as name="section1.field2". It can help me to find out parent element of the field.
Create an XML in Javascript on onSubmit Event by traversing the input elements of the form using any framework like jQuery.
And creating the XML in your desired format is very easy if you follow this.
Use flush() to write it to any hidden variable, thereupon you can submit the form or you can post it using ajax as per your requirement.

Spring Validation Displaying Custom Message

Validation in Spring 3.x using #Valid annotation:
Below is snippet from the messages_en.properties. I have a form having Username and Password field. When user does not enter anything in Username field, it displays both these messages one below other.
NotEmpty.loginBean.username=Username cannot be Empty
Size.loginBean.username=Size must between 5 to 50 characters.
Any HTML tag given in the message.properties is not interpreted.
NotEmpty.loginBean.username=<li>Username cannot be Empty</li>
Above would display <li> as it is.
Questions:
1) Is there any ways to interpret HTML tag and display its output?
2) Can i show single message though both validation fails?
Ad. 1) Yes, use htmlEscape="false":
<form:errors path="nip" cssClass="error" htmlEscape="false" />
Ad. 2) This is actually JSR303's Achilles' heel - it can be done, but is neither easy nor clean (see this issue). Order of validating each annotated field is undefined, so trick is to use #GroupSequence and custom groups like described here or here.
Alternative solution would be to use custom annotation with #ReportAsSingleViolation, but it will not distinct NotEmpty and Size errors as it'll have its own error message.

Switching from ASP.NET code-behind to Play MVC and have lots of questions

How do I dynamically render HTML in Play? For example I have an enum or DB table with a list of platforms (pc/xbox/ps3). I want to render in my HTML form a checkbox list with each of those. For now I've just hard coded the HTML form with elements, but long term it needs to be dynamic and the lists (checkbox/radio/dropdown) need to be bound to the system so they receive new items as new items are added to the database.
On postback how do i access those form fields in the controller handling the postback? I've found that in the controller action's function parameter list I can specify the name of the form field, and that works, but this is unrealistic if i'm going to have forms that have 100 fields, then I do'nt want to have to declare 100 params in my controller action function.
you can call method values() on enums to obtain array of enum values. And then map it to List. For example you can use Lists.newArrayList(array) from google-collections.
form parameters you can find in request.params hashmap. Request is a field inherited from play.mvc.Controller.
Also have a look at this http://www.playframework.org/documentation/1.2.3/cheatsheet/controllers. (Usefull stuff but not easy to find)
UPD
List theList =....;
render(theList);
and in the template (example for select):
<select id="viewSelect" size="1" >
#{list items:theList, as:'listItem'}
<option id="${listItem.getId()}">${listItem.getName()}</option>
#{/list}
</select>

how spring mvc tag works?

I am trying to write some kind of raw html to mimic what spring mvc tag produces after page rendering(and I do make them look exactly the same if you open them with a html element inspector). as I want to create dynamic input form using javascript. but it didn't work. it seems I had to use only what it offers: e.g. <form:input path="firstName" />.
to get the data binding working.
I thought the tag lib only help you to produce a html block that spring knows how to handle them in backend (action). from a web http perspective. what else it can send beyond a bunch of form data, and they should send the same thing. so I am really curious to learn what magic thing the tag lib dose beyond producing a html block.
the other thing I would like to know is where the model object is being hold when the form is being submit to a matched action. you know, you can get the model attribute by using #modelAttribute as the input parameter. is it in the original request object? or in the ActionRequest to which the dispatcherServlet build and put it. or even somewhere else?
thanks in advance.
I got it figured out. the raw html just works as spring tag does. as long as you are in the form tag block. you are okay to use raw html such as
<input type="text" id="abc" name="abc"/> just make sure name reflect your bean attribute path.
id is not mandatory and is just helping you to identify the very element. I guess I missed something when I work with the raw html by the time I ask the question. hope this helps for guys working with raw html approach, especially in case of dynamic input creation.

Resources