In JSP, Field Path Uses HashMap<String,ArrayList<String>> (SpringMVC Form) - spring

Suppose my model field is a HashMap of Strings to ArrayList's of Strings.
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
For field binding, I need to point to an i-th item of the ArrayList of a specific key in my JSP. How do I do it? Will this double-bracket notation work? Assume that 'key' and 'index' are known JSTL variables in my JSP.
<form:checkbox path="map[${key}][${index}]" />
What I'm essentially doing is, for a field value, store its path e.g.
map['testkey'][4]
which would point to
map.get("testkey").get(4);
That doesn't work:
org.springframework.beans.NullValueInNestedPathException: Invalid property 'map[TestKey][0]'
NOTE According to this (Binding a map of lists in Spring MVC), this will be a problem because sub-objects are not auto-growable, and I need to implement a LazyList or a growable list? An ArrayList by itself is growable for Spring MVC's forms, but when used as a sub-collection, it's not? Very tricky.

I solved this problem by following the LazyList/LazySet implementation in the link above,
Binding a map of lists in Spring MVC
From their example,
public class PrsData {
private Map<String, List<PrsCDData>> prsCDData;
public PrsData() {
this.prsCDData = MapUtils.lazyMap(new HashMap<String,List<Object>>(), new Factory() {
public Object create() {
return LazyList.decorate(new ArrayList<PrsCDData>(),
FactoryUtils.instantiateFactory(PrsCDData.class));
}
});
}
}
Once I had a data structure like that mapped to my JSP Path, the field binding magically started working and I could then make my path e.g. map["key"][index] in the JSP.
As they replied, SpringMVC auto-converts simple lists and maps to Lazy collections, but does not do it when the collection is a subobject.

Related

Easy way to submit an array field with Spring MVC forms?

