Announcement Announcement Module
Collapse
No announcement yet.
need help with somewhat tricky custom prop editor to set appropriate Object's fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • need help with somewhat tricky custom prop editor to set appropriate Object's fields

    Hi, Gentlemen.
    I need Your help with the following (I work with Spring 3):
    there's a Controller, e.g. SubmitSomeRequestController (it doesn't extend a spring's base class ->Spring 3 doesn't require that):

    @Controller
    public class SubmitSomeRequestController {
    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    MyCustomBoolPropEditor editor = new MyCustomBoolPropEditor();
    binder.registerCustomEditor(Boolean.class, "boolField1", editor);
    binder.registerCustomEditor(Boolean.class, "boolField2", editor);
    }
    @RequestMapping(method = RequestMethod.POST, value="someUrlOfMine/htmPageName.htm")
    public ModelAndView createTheDesiredObject(
    HttpServletRequest request) {
    Map<String, String[]> params = request.getParameterMap();
    boolean field1 = Boolean.parseBoolean( params.get("boolField1")[0] );
    boolean field2 = Boolean.parseBoolean( params.get("boolField2")[0] );
    //retrieve any other params from Request
    //do some othe sexy stuff concerned with setting/populating appropriate Objects' fields
    ModelAndView mav = buildMavForCheckRequest(...);
    return mav;
    }
    }
    What I expect/wanna to have via using the mentioned above? I want Spring to convert automatically those boolField1/boolField2 etc. from String "true" to true of type boolean (yes, setAsText/getAsText in MyCustomBoolPropEditor are properly implemented). And I EXPECT that conversion already done when createTheDesiredObject() method is invoked, but meanwhile request contains String parameters, so what do I do wrong (or rather how should I implement the method to make Spring do the conversion automatically for me)??? There's one more implication:
    the form's fields represent different Objects' fields, so I tried to specify the handling method in my Controller like this:
    @RequestMapping(method = RequestMethod.POST, value="someUrlOfMine/htmPageName.htm")
    public ModelAndView createTheDesiredObject(HttpServletRequest request, DataBinder binder) {
    //...
    }
    but the above seems incorrect as in case of using DataBinder I have to point out the @ModelAttribute, but I have several objects, who's fields have to be populated from the submitted form's values, so using the @ModelAttribute seems incorrect to me, pls correct me if I'm wrong..
    Perhaps I have to implement some other method to get Spring do the trick - convert automatically String "true" to Boolean type?? Or do I have to change the createTheDesiredObject() method mentioned above for the conversion to work?
    Any help with the above (perhaps I missed smt substantial concerning the propEditors' conversion) or explanations how it works would be highly appreciated (sure, I've read appropriate Spring's reference docs and googled a lot, but meanwhile still can't get it work as expected ).
    Gents, thanks in advance for help/hints.
    Last edited by UntiredAdventurer; Nov 14th, 2011, 03:58 AM.

  • #2
    Please use [ code][/code ] tags when posting code ...

    And I EXPECT that conversion already done when createTheDesiredObject() method is invoked, but meanwhile request contains String parameters, so what do I do wrong (or rather how should I implement the method to make Spring do the conversion automatically for me)???
    Your assumption is wrong! Spring will only convert when setting the properties on an actual object (so setting the parameters on a ModelAttribute)... The request ALWAYS contains String parameters because that is all we have on the web side of things...

    Comment


    • #3
      Thanks for the hint, Martin. Upon having looked through number of other posts/postThreads (yes, I noticed numerous hints/attemptsToHelp of yours) it became clear why I was facing this issue
      Please use [ code][/code ] tags when posting code <-- will try to keep in mind

      Comment

      Working...
      X