Announcement Announcement Module
Collapse
No announcement yet.
MultiActionController and initBinder Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MultiActionController and initBinder

    I see stuff in this post about using initBinder in MultiActionController:

    http://forum.springframework.org/showthread.php?t=13415

    I overrode initBinder in my class that extends MultiActionController, but it's not getting call when the class is loaded in and my methods are called. Do I have to do something else besides override initBinder?

    I've been searching but I can't find any examples of MultiActionControllers that override initBinder.

    Thanks in advance.
    Last edited by robyn; May 15th, 2006, 06:08 PM.

  • #2
    Do I have to do something else besides override initBinder?
    Having reviewed the code, you need a method signature like:
    Code:
    public ModelAndView myHandler(HttpServletRequest request, HttpServletResponse response, Object command) {
    This seems to be by evolution as binding only makes sense when you have a command object. The relevant code is in org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController:
    Code:
    protected final ModelAndView invokeNamedMethod(String methodName, HttpServletRequest request, HttpServletResponse response) throws Exception {
    ...
    			if (method.getParameterTypes().length >= 3 &&
    					!method.getParameterTypes()[method.getParameterTypes().length - 1].equals(HttpSession.class)) {
    				Object command = newCommandObject(method.getParameterTypes()[method.getParameterTypes().length - 1]);
    				params.add(command);
    				bind(request, command);
    			}
    So bind is called if the method has than 3 or more arguments, and the last argument is not of type HttpSession.class.

    Comment


    • #3
      Awesome, that worked great.

      That would've taken me forever to find that on my own, thanks for the tip.

      Comment


      • #4
        Having reviewed the code, you need a method signature like:
        Code:
        public ModelAndView myHandler(HttpServletRequest request, HttpServletResponse response, Object command) {
        Am I supposed to be using this command object that's passed in? Should I be setting fields on it, then passing it to the new ModelAndView?

        If so, where should I be setting the class type of this command? I tried setting commandClass in the config like with the form controllers, but MultiActionController doesn't have a commandClass var to set.

        Comment


        • #5
          set it directly in method declaration. Like
          Code:
          public ModelAndView myHandler(HttpServletRequest request, HttpServletResponse response, YourCommandObjectClassName command) {

          Comment


          • #6
            Originally posted by lucker
            set it directly in method declaration. Like
            Code:
            public ModelAndView myHandler(HttpServletRequest request, HttpServletResponse response, YourCommandObjectClassName command) {
            Can't get any easier than that. Thanks.

            Comment


            • #7
              This seems like it should be so simple, but I can't get it to format my fields when I display them.

              My Controller:
              Code:
              public class PreferencesController extends MultiActionController {
              
                  public PreferencesController() {
                      super();
                  }
              
                  protected void initBinder(ServletRequest servletRequest, ServletRequestDataBinder servletRequestDataBinder) throws Exception {
                      System.out.println("\n\nPreferencesController.initBinder\n\n");
                      servletRequestDataBinder.registerCustomEditor(String.class, "user.contactInformation.homePhone", new PhoneNumberPropertyEditor());
              
                      SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                      servletRequestDataBinder.registerCustomEditor(java.util.Date.class, null, new CustomDateEditor(dateFormat,false));
                  }
              
                  public ModelAndView prepareAccountHome(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreferencesCommand command) throws ServletException {
                      PersonTO user = (PersonTO) httpServletRequest.getSession().getAttribute(Constants.SESSION_USER);
                      user.setBirthday(Calendar.getInstance().getTime());
                      command.setUser(user);
                      return new ModelAndView("/preferences/account", "command", command);
                  }
              }
              In the resulting jsp, I've tried displaying the fields using both jstl, or by doing:

              Code:
                      <spring&#58;bind path="command.user.contactInformation.homePhone">
                          Home Phone Bind&#58; $&#123;status.value&#125;<br>
                      </spring&#58;bind>
              With both JSTL and the spring tag, it displays the normal value, not nicely formatted like the editor would make.

              Am I missing a step? It seems like the property editors aren't getting called.

              Looking in the code, I see the MultiActionController does this:
              Code:
              		ServletRequestDataBinder binder = createBinder&#40;request, command&#41;;
              		binder.bind&#40;request&#41;;
              Then ServletRequestDataBinder does this:
              Code:
              		MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues&#40;request&#41;;
              mpvs comes back with a propertyValueList with size 0. That doesn't sound right. What am I missing?

              Thanks in advance.

              Comment


              • #8
                Am I missing a step? It seems like the property editors aren't getting called.
                Yes. You're right! In the case of MultiActionController initialized binder used to parse request parrameters only and resulting BindException object is not passed to view.

                So, consider use SimpleFormController intead.

                Comment


                • #9
                  Originally posted by lucker
                  So, consider use SimpleFormController intead.
                  So I should use the SimpleFormController even if there's no form on my page? I'm just looking to be able to format the way things like phone numbers are displayed when they're not in a form.

                  Comment


                  • #10
                    ok. You can write something like this

                    Code:
                        public ModelAndView prepareAccountHome&#40;HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PreferencesCommand command&#41; throws ServletException &#123;
                            //.... process your command object here 
                            return new ModelAndView&#40;"/preferences/account", createBinder&#40;request, command&#41;.getErrors&#40;&#41;.getModel&#40;&#41;&#41;;
                        &#125;

                    Comment


                    • #11
                      It's possible validate a command whith MultiActionController ?

                      Comment


                      • #12
                        I'm just looking to be able to format the way things like phone numbers are displayed when they're not in a form
                        Take a look at the spring:transform tag

                        Comment


                        • #13
                          It's possible validate a command whith MultiActionController ?
                          Not out of the box. To work with command objects, you normally have a form which is normally more suitably handled by a form based controller such as SimpleFormController

                          Comment


                          • #14
                            Originally posted by katentim
                            Take a look at the spring:transform tag
                            I believe it is usable in the case, when model contains BindException object with initialized custom editors. AbstractFormController put that object into model calling Map model = errors.getModel(), and merging this map with resulting model. But MultiActionController dont make that. So you need put it yourself.

                            Comment


                            • #15
                              katentim wrote:
                              Not out of the box. To work with command objects, you normally have a form which is normally more suitably handled by a form based controller such as SimpleFormController
                              I have a form what receive a List and iterate in her, before of to be submited. So SimpleFormController is not indicate, correct ?
                              I am use AbstracCommandController, with this constructor:

                              PedidosAutorizadosControlador(){
                              setCommandClass(PedidosAutorizadosBean.class);
                              setValidator(new PedidosAutorizadosValidacao());
                              setCommandName("pedidos");
                              }

                              so it's not validate my form, why ?
                              Thank's.
                              Carlos

                              Comment

                              Working...
                              X