I have a form with a list of nested fieldsets corresponding to a collection of objects, backed by a form object server-side. Fieldsets can be added or removed client-side. I want to submit a form without caring about object indices or sparse lists in command object.
Here's my Controller method code:
#PostMapping("/foobar")
public String doPost(FoobarForm form) {
//...
}
In PHP, Rails etc it's very easy:
<input name="prop[]">
, and it will automatically populate $_POST["prop"] with all the values.
Working with Spring MVC, I tried these things:
<input name="prop[]"> - doesn't work saying Invalid property 'prop[]' of bean class [...]: Invalid index in property path 'prop[]'; nested exception is java.lang.NumberFormatException: For input string: ""
<input name="prop"> - will not bind to a list-typed bean property, even when multiple fields present.
<input name="prop[${i}]"> - implies all that hassle with sparse list and index handling, both client-side and server-side. Certainly not the right way to do things when working with a powerful web framework.
I'm wondering why can't I just use [] in property name and let Spring create a list automatically? It was asked three times on Spring JIRA without any reasonable responses.
Spring form binding makes this more easy. You need to add List object in your bean and bind that in jsp using spring form.
class FoobarForm {
List<String> prop;
}
In jsp, if you need to show/edit value all at once then <form:input path="prop" />
.if you want to show one by one then use indexing<form:input path="prop[0]" />. Use proper CommandName in form. It will work.
I found the answer here.
You can use the MultiValueMap<String, String>
#RequestMapping("foo")
public String bar(#RequestParam MultiValueMap<String, String> parameters){ ... }
With these two inputs:
<input name="prop" value="One">
<input name="prop" value="Two">
the result will be {prop=["One","Two"]}
The code below will work too.
public String bar(#RequestParam("prop") List<String> props){ ... }

How does Spring bind form values into class variables?

If I use a form:form object in Spring, I can use the commandName in order to let Spring inject the class variable values.
However, I wonder, how does the controller catch this value?
#RequestMapping(method = RequestMethod.POST, value = "/form")
public String postForm(#ModelAttribute("item") Item item, ModelMap model)
{
return "result";
}
In the above code, the Item is injected. However, even changing the name of this variable (or removing the modelattribute), doesn't affect that this variable is injected with the form values.
Will spring just inject the values in the first model class found, from the form? How does Spring know that it has to inject the form into the Item item parameter?
At first I thought the variable in the controller (POST) should have the name of commandName of the form, but it does work with other names as well, strangely enough.
There is a dedicated section in the Spring Documentation describing the usage of #ModelAttribute on method arguments.
This process is known as Data Binding on submit and is following some conventions:
If #ModelAttribute is explicitely declared with a name on an argument (your case). In this case the submitted data of the form are copied over automatically under this name. You can check yourself that it is already there in your ModelMap model by invoking/inspecting model.get("item").
If there is no #ModelAttribute argument annotation at all, then the attribute name is assumed from the type essentially in your case type Item converts to attribute name item (camelCase notation) that is created for you holding a new Item with the form data-bind'ed fields. That is also there in the ModelMap (same check as above: model.get("item"))
Key point to realise is in all these cases DataBinding occurs before hitting your Post form RequestMapping.

How to control two operations for same url in Spring mvc?

Consider the following problem.
The user has chosen to create a document by clicking on the Create document and then he writes data into the document. The url for creating the document is /document/save.
For the subsequent write up, the existing document must be saved instead of creating a new one.
Here is my code for that.
#Controller
public MyController implements Controller, TemplateTypeAware
{
#RequestMapping("/document/save")
public String saveOrCreateDocument(#ModelAttribute DocumentWrapper wrapper, ModelAndView m)
{
if(m.getModel().get("document_id")==null)
{
Document doc=createDocument(wrapper);
m.addObject("document_id",doc.getId());
}
else
{
saveDocument(m.getModel().get("document_id"), wrapper);
}
return documentView;
}
}
Template:
<form>
<input type="hidden" name="document_id" th:value="*{document_id}"/>
<!-- other fields -->
</form>
The problem here is, I am getting document_id always null. Is there any work around for this problem?
Thanks in advance. Hope you will reply as soon as possible.
Form fields will be automatically bound to your DocumentWrapper fields if they have matching names, that means DocumentWrapper needs a field named document_id, otherwise the document_id request parameter won't be bound to your object.
Model attributes will be exposed to the view, at this point the model will be empty, you can add attributes in your handler method and they will become in your view, but request parameters won't be in the model. That explains why you always get null.
If you just need the document_id parameter, use #RequestParam:
#RequestMapping("/document/save")
public String saveOrCreateDocument(#RequestParam("document_id") Long documentId, Model m) {
...
}
Please refer to the binding section of Spring MVC: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

Dynamically generated form submission and Spring framework?

I have a dynamically generated form (using jQuery). So beforehand I don't know how many form elements do I have. Does anybody have any idea how we can do it.?
POST the fields in the request body as a JSON-ified map, and use Jackson to to turn it into a Map<String, String> on the controller side. Your controller method would look something like this:
#RequestMapping(value="/url", method=RequestMethod.POST)
public getMap(#RequestBody Map<String, String> fields){
//Manipulate fields as you wish
.
.
.
}
That way you will have your fields as key/pair values inside the map ready for you to manipulate them.
You can turn your fields into a map fairly easily using jQuery (or similar) like this:
var fields = {}
$.each("input:text", function(i, item){
fields[mapKeyValue] = $(item).val();
}
This example assumes that your pair value is the text input, but you can manipulate the values however you want. This is by no means a complete example, but should give you an idea on how to start.

In Spring MVC 3, how do I bind an object to a query string when the query string parameters don't match up with the object fields?

A 3rd party is sending me part of the data to fill in my domain object via a query string. I need to partially fill in my domain object, and then have the user fill in the rest via a form. I don't have any control over the query string parameters coming in, so I can't change those, but I'd really like to be able to use Spring MVC's data binding abilities, rather than doing it by hand.
How can I do this?
To add some complication to this, some of the parameters will require extensive processing because they map to other objects (such as mapping to a user from just a name) that may not even exist yet and will need to be created. This aspect, I assume, can be handled using property editors. If I run into trouble with this, I will ask another question.
Once I have a partially filled domain object, passing it on to the edit view, etc. is no problem, but I don't know how to properly deal with the initial domain object population.
The only thing I have been able to come up with so far is to have an extra class that has it's properties named to match the inbound query parameters and a function to convert from this intermediary class to my domain class.
This seems like a lot of overhead though just to map between variable names.
Can you not just have the getter named differently from the setter, or have 2 getters and 2 setters if necessary?
private int spn;
// Standard getter/setter
public int getSpn() {
return spn;
}
public void setSpn(int spn) {
this.spn = spn;
}
// More descriptively named getter/setter
public int getShortParameterName() {
return spn;
}
public void setShortParameterName(int spn) {
this.spn = spn;
}
Maybe that is not standard bean convention, but surely would work?

Resources