Announcement Announcement Module
Collapse
No announcement yet.
Initializing commandClass in SimpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initializing commandClass in SimpleFormController

    I have an instance of SimpleFormController which handles the URL /query.htm. Further, I am injecting commandClass into the
    SimpleFormHandler using the bean descriptor as follows.

    When query.htm is accessed, BaseCommandController issues an exception that commandClass is not initialized. Does not the
    initialization of commandClass into SimpleFormController automatically
    inject the commandClass into BaseCommandController?

    java.lang.IllegalStateException: Cannot create command without commandClass being set - either set commandClass or override formBackingObject
    org.springframework.web.servlet.mvc.BaseCommandCon troller.createCommand(BaseCommandController.java:2 67)
    org.springframework.web.servlet.mvc.BaseCommandCon troller.getCommand(BaseCommandController.java:256)
    org.springframework.web.servlet.mvc.AbstractComman dController.handleRequestInternal(AbstractCommandC ontroller.java:77)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)


    sproject-servlet.xml:
    =============
    </bean>
    <bean id="formCommandController" class="StudentFormController">
    <property name="commandClass"><value>UserCommand.class</value></property>

    <property name="formView"><value>index</value></property>
    <property name="successView"><value>userInfo</value>
    </property>
    </bean>
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="urlMap">
    <map>
    <entry key="/query.htm">
    <ref bean="formCommandController" />
    </entry>
    </map>
    </property>
    </bean>

  • #2
    Re: Initializing commandClass in SimpleFormController

    Originally posted by umark
    Code:
    <property name="commandClass"><value>UserCommand.class</value></property>
    I believe you just need the fully qualified class name for the value, and without the ".class" part.

    Comment


    • #3
      Re: Initializing commandClass in SimpleFormController

      Hi -

      I do not believe that the IllegalStateException is due to
      my not qualifying the class name fully; if the class name were not
      qualified fully, it would yield a ClassDefNotFoundException.

      Besides, the class UserCommand is fully qualified - its in the default
      package (./).

      I tried to set the commandName as well and I got the same exception
      (actually, I do not understand the relation between "commandName" and "commandClass" parameters).

      As a final step, I upgraded to Spring 1.2.1 and also overrode
      formBackingObject(); still the same exception.

      I'd quite appreciate any help with this.

      Thanks,

      U.


      Originally posted by manifoldronin
      Originally posted by umark
      Code:
      <property name="commandClass"><value>UserCommand.class</value></property>
      I believe you just need the fully qualified class name for the value, and without the ".class" part.

      Comment


      • #4
        Re: Initializing commandClass in SimpleFormController

        Originally posted by umark
        I do not believe that the IllegalStateException is due to
        my not qualifying the class name fully; if the class name were not
        qualified fully, it would yield a ClassDefNotFoundException.
        BaseCommandController.createCommand() throws IllegalStateException if property 'commandClass' is null. One of the reasons for 'commandClass' to be null (other than not being specified or wrong property name in the definition) is that it failed to be loaded when the controller was being initiated and injected, which is when your app is being deployed. Did you look back in your log and see if there is any previous exceptions?

        Originally posted by umark
        Besides, the class UserCommand is fully qualified - its in the default
        package (./).
        Well, did you at least try the "without the '.class' part" part? :wink:

        Comment


        • #5
          Re: Initializing commandClass in SimpleFormController

          Originally posted by manifoldronin
          Well, did you at least try the "without the '.class' part" part? :wink:
          You are correct. Do not append the .class when injecting the property. It is magically resolved to a class internally.

          Comment


          • #6
            Yes, I tried it without the ".class" suffix and I get the same result.

            Here is my controller definition:

            <bean id="formCommandController" class="StudentFormController">
            <property name="commandName"><value>user</value></property>
            <property name="commandClass"><value>UserCommand</value></property>
            <property name="formView"><value>index</value></property>
            <property name="successView"><value>userInfo</value>
            </property>
            </bean>

            <bean id="urlMapping"
            class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
            <property name="urlMap">
            <map>
            <entry key="/query.htm">
            <ref bean="formCommandController" />
            </entry>
            </map>
            </property>
            </bean>



            Here is the controller implementation:


            public class StudentFormController extends SimpleFormController {

            /**
            *
            */
            public StudentFormController() {
            super();
            // TODO Auto-generated constructor stub
            setCommandClass(UserCommand.class);
            setCommandName("user");
            System.out.println(" Ctor of StudentFormController");
            }

            protected Object formBackingObject(HttpServletRequest request)
            throws Exception
            {
            System.out.println("formBackingObject of StudentFormController");
            return new UserCommand("rks");
            }

            protected ModelAndView onSubmit(Object cmd, BindException errors) throws
            Exception
            {
            System.out.println("onSubmit of StudentFormController");
            UserCommand command = (UserCommand) cmd;
            return new ModelAndView(getSuccessView(), "userInfo", command.ge
            tUser());
            }

            }


            What is puzzling is that even the overriding of the formBackingObject
            causes the same exception to be raised.


            Thanks for the suggestions,

            /U

            Comment


            • #7
              Originally posted by umark
              Yes, I tried it without the ".class" suffix and I get the same result.
              Are you sure it's exactly the same exception as in the OP? The one in OP shows stacktrace:
              Code:
              org.springframework.web.servlet.mvc.BaseCommandController.createCommand&#40;BaseCommandController.java&#58;267&#41;
              org.springframework.web.servlet.mvc.BaseCommandController.getCommand&#40;BaseCommandController.java&#58;256&#41;
              org.springframework.web.servlet.mvc.AbstractCommandController.handleRequestInternal&#40;AbstractCommandController.java&#58;77&#41;
              which indicates AbstractCommandController.handleRequestInternal isn't overriden, which it's supposed to by the version in AbstractFormController.

              Comment

              Working...
              X