|
From: Jean-Francois C. <cor...@ro...> - 2003-07-07 01:27:48
|
Hi,
I have been lurking on this list for a while. And I am currently
experimenting with Spring 0.9.
In a recent email, Juergen asked for feedback from people using Spring.
Here goes.
In my application, I wanted to have something like a Wizard when a user is
filling a form. I used the AbstractWizardFormController.
My form object contains a collection. Using the wizard, I would like that
one step allowed the user to add an arbitrary number of items to the
collection. So each time a user adds an item, the same step is displayed so
that he can continue adding. When he is finished he just presses the Next
or Finish Button.
I have modified the petclinic application to see how I could do this with
Spring. I modified the AddOwnerForm so that when a Owner is Added his pets
can also be added. What I did is add a second step where a Pet can be
created. On this steps there are the standard Prev, Cancel and Finish
Buttons. But there is also a Add button to add the pet to the owner. The
add button points to the current wizard step.
I modified the onBindAndValidate() method to check for the presence of the
_target1 attribute for page1. If it is there, it means that the user has
pressed the Add button. I create a pet and add it to the owner. It kinda
works, but is a bit klunky. I do not like the fact that I had to hard code
"_target1" to make this work.
Is there a better way to do this?
English is not my first language, so my explanation may not be clear. Just
ask and I will explain further.
Thanks
Jean-Francois Cormier
Here is the code to the AddOwnerAndPetForm
/*
* Created on 2-Jul-2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package petclinic.web;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import petclinic.Clinic;
import petclinic.Owner;
import petclinic.Pet;
import com.interface21.validation.BindException;
import com.interface21.validation.Errors;
import com.interface21.web.servlet.ModelAndView;
import com.interface21.web.servlet.mvc.AbstractWizardFormController;
/**
* @author jxc170
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class AddOwnerAndPetsForm extends AbstractWizardFormController {
private Clinic clinic;
public AddOwnerAndPetsForm() {
setCommandClass(Owner.class);
}
/* (non-Javadoc)
* @see
com.interface21.web.servlet.mvc.AbstractWizardFormController#validatePage(java.lang.Object,
com.interface21.validation.Errors, int)
*/
protected void validatePage(Object command, Errors errors, int page) {
// TODO Auto-generated method stub
logger.debug("Validate Page:" + page);
}
/* (non-Javadoc)
* @see
com.interface21.web.servlet.mvc.AbstractWizardFormController#processFinish(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse, java.lang.Object,
com.interface21.validation.BindException)
*/
protected ModelAndView processFinish(HttpServletRequest
request,HttpServletResponse response,Object command,BindException errors)
throws ServletException, IOException {
Owner owner = (Owner) command;
getClinic().insert(owner);
List pets = owner.getPets();
Object[] petArray = pets.toArray();
for ( int i = 0; i < petArray.length; i++) {
Pet pet = (Pet) petArray[i];
pet.setOwner(owner);
getClinic().insert(pet);
}
return new ModelAndView("ownerRedirect", "ownerId",
Integer.toString(owner.getId()));
}
/* (non-Javadoc)
* @see
com.interface21.web.servlet.mvc.AbstractWizardFormController#referenceData(javax.servlet.http.HttpServletRequest,
java.lang.Object, com.interface21.validation.Errors, int)
*/
protected Map referenceData(HttpServletRequest request,Object
command,Errors errors,int page) throws ServletException {
Map model = new HashMap();
System.out.println("\n\n\n+++++++++++++++++++++++++++++++++++++++++++\n\n\n")
;
System.out.println(" GETTING TYPES ");
Map types = clinic.getTypes();
System.out.println("\n\n\n+++++++++++++++++++++++++++++++++++++++++++\n\n\n")
;
model.put("types", types);
return model;
}
/* (non-Javadoc)
* @see
com.interface21.web.servlet.mvc.AbstractWizardFormController#processCancel(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse, java.lang.Object,
com.interface21.validation.BindException)
*/
protected ModelAndView processCancel(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws ServletException, IOException {
// TODO Auto-generated method stub
logger.debug("Wizard Canceled");
return null;
}
/* (non-Javadoc)
* @see
com.interface21.web.servlet.mvc.AbstractWizardFormController#onBindAndValidate(javax.servlet.http.HttpServletRequest,
java.lang.Object, com.interface21.validation.BindException, int)
*/
protected void onBindAndValidate(HttpServletRequest request,Object
command,BindException errors,int page) throws ServletException {
logger.debug("On Bind and Validate Page:" + page);
switch (page) {
case 0:
break;
case 1:
if ( request.getParameter("_target1") != null ) {
String petName = request.getParameter("name");
String petBirthDate = request.getParameter("birthDate");
String type = request.getParameter("typeId");
if ( petName != null ) {
Pet pet = new Pet();
pet.setName(petName);
int year = Integer.parseInt(petBirthDate.substring(0,4));
int month = Integer.parseInt(petBirthDate.substring(5,7));
int day = Integer.parseInt(petBirthDate.substring(8,10));
pet.setBirthDate(new Date(year, month, day));
pet.setTypeId(Integer.parseInt(type));
Owner owner = (Owner) command;
owner.addPet(pet);
}
}
break;
default:
break;
}
}
/**
* @return
*/
public Clinic getClinic() {
return clinic;
}
/**
* @param clinic
*/
public void setClinic(Clinic clinic) {
this.clinic = clinic;
}
}
|