⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accountformcontroller.java

📁 Spring的项目
💻 JAVA
字号:
/**
 * 
 */
package carNumber.web;

import java.util.*;

import org.springframework.validation.BindException;
import org.springframework.dao.DataIntegrityViolationException;

import org.springframework.validation.ValidationUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
//
import org.apache.log4j.*;

import com.sun.org.apache.xerces.internal.impl.dv.DatatypeValidator;

import carNumber.service.CarNumberFacade;
import carNumber.domain.Account;
import carNumber.domain.AccountForm;
/**
 * @author xiaobin
 *
 */
public class AccountFormController extends SimpleFormController {
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	public static final String[] LANGUAGES = {"english", "chinese"};
	
	private CarNumberFacade carNumberFacade;
	
	public AccountFormController() {
		// TODO Auto-generated constructor stub
		setValidateOnBinding(false);
		setCommandName("actForm");
		setFormView("EditActForm");
	}
	
	public void setCarNumberFacde(CarNumberFacade cnf) {
		this.carNumberFacade = cnf;
	}
	
	protected Object formBackingObject() {
		return new AccountForm();
	}
	
	protected void onBindAndValidate(HttpServletRequest request, Object obj, 
			BindException/*this is spring's BindException!
			               not java.net.BindException */ bindError) throws Exception {
		logger.info("run: onBindAndValidate(HttpRequestHandler request, Object obj, " +
				"BindException bindError)");
		
		AccountForm actForm = (AccountForm) obj;
		Account act = actForm.getAccount();
		
		if(request.getParameter("account.listOption") == null) {
			act.setListOption(false);
		}
		if(request.getParameter("account.bannerOption") == null) {
			act.setBannerOption(false);
		}
		
		bindError.setNestedPath("account");
		getValidator().validate(act, bindError);
		bindError.setNestedPath("");
		
		if(actForm.isNewAccount()) {
			act.setStatus("OK");
			ValidationUtils.rejectIfEmpty(bindError, "account.username", "USER_ID_REQUIRED", "User id is required");
			if(act.getPassword() == null || act.getPassword().length() < 1 
					|| !act.getPassword().equals(actForm.getRepeatePassword())) {
				bindError.reject("PASSWORD_MISMATCH", 
						"Passwords did not match or were not provided. Matching passwords are required.");
			}
			else if(act.getPassword() != null && act.getPassword().length() > 0) {
				if(!act.getPassword().equals(actForm.getRepeatePassword())) {
					bindError.reject("PASSWORD_MISMATCH", "Passwords did not match. Matching passwords are required.");
				}
			}
		}
	}
	
	protected Map referenceData(HttpServletRequest request) throws Exception {
		logger.info("run: 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 bindErrors) throws Exception {
		logger.info("run: ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse," +
				" Object command, BindException bindErrors)");
		
		AccountForm actFrom = (AccountForm) command;
		
		try {
			if(actFrom.isNewAccount()){
				this.carNumberFacade.insertAccount(actFrom.getAccount());
			}
			else {
				this.carNumberFacade.updateAccount(actFrom.getAccount());
			}
		} catch (DataIntegrityViolationException DaoViolationEx) {
			// TODO: handle exception
			bindErrors.rejectValue("account.username", "USER_ID_ALREADY_EXISTS", 
					"User id already exists: Choose a different id");
			return showForm(request, response,bindErrors);
		}
		
		return super.onSubmit(request, response, command, bindErrors);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -