views:

998

answers:

2

Hi,

I have a form JSP web page, this form contains multiple instances of the same Java object, I am using Java Spring Framework to control the "communication" with the controller and the view.

My problem is that i would like to be able to receive from the view a simple array containing the instances of my objects wich are currently on the page (on wich were probably modified).

When i want a specific kind of item, I usually just name it in my controller's method declaration, however for an array (or any Collection), this won't work.

so something like:

@RequestMapping
public String edit(...SomeObject[] objectName, ...){
}

would just return me an error, i can however receive an array of String, so this works:

@RequestMapping
public String edit(...String[] objectString, ...){
}

the goal would be to be able to make Spring automatically map the object thanks for your answers!

A: 

This is certainly possible; while I've not done it using @RequestMapping, I know that you can retrieve a collection it can be done with a "command" object (or @ModelAttribute)

Define a POJO with a collection attribute as your command

public class FooCommand {
    private List<String> myCollection;
    // Getter & Setter
}

Then access it in your controller

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("fooCommand") FooCommand fooCmd) {
    // do stuff with fooCmd.getMyCollection() 
}

That make any sense?

that works, however, what i need is to be able to receiving something other than a String, i would like Spring to automatically map the received String to a POJO
Gadgetsan
What kind of object? If can handle stuff like string, long, int, char (i think) but if it's a more complex object, you'll need to work with the property editor mentioned by Mark.
my object contains strings and integers, it can map 1 item, the problem arises when i try to map an array or a list of item
Gadgetsan
if i try to ask for a list, i receive a GenericSignatureFormatErrorException, if i try to ask for an array, i get:Could not instantiate bean class [SrbAdminInvMask;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [SrbAdminInvMask;.<init>()
Gadgetsan
Can you post a more code? GenericSignatureFormatError is a really odd error to get, as is the default contructor problem...
i'm sorry i cannot really post more code, but i finally used a different approach, i do the conversion from a String to the object myself. I think i was getting the GenericSignatureFormatError since i was trying to get a "List" wich if i remember correctly cannot be instanciated by itself
Gadgetsan
A: 

Spring does not know how to create your custom object from a String so you will need to create your own PropertyEditor for your custom object.

Chapter 5 of the Spring Reference explains data binding and there is an example in Chapter 13 of how to register custom property editors in your controller.

Mark
If you're converting something like a string to a date, yes, property editors are certainly needed.However, if you're submitting a form with foo=a, foo=b and foo=c, Spring MVC is smart enough to turn that into a List<String> named "foo"...
i was confused about initBinder and what was a property editor but i found an exemple there http://robobruin.blogspot.com/2007/09/spring-initbinder-example-that.html
Gadgetsan
the thing i need to receive is rather something like foo.atr1 = afoo.atr2 = bList<Object> foo
Gadgetsan
ah, i can see why spring would have a hard time; from spring's perspective, foo.atr1 and foo.atr2 are two different parameters. if you need something like that though, you can probably dump them in a map by naming them foo['atr1'] = a