转载: 以下所有资料由程序自动摘自 http://www.youkuaiyun.com
public class AccountFormController extends SimpleFormController {
public static final String[] LANGUAGES = {"english", "japanese"};
private PetStoreFacade petStore;
public AccountFormController() {
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
setFormView("newUser");
}
public void setPetStore(PetStoreFacade petStore) {
this.petStore = petStore;
System.out.println(this.getClass()+"this.petStore = petStore;");
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
if (userSession != null) {
return new AccountForm(this.petStore.getUser(userSession.getUsers().getId().intValue()));
}else{
return new AccountForm();
}
}
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
throws Exception {
AccountForm accountForm = (AccountForm) command;
Users users = accountForm.getUsers();
errors.setNestedPath("users");
getValidator().validate(users, errors);
errors.setNestedPath("");
if(accountForm.isNewAccount()) {
ValidationUtils.rejectIfEmpty(errors, "users.username", "USER_ID_REQUIRED", "User ID is required.");
if(users.getPassword()==null|| users.getPassword().length() < 1 ||
!users.getPassword().equals(accountForm.getRepeatedPassword())) {
errors.reject("PASSWORD_MISMATCH","Passwords did not match or were not provided. Matching passwords are required.");
}
}else if (users.getPassword() != null && users.getPassword().length()>0){
if(!users.getPassword().equals(accountForm.getRepeatedPassword()))
{
errors.reject("PASSWORD_MISMATCH","Passwords did not match. Matching passwords are required.");
}
}
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("languages", LANGUAGES);
return model;
}
protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
AccountForm accountForm = (AccountForm) command;
try {
if(accountForm.isNewAccount())
{
this.petStore.insertUsers(accountForm.getUsers());
}else{
this.petStore.updateUsers(accountForm.getUsers());
}
}catch (DataIntegrityViolationException ex){
errors.rejectValue("account.username","USER_ID_ALREADY_EXISTS","User ID already exists: choose a different ID.");
return showForm(request, response, errors);
}
UserSession userSession = new UserSession(this.petStore.getUser(accountForm.getUsers().getId().intValue()));
request.getSession().setAttribute("userSession", userSession);
return super.onSubmit(request, response, command, errors);
}
在以上该formController中
能帮我解释一下:
1、
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
这三句是干什么用的有什么左右??
2、
formBackingObject()该方法有什么作用?
3、
onBindAndValidate()该方法有什么作用?
4、
referenceData()该方法有什么作用?
public static final String[] LANGUAGES = {"english", "japanese"};
private PetStoreFacade petStore;
public AccountFormController() {
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
setFormView("newUser");
}
public void setPetStore(PetStoreFacade petStore) {
this.petStore = petStore;
System.out.println(this.getClass()+"this.petStore = petStore;");
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
if (userSession != null) {
return new AccountForm(this.petStore.getUser(userSession.getUsers().getId().intValue()));
}else{
return new AccountForm();
}
}
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
throws Exception {
AccountForm accountForm = (AccountForm) command;
Users users = accountForm.getUsers();
errors.setNestedPath("users");
getValidator().validate(users, errors);
errors.setNestedPath("");
if(accountForm.isNewAccount()) {
ValidationUtils.rejectIfEmpty(errors, "users.username", "USER_ID_REQUIRED", "User ID is required.");
if(users.getPassword()==null|| users.getPassword().length() < 1 ||
!users.getPassword().equals(accountForm.getRepeatedPassword())) {
errors.reject("PASSWORD_MISMATCH","Passwords did not match or were not provided. Matching passwords are required.");
}
}else if (users.getPassword() != null && users.getPassword().length()>0){
if(!users.getPassword().equals(accountForm.getRepeatedPassword()))
{
errors.reject("PASSWORD_MISMATCH","Passwords did not match. Matching passwords are required.");
}
}
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("languages", LANGUAGES);
return model;
}
protected ModelAndView onSubmit(
HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
throws Exception {
AccountForm accountForm = (AccountForm) command;
try {
if(accountForm.isNewAccount())
{
this.petStore.insertUsers(accountForm.getUsers());
}else{
this.petStore.updateUsers(accountForm.getUsers());
}
}catch (DataIntegrityViolationException ex){
errors.rejectValue("account.username","USER_ID_ALREADY_EXISTS","User ID already exists: choose a different ID.");
return showForm(request, response, errors);
}
UserSession userSession = new UserSession(this.petStore.getUser(accountForm.getUsers().getId().intValue()));
request.getSession().setAttribute("userSession", userSession);
return super.onSubmit(request, response, command, errors);
}
在以上该formController中
能帮我解释一下:
1、
setSessionForm(true);
setValidateOnBinding(false);
setCommandName("accountForm");
这三句是干什么用的有什么左右??
2、
formBackingObject()该方法有什么作用?
3、
onBindAndValidate()该方法有什么作用?
4、
referenceData()该方法有什么作用?
----------------------------------------------------------------
搂主看得是springMVC的petstore例子吧。如果你理解了springMVC,相信这3句话很好理解。
1
setSessionForm(true); //使form在session范围内有效
setValidateOnBinding(false);//是否绑定了验证
setCommandName("accountForm");//你的commandName是这个accountForm,相当于struts的form
2
formBackingObject()方法是回传数据到表单,最后返回的是Command对象
3,onBindAndValidate(),当你表单的数据已经绑定上,但还没开始验证之前,你可以在这个方法中作一些预处理。
4,referenceData()方法是方便你在JSP页面对一些数据做出调用的。
比如你方法当中
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("languages", LANGUAGES);
return model;
}
jsp便可从页面${languages},获得你绑定的值
1
setSessionForm(true); //使form在session范围内有效
setValidateOnBinding(false);//是否绑定了验证
setCommandName("accountForm");//你的commandName是这个accountForm,相当于struts的form
2
formBackingObject()方法是回传数据到表单,最后返回的是Command对象
3,onBindAndValidate(),当你表单的数据已经绑定上,但还没开始验证之前,你可以在这个方法中作一些预处理。
4,referenceData()方法是方便你在JSP页面对一些数据做出调用的。
比如你方法当中
protected Map referenceData(HttpServletRequest request) throws Exception {
Map model = new HashMap();
model.put("languages", LANGUAGES);
return model;
}
jsp便可从页面${languages},获得你绑定的值