📄 actioncontextbase.java
字号:
public void saveMessages(ActionMessages messages) {
this.saveActionMessages(MESSAGE_ACTION_MESSAGES_KEY, messages);
}
// ISSUE: do we want to add this to the public API?
/**
* <p> Add the given messages to a cache stored in this Context, under
* key. </p>
*
* @param key The attribute name for the message cache
* @param messages The ActionMessages to add
*/
public void addActionMessages(String key, ActionMessages messages) {
if (messages == null) {
// bad programmer! *slap*
return;
}
// get any existing messages from the request, or make a new one
ActionMessages requestMessages = (ActionMessages) this.get(key);
if (requestMessages == null) {
requestMessages = new ActionMessages();
}
// add incoming messages
requestMessages.add(messages);
// if still empty, just wipe it out from the request
this.remove(key);
// save the messages
this.saveActionMessages(key, requestMessages);
}
// ISSUE: do we want to add this to the public API?
/**
* <p> Save the given ActionMessages into the request scope under the
* given key, clearing the attribute if the messages are empty or null.
* </p>
*
* @param key The attribute name for the message cache
* @param messages The ActionMessages to add
*/
public void saveActionMessages(String key, ActionMessages messages) {
this.saveActionMessages(REQUEST_SCOPE, key, messages);
}
/**
* <p>Save the given <code>messages</code> into the map identified by the
* given <code>scopeId</code> under the given <code>key</code>.</p>
*
* @param scopeId
* @param key
* @param messages
*/
public void saveActionMessages(String scopeId, String key,
ActionMessages messages) {
Map scope = getScope(scopeId);
if ((messages == null) || messages.isEmpty()) {
scope.remove(key);
return;
}
scope.put(key, messages);
}
// ISSUE: Should we deprecate this method, since it is misleading?
// Do we need it for backward compatibility?
/**
* <p> Adapt a legacy form of SaveMessages to the ActionContext API by
* storing the ActoinMessages under the default scope.
*
* @param scope The scope for the internal cache
* @param messages ActionMesssages to cache
*/
public void saveMessages(String scope, ActionMessages messages) {
this.saveMessages(messages);
}
// -------------------------------
// Token Processing
// -------------------------------
// ISSUE: Should there be a getToken method?
// Is there a problem trying to map this method from Action
// to ActionContext when we aren't necessarily sure how token
// processing maps into a context with an ill-defined "session"?
// There's no getToken() method, but maybe there should be. *
public void saveToken() {
String token = this.generateToken();
this.put(TRANSACTION_TOKEN_KEY, token);
}
public String generateToken() {
return token.generateToken(getTokenGeneratorId());
}
// ISSUE: The original implementation was based on the HttpSession
// identifier; what would be a way to do that without depending on the
// Servlet API?
// REPLY: uuid's
// http://java.sun.com/products/jini/2.0/doc/specs/api/net/jini/id/Uuid.html
protected String getTokenGeneratorId() {
return "";
}
public boolean isTokenValid() {
return this.isTokenValid(false);
}
public boolean isTokenValid(boolean reset) {
// Retrieve the transaction token from this session, and
// reset it if requested
String saved = (String) this.get(TRANSACTION_TOKEN_KEY);
if (saved == null) {
return false;
}
if (reset) {
this.resetToken();
}
// Retrieve the transaction token included in this request
String token = (String) this.get(TOKEN_KEY);
if (token == null) {
return false;
}
return saved.equals(token);
}
public void resetToken() {
this.remove(TRANSACTION_TOKEN_KEY);
}
// -------------------------------
// Cancel Processing
// -------------------------------
public Boolean getCancelled() {
return (Boolean) this.get(CANCEL_KEY);
}
public void setCancelled(Boolean cancelled) {
this.put(CANCEL_KEY, cancelled);
}
// -------------------------------
// MessageResources Processing
// -------------------------------
public void setMessageResources(MessageResources messageResources) {
this.put(MESSAGE_RESOURCES_KEY, messageResources);
}
public MessageResources getMessageResources() {
return (MessageResources) this.get(MESSAGE_RESOURCES_KEY);
}
public MessageResources getMessageResources(String key) {
return (MessageResources) this.get(key);
}
// -------------------------------
// Locale Processing
// -------------------------------
public void setLocale(Locale locale) {
this.put(LOCALE_KEY, locale);
}
public Locale getLocale() {
return (Locale) this.get(LOCALE_KEY);
}
// -------------------------------
// Convenience Methods: these are not part of the formal ActionContext API,
// but are likely to be commonly useful.
// -------------------------------
/**
* <p> Provide the currently configured commons-logging <code>Log</code>
* instance. </p>
*
* @return Log instance for this context
*/
public Log getLogger() {
return this.logger;
}
/**
* <p> Set the commons-logging <code>Log</code> instance which should be
* used to LOG messages. This is initialized at instantiation time but may
* be overridden. Be advised not to set the value to null, as
* <code>ActionContextBase</code> uses the logger for some of its own
* operations. </p>
*/
public void setLogger(Log logger) {
this.logger = logger;
}
/**
* <p> Using this <code>ActionContext</code>'s default
* <code>ModuleConfig</code>, return an existing <code>ActionForm</code>
* in the specified scope, or create a new one and add it to the specified
* scope. </p>
*
* @param formName The name attribute of our ActionForm
* @param scopeName The scope identier (request, session)
* @return The ActionForm for this request
* @throws IllegalAccessException If object cannot be created
* @throws InstantiationException If object cannot be created
* @see this.findOrCreateActionForm(String, String, ModuleConfig)
*/
public ActionForm findOrCreateActionForm(String formName, String scopeName)
throws IllegalAccessException, InstantiationException {
return this.findOrCreateActionForm(formName, scopeName,
this.getModuleConfig());
}
/**
* <p> In the context of the given <code>ModuleConfig</code> and this
* <code>ActionContext</code>, look for an existing
* <code>ActionForm</code> in the specified scope. If one is found, return
* it; otherwise, create a new instance, add it to that scope, and then
* return it. </p>
*
* @param formName The name attribute of our ActionForm
* @param scopeName The scope identier (request, session)
* @return The ActionForm for this request
* @throws IllegalAccessException If object cannot be created
* @throws InstantiationException If object cannot be created
* @throws IllegalArgumentException If form config is missing from module
* or scopeName is invalid
*/
public ActionForm findOrCreateActionForm(String formName, String scopeName,
ModuleConfig moduleConfig)
throws IllegalAccessException, InstantiationException {
Map scope = this.getScope(scopeName);
ActionForm instance;
FormBeanConfig formBeanConfig =
moduleConfig.findFormBeanConfig(formName);
if (formBeanConfig == null) {
throw new IllegalArgumentException("No form config found under "
+ formName + " in module " + moduleConfig.getPrefix());
}
instance = (ActionForm) scope.get(formName);
// ISSUE: Can we recycle the existing instance (if any)?
if (instance != null) {
getLogger().trace("Found an instance in scope " + scopeName
+ "; test for reusability");
if (formBeanConfig.canReuse(instance)) {
return instance;
}
}
ActionForm form = formBeanConfig.createActionForm(this);
// ISSUE: Should we check this call to put?
scope.put(formName, form);
return form;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -