abstractclinicform.java

来自「Java/J2EE application framework based on」· Java 代码 · 共 62 行

JAVA
62
字号
package org.springframework.samples.petclinic.web;

import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 * JavaBean abstract base class for petclinic-aware form controllers.
 * Provides convenience methods for subclasses.
 *
 * @author Ken Krebs
 */
abstract public class AbstractClinicForm extends SimpleFormController {

	private Clinic clinic;

	public void setClinic(Clinic clinic) {
		this.clinic = clinic;
	}

	protected Clinic getClinic() {
		return this.clinic;
	}

	public void afterPropertiesSet() {
		if (this.clinic == null) {
			throw new IllegalArgumentException("'clinic' is required");
		}
	}

	/**
	 * Set up a custom property editor for the application's date format.
	 */
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, false));
	}

	/**
	 * Method disallows duplicate form submission.
	 * Typically used to prevent duplicate insertion of <code>Entity</code>s
	 * into the datastore. Shows a new form with an error message.
	 */
	protected ModelAndView disallowDuplicateFormSubmission(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BindException errors = new BindException(formBackingObject(request), getCommandName());
		errors.reject("duplicateFormSubmission", "Duplicate form submission");
		return showForm(request, response, errors);
	}

}

⌨️ 快捷键说明

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