PDA

View Full Version : Spring 3.0 MVC @ModelAttribute - order of properties



BaumTom
Jan 25th, 2012, 02:29 AM
Hello,

I use a controller class that has a method with a @ModelAttribute parameter:


@Controller()
public class SomeController {

@RequestMapping({ "/show" })
public ModelAndView show(@ModelAttribute("entry") SomeEntry entry) {
return new ModelAndView("show");
}

}



public class SomeEntry {

private String a;
private String b;
private String c;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

}

When the @ModelAttribute "entry" is filled in a POST request, the setters of "entry" are called in the following order:
1. setA
2. setB
3. setC

Can I specify in which order the setters are called? For example:
1. setC
2. setA
3. setB

Regards
Thomas

chelu
Jan 25th, 2012, 03:39 AM
No, you can't do it. The order come from request.getParameterMap().entrySet()

BaumTom
Jan 25th, 2012, 03:54 AM
Ok.

I have to ensure that, in my example, setC is called before setA. Is there any way to do that?

chelu
Jan 25th, 2012, 04:28 AM
Bind parameters to variables and fill your model in the order that you want.