spring mvc 学习二

command:

public class RantsForVehicleController     extends AbstractCommandController {   public RantsForVehicleController() {     setCommandClass(Vehicle.class);     setCommandName("vehicle");   }   protected ModelAndView handle(HttpServletRequest request,       HttpServletResponse response, Object command,       BindException errors) throws Exception {     Vehicle vehicle = (Vehicle) command;     List vehicleRants =        rantService.getRantsForVehicle(vehicle));     Map model = errors.getModel();     model.put("rants",4         rantService.getRantsForVehicle(vehicle));     model.put("vehicle", vehicle);     return new ModelAndView("vehicleRants", model);   }

public AddRantFormController() {    setCommandClass(Rant.class);    setCommandName("rant"); } protected Object formBackingObject(HttpServletRequest request)     throws Exception {    Rant rantForm = (Rant) super.formBackingObject(request);    rantForm.setVehicle(new Vehicle());    return rantForm; } protected Map referenceData(HttpServletRequest request)     throws Exception {    Map referenceData = new HashMap();    referenceData.put("states", ALL_STATES);    return referenceData; } protected ModelAndView onSubmit(Object command,     BindException bindException) throws Exception {    Rant rant = (Rant) command;    rantService.addRant(rant);    return new ModelAndView(getSuccessView()); }

validator相关:

<bean id="addRantController"     class="com.roadrantz.mvc.AddRantFormController">   <property name="formView" value="addRant" />   <property name="successView" value="rantAdded" />   <property name="rantService" ref="rantService" />   <property name="validator">    <bean class="com.roadrantz.mvc.RantValidator" />  </property></bean>

protected Object formBackingObject(HttpServletRequest request)      throws Exception {    Motorist formMotorist = new Motorist();    List<Vehicle> vehicles = new ArrayList<Vehicle>();    vehicles.add(new Vehicle());    formMotorist.setVehicles(vehicles);    return formMotorist; } protected Map referenceData(HttpServletRequest request,     Object command, Errors errors, int page) throws Exception {    Motorist motorist = (motorist) command;    Map refData = new HashMap();    if(page == 1 && request.getParameter("_target1") != null) {        refData.put("nextVehicle",            motorist.getVehicles().size() – 1);    }    return refData; } protected void postProcessPage(HttpServletRequest request,     Object command, Errors errors, int page) throws Exception {    Motorist motorist = (Motorist) command;

  if(page == 1 && request.getParameter("_target1") != null) {     motorist.getVehicles().add(new Vehicle());   } } protected ModelAndView processFinish(HttpServletRequest request,    HttpServletResponse response, Object command,    BindException errors)    throws Exception {   Motorist motorist = (motorist) command;   // the last vehicle is always blank…remove it   motorist.getVehicles().remove(       motorist.getVehicles().size() – 1);   rantService.addMotorist(motorist);   return new ModelAndView(getSuccessView(),      "motorist", motorist); } // injected private RantService rantService; public void setRantService(RantService rantService) {   this.rantService = rantService; } // returns the last page as the success view private String getSuccessView() {   return getPages()[getPages().length-1]; }

==========================================================

没有人陪你走一辈子,所以你要适应孤独,

spring mvc 学习二

相关文章:

你感兴趣的文章:

标签云: