📄 baseformcontroller.java
字号:
package net.java.workeffort.webapp.action;import java.lang.reflect.Method;import java.math.BigDecimal;import java.text.NumberFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.java.workeffort.infrastructure.RandomGuid;import net.java.workeffort.infrastructure.context.RequestContextHolder;import net.java.workeffort.infrastructure.exception.IPropertyException;import net.java.workeffort.infrastructure.security.ISecurityProfile;import net.java.workeffort.infrastructure.view.ViewHelper;import net.java.workeffort.webapp.support.CodeAndArguments;import net.java.workeffort.webapp.support.WebConstants;import net.java.workeffort.webapp.support.WebappUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.beans.propertyeditors.CustomNumberEditor;import org.springframework.beans.propertyeditors.StringTrimmerEditor;import org.springframework.validation.BindException;import org.springframework.validation.DataBinder;import org.springframework.validation.Errors;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import org.springframework.web.servlet.support.RequestContextUtils;import org.springframework.web.util.UrlPathHelper;/** * The base controller that most other controllers extend. * <p> * <b>Exposed configuration properties: </b> <br> * <table border="1"> * <tr> * <td><b>Name </b></td> * <td><b>Default </b></td> * <td><b>Description </b></td> * </tr> * <tr> * <td>lovServiceService</td> * <td>'listOfValuesService' if lovList is not null</td> * <td>The list of values service</td> * </tr> * <tr> * <td>lovList</td> * <td>null</td> * <td>The list of values used for the action. The names should match the * method names available in 'lovService'</td> * </tr> * <tr> * <td>validator</td> * <td>null</td> * <td>The validator. (Note that this is the property of * <code>SimpleFormController</code> which this class extends)</td> * </tr> * </table> * @author Antony Joseph */public abstract class BaseFormController extends SimpleFormController implements InitializingBean, BeanNameAware { public static final String TOKEN_ATTRIBUTE_NAME = "txToken"; public static final String TOKEN_PARAMETER_NAME = "_txToken"; public static final String MESSAGES_ATTRIBUTE_NAME = "messages"; public static final String DISPATCH_PARAMETER_NAME = "dispatch"; public static final String INSERT = "insert"; public static final String UPDATE = "update"; public static final String DELETE = "delete"; public static final String QUERY = "query"; public static final String SAVE = "save"; public static final String NEW = "new"; public static final String SKIP_ERRORS = "skipErrors"; private Object lovService; protected Object service; private List lovList; private Map lovMethodCache; protected String beanName; /** * Initialize */ public void afterPropertiesSet() { init(); } /** * Initialize Sub classes can override this. */ protected void init() { initLov(); } /** * Initialize the list of values. */ protected void initLov() { if (lovList != null) { if (lovService == null) { lovService = getWebApplicationContext().getBean( "listOfValuesService"); } lovMethodCache = WebappUtils.getLovMethods(lovList, lovService); } } /** * Since the application uses generic controllers the beanName is used to * create the form session attribute names to make is easy to differentiate * the forms stored in session scope while debugging */ public void setBeanName(String beanName) { this.beanName = beanName; } /** * @return Returns the service. */ public Object getService() { return service; } /** * @param service The service to set. */ public void setService(Object service) { this.service = service; } /** * @return Returns the lovService. */ public Object getLovService() { return lovService; } /** * @param lovService The lovService to set. */ public void setLovService(Object lovService) { this.lovService = lovService; } /** * @return Returns the lovList. */ public List getLovList() { return lovList; } /** * @param lovList The lovList to set. */ public void setLovList(List lovList) { this.lovList = lovList; } /** * @return Returns the lovMethodCache. */ public Map getLovMethodCache() { return lovMethodCache; } protected void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception { // if there is a validator configured and validateOnBind is false and // there are no binding errors then invoke the validator. if (getValidator() != null && !isValidateOnBinding() && !errors.hasErrors()) { getValidator().validate(command, errors); } } protected Map referenceData(HttpServletRequest request) throws Exception { if (logger.isInfoEnabled()) logger.info("referenceData invoked."); return WebappUtils.referenceLovData(getLovMethodCache(), getLovService()); } /** * initialize the data binder * @param request The http request * @param binder the servlet request data binder */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { initDataBinder(request, (DataBinder) binder); } /** * initialize the data binder * @param request The http request * @param binder the data binder */ protected void initDataBinder(HttpServletRequest request, DataBinder binder) { if (logger.isInfoEnabled()) logger.info("initDataBinder() invoked."); NumberFormat nf = NumberFormat.getNumberInstance(); binder.registerCustomEditor(Integer.class, new CustomNumberEditor( Integer.class, nf, true)); binder.registerCustomEditor(Long.class, new CustomNumberEditor( Long.class, nf, true)); binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor( BigDecimal.class, nf, true)); binder .registerCustomEditor(String.class, new StringTrimmerEditor( true)); binder.registerCustomEditor(Date.class, null, new CustomDateEditor( getDateFormatter(request), true)); } /** * create a new BindException with the appropriate custom editors * @param request the http request * @param command the command object * @param commandName the command name; */ protected BindException createNewBindException(HttpServletRequest request, Object command, String commandName) { DataBinder binder = new DataBinder(command, commandName); initDataBinder(request, binder); return binder.getErrors(); } /** * Save a new transaction token in session. * @param request The request */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -