spring mvc 学习二
时间:2010-10-09 来源:vcycyv
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];
}
==========================================================
To create Next
and Back buttons on the page, all you must do is create submit buttons that are
appropriately named with the _target prefix:
<form method="POST" action="feedback.htm">
…
<input type="submit" value="Back" name="_target0">
<input type="submit" value="Next" name="_target2">
</form>
==========================================================
ThrowawayController都是prototype的
==============================================
<bean id="exceptionResolver" class="org.springframework.web.
? servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">friendlyError</prop>
</props>
</property>
</bean>
==============================
单词:
equanimity