PDA

View Full Version : "Cannot create command without commandClass" error message


wessie
Aug 9th, 2007, 11:55 AM
Hi all,

I've recently started work on a spring/hibernate/mysql website (just to get familiar with spring) and i've found that the petclinic sample application that ships with the distribution is very similar to what i've invisioned my website to be. As such, i've used the petclinic application as a framework (for lack of better words) on which i'll base my application.

I've started by creating the application piece by piece with only the most basic features - the redirection from the "index.jsp" to "welcome.htm" complete.

I've now added the functionality that would cater for adding patients (akin to adding owners in the petclinic application). On my "welcome.htm" file I have a link that would allow users to add a patient. When i click on the "Add Patient" link i get the following error message

java.lang.IllegalStateException: Cannot create command without commandClass being set - either set commandClass or (in a form controller) override formBackingObject
org.springframework.web.servlet.mvc.BaseCommandCon troller.createCommand(BaseCommandController.java:3 36)
org.springframework.web.servlet.mvc.AbstractFormCo ntroller.formBackingObject(AbstractFormController. java:458)
org.springframework.web.servlet.mvc.AbstractFormCo ntroller.getErrorsForNewForm(AbstractFormControlle r.java:341)

.
.
.
.
Now, i've made pretty much an exact copy of the code thats involved in the Add Owner functionality of the petclinic application.

The main area of contention is probably the fact that in my controller i dont have a formBackingObject method, but this doesn't exist in the AddOwnerForm class in the petclinic application either. And yes, the setCommandClass method is called in the constructor of my controller.

Does anybody know what the cause of the problem could be?

Jörg Heinicke
Aug 10th, 2007, 12:13 AM
The main area of contention is probably the fact that in my controller i dont have a formBackingObject method, but this doesn't exist in the AddOwnerForm class in the petclinic application either. And yes, the setCommandClass method is called in the constructor of my controller.

When you don't implement formBackingObject and so not overwrite the super implementation createCommand gets called as you can see from your stacktrace. That's absolutely ok if you provide the commandClass property. If the command class has no default constructor it would also fail. Can not think of another cause for this at the moment.

Joerg

wessie
Aug 10th, 2007, 02:48 AM
thanks for the quick response Jörg!

I've tried setting commandClass in the constructor of my controller and even setting it in my xxx-servlet.xml file and even a combination of the 2 and still i get the same error message. I've even tried returning an empty object from my formBackingObject method in addition to setting commandClass in my constructor and i still get the error message.

At this point i'm at pretty much a loss for what the problem could be!

Any assistance is appreciated

--spring newbie!

Jörg Heinicke
Aug 10th, 2007, 09:29 AM
I've tried setting commandClass in the constructor of my controller and even setting it in my xxx-servlet.xml file and even a combination of the 2 and still i get the same error message.

You did not respond on my question regarding the default constructor ...

I've even tried returning an empty object from my formBackingObject method in addition to setting commandClass in my constructor and i still get the error message.

... but this probably points to a more severe problem. Since I have no further idea you might provide your configuration and/or controller code.

Joerg

wessie
Aug 11th, 2007, 04:01 PM
Hi Jörg,

Thanks again for the quick response!!

"You did not respond on my question regarding the default constructor ..." - I set the commandClass and commandName in the default constructor of the command controller. I still get the error message.

Below is the relevant code snippets

emrapp-servlet.xml
==============
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.htm">surgeryController</prop>
<prop key="/addPatient.htm">addPatientForm</prop>
</props>
</property>
</bean>

<bean id="addPatientForm" class="com.emr.web.AddPatientForm">
<property name="commandClass" value="com.emr.Patient"/>
<property name="commandName" value="patient"/>
<property name="formView" value="patientForm"/>
<property name="successView" value="patientRedirect"/>
</bean>


AbstractHospitalForm.java
===================
public abstract class AbstractHospitalForm extends SimpleFormController
{
private Hospital hospital;

public void setHospital(Hospital h)
{
hospital = h;
}

public Hospital getHospital()
{
return hospital;
}

public void afterPropertiesSet() {
if (hospital == null) {
throw new IllegalArgumentException("'hospital' is required");
}
}

/**
* Set up a custom property editor for the application's date format.
*/
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

/**
* Method disallows duplicate form submission.
* Typically used to prevent duplicate insertion of entities
* into the datastore. Shows a new form with an error message.
*/
protected ModelAndView disallowDuplicateFormSubmission(HttpServletRequest request, HttpServletResponse response)
throws Exception {

BindException errors = getErrorsForNewForm(request);
errors.reject("duplicateFormSubmission", "Duplicate form submission");
return showForm(request, response, errors);
}
}

AddPatientForm.java
===============

public class AddPatientForm extends AbstractHospitalForm
{
public AddPatientForm()
{
setCommandName("Patient");
setCommandClass(Patient.class);
setSessionForm(true); // need a session to hold the formBackingObject
}

/*protected Object formBackingObject(HttpServletRequest request) throws ServletException
{ System.out.println("*************** I AM HERE*************");
Object o = new Object();
return o;
}*/

/* protected void onBind(HttpServletRequest request, Object command)
{
}
*/
/** Method inserts a new Patient */
public ModelAndView onSubmit(Object command) throws ServletException
{
Patient patient = (Patient)command;
getHospital().storePatient(patient); //insert to be done by Business Layer
return new ModelAndView(getSuccessView(), "patientId", patient.getId());
}

protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
return disallowDuplicateFormSubmission(request, response);
}
}




I'd be most appreciative of any help :-)

Jörg Heinicke
Aug 11th, 2007, 05:05 PM
I set the commandClass and commandName in the default constructor of the command controller. I still get the error message.

The default constructor did not refer to the controller but to the form-backing or command object. So has Patient a default constructor? What happens if you remove the comments for formBackingObject() in AddPatientForm (which I consider a bad naming since it is a controller but this is of course up to you)? There must be another error then.

Joerg

wessie
Aug 12th, 2007, 12:40 PM
Hi Jörg,

You were spot on!
It was my lack of a default constructor in the Patient class.

Thanks alot for your help! It's really appreciated!