Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.appfuse.webapp.controller;
  
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;

Implementation of SimpleFormController that contains convenience methods for subclasses. For example, getting the current user and saving messages/errors. This class is intended to be a base class for all Form controllers.

View Source

Author(s):
Matt Raible
 
 public class BaseFormController implements ServletContextAware {
   protected final transient Log log = LogFactory.getLog(getClass());
   public static final String MESSAGES_KEY = "successMessages";
   protected String cancelView;
   protected String successView;
 
 
   @Autowired(required = false)
 
   public void setMessages(MessageSource messageSource) {
      = new MessageSourceAccessor(messageSource);
   }
 
   @SuppressWarnings("unchecked")
   public void saveError(HttpServletRequest requestString error) {
     List errors = (Listrequest.getSession().getAttribute("errors");
     if (errors == null) {
       errors = new ArrayList();
     }
     errors.add(error);
     request.getSession().setAttribute("errors"errors);
   }
 
   @SuppressWarnings("unchecked")
   public void saveMessage(HttpServletRequest requestString msg) {
     List messages = (Listrequest.getSession().getAttribute();
 
     if (messages == null) {
       messages = new ArrayList();
     }
 
     messages.add(msg);
     request.getSession().setAttribute(messages);
   }

  
Convenience method for getting a i18n key's value. Calling getMessageSourceAccessor() is used because the RequestContext variable is not set in unit tests b/c there's no DispatchServlet Request.

Parameters:
msgKey
locale the current locale
Returns:
 
   public String getText(String msgKeyLocale locale) {
     return .getMessage(msgKeylocale);
   }

  
Convenient method for getting a i18n key's value with a single string argument.

Parameters:
msgKey
arg
locale the current locale
Returns:
 
   public String getText(String msgKeyString argLocale locale) {
     return getText(msgKeynew Object[]{arg}, locale);
  }

  
Convenience method for getting a i18n key's value with arguments.

Parameters:
msgKey
args
locale the current locale
Returns:
  public String getText(String msgKeyObject[] argsLocale locale) {
    return .getMessage(msgKeyargslocale);
  }

  
Convenience method to get the Configuration HashMap from the servlet context.

Returns:
the user's populated form from the session
  public Map getConfiguration() {
    // so unit tests don't puke when nothing's been set
    if (config == null) {
      return new HashMap();
    }
    return config;
  }

  
Set up a custom property editor for converting form inputs to real objects

Parameters:
request the current request
binder the data binder
  protected void initBinder(HttpServletRequest request,
                            ServletRequestDataBinder binder) {
    binder.registerCustomEditor(Integer.classnull,
        new CustomNumberEditor(Integer.classnulltrue));
    binder.registerCustomEditor(Long.classnull,
        new CustomNumberEditor(Long.classnulltrue));
    binder.registerCustomEditor(byte[].class,
        new ByteArrayMultipartFileEditor());
    SimpleDateFormat dateFormat =
        new SimpleDateFormat(getText("date.format"request.getLocale()));
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.classnull,
        new CustomDateEditor(dateFormattrue));
  }
  public final BaseFormController setCancelView(String cancelView) {
    this. = cancelView;
    return this;
  }
  public final String getCancelView() {
    // Default to successView if cancelView is invalid
    if (this. == null || this..length() == 0) {
      return getSuccessView();
    }
    return this.;
  }
  public final String getSuccessView() {
    return this.;
  }
  public final BaseFormController setSuccessView(String successView) {
    this. = successView;
    return this;
  }
  public void setServletContext(ServletContext servletContext) {
    this. = servletContext;
  }
    return ;
  }
New to GrepCode? Check out our FAQ X