📄 action.java
字号:
/**
* Retrieves any existing errors placed in the request by previous actions. This method could be called instead
* of creating a <code>new ActionMessages()<code> at the beginning of an <code>Action<code>
* This will prevent saveErrors() from wiping out any existing Errors
*
* @return the Errors that already exist in the request, or a new ActionMessages object if empty.
* @param request The servlet request we are processing
* @since Struts 1.2.1
*/
protected ActionMessages getErrors(HttpServletRequest request) {
ActionMessages errors =
(ActionMessages) request.getAttribute(Globals.ERROR_KEY);
if (errors == null) {
errors = new ActionMessages();
}
return errors;
}
/**
* <p>Return the user's currently selected Locale.</p>
*
* @param request The request we are processing
*/
protected Locale getLocale(HttpServletRequest request) {
return RequestUtils.getUserLocale(request, null);
}
/**
* Retrieves any existing messages placed in the request by previous actions. This method could be called instead
* of creating a <code>new ActionMessages()<code> at the beginning of an <code>Action<code>
* This will prevent saveMessages() from wiping out any existing Messages
*
* @return the Messages that already exist in the request, or a new ActionMessages object if empty.
* @param request The servlet request we are processing
* @since Struts 1.2.1
*/
protected ActionMessages getMessages(HttpServletRequest request) {
ActionMessages messages =
(ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
if (messages == null) {
messages = new ActionMessages();
}
return messages;
}
/**
* <p>Return the default message resources for the current module.</p>
*
* @param request The servlet request we are processing
* @since Struts 1.1
*/
protected MessageResources getResources(HttpServletRequest request) {
return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));
}
/**
* <p>Return the specified message resources for the current module.</p>
*
* @param request The servlet request we are processing
* @param key The key specified in the
* <code><message-resources></code> element for the
* requested bundle
*
* @since Struts 1.1
*/
protected MessageResources getResources(
HttpServletRequest request,
String key) {
// Identify the current module
ServletContext context = getServlet().getServletContext();
ModuleConfig moduleConfig =
ModuleUtils.getInstance().getModuleConfig(request, context);
// Return the requested message resources instance
return (MessageResources) context.getAttribute(
key + moduleConfig.getPrefix());
}
/**
* <p>Returns <code>true</code> if the current form's cancel button was
* pressed. This method will check if the <code>Globals.CANCEL_KEY</code>
* request attribute has been set, which normally occurs if the cancel
* button generated by <strong>CancelTag</strong> was pressed by the user
* in the current request. If <code>true</code>, validation performed
* by an <strong>ActionForm</strong>'s <code>validate()</code> method
* will have been skipped by the controller servlet.</p>
*
* @param request The servlet request we are processing
* @see org.apache.struts.taglib.html.CancelTag
*/
protected boolean isCancelled(HttpServletRequest request) {
return (request.getAttribute(Globals.CANCEL_KEY) != null);
}
/**
* <p>Return <code>true</code> if there is a transaction token stored in
* the user's current session, and the value submitted as a request
* parameter with this action matches it. Returns <code>false</code>
* under any of the following circumstances:</p>
* <ul>
* <li>No session associated with this request</li>
* <li>No transaction token saved in the session</li>
* <li>No transaction token included as a request parameter</li>
* <li>The included transaction token value does not match the
* transaction token in the user's session</li>
* </ul>
*
* @param request The servlet request we are processing
*/
protected boolean isTokenValid(HttpServletRequest request) {
return token.isTokenValid(request, false);
}
/**
* <p>Return <code>true</code> if there is a transaction token stored in
* the user's current session, and the value submitted as a request
* parameter with this action matches it. Returns <code>false</code> under
* any of the following circumstances:</p>
* <ul>
* <li>No session associated with this request</li>
* <li>No transaction token saved in the session</li>
* <li>No transaction token included as a request parameter</li>
* <li>The included transaction token value does not match the
* transaction token in the user's session</li>
* </ul>
*
* @param request The servlet request we are processing
* @param reset Should we reset the token after checking it?
*/
protected boolean isTokenValid(HttpServletRequest request, boolean reset) {
return token.isTokenValid(request, reset);
}
/**
* <p>Reset the saved transaction token in the user's session. This
* indicates that transactional token checking will not be needed
* on the next request that is submitted.</p>
*
* @param request The servlet request we are processing
*/
protected void resetToken(HttpServletRequest request) {
token.resetToken(request);
}
/**
* <p>Save the specified error messages keys into the appropriate request
* attribute for use by the <html:errors> tag, if any messages
* are required. Otherwise, ensure that the request attribute is not
* created.</p>
*
* @param request The servlet request we are processing
* @param errors Error messages object
* @deprecated Use saveErrors(HttpServletRequest, ActionMessages) instead.
* This will be removed after Struts 1.2.
*/
protected void saveErrors(HttpServletRequest request, ActionErrors errors) {
this.saveErrors(request,(ActionMessages)errors);
// :TODO: Remove after Struts 1.2.
}
/**
* <p>Save the specified error messages keys into the appropriate request
* attribute for use by the <html:errors> tag, if any messages
* are required. Otherwise, ensure that the request attribute is not
* created.</p>
*
* @param request The servlet request we are processing
* @param errors Error messages object
* @since Struts 1.2
*/
protected void saveErrors(HttpServletRequest request, ActionMessages errors) {
// Remove any error messages attribute if none are required
if ((errors == null) || errors.isEmpty()) {
request.removeAttribute(Globals.ERROR_KEY);
return;
}
// Save the error messages we need
request.setAttribute(Globals.ERROR_KEY, errors);
}
/**
* <p>Save the specified messages keys into the appropriate request
* attribute for use by the <html:messages> tag (if
* messages="true" is set), if any messages are required. Otherwise,
* ensure that the request attribute is not created.</p>
*
* @param request The servlet request we are processing.
* @param messages The messages to save. <code>null</code> or empty
* messages removes any existing ActionMessages in the request.
*
* @since Struts 1.1
*/
protected void saveMessages(
HttpServletRequest request,
ActionMessages messages) {
// Remove any messages attribute if none are required
if ((messages == null) || messages.isEmpty()) {
request.removeAttribute(Globals.MESSAGE_KEY);
return;
}
// Save the messages we need
request.setAttribute(Globals.MESSAGE_KEY, messages);
}
/**
* <p>Save the specified messages keys into the appropriate session
* attribute for use by the <html:messages> tag (if
* messages="true" is set), if any messages are required. Otherwise,
* ensure that the session attribute is not created.</p>
*
* @param session The session to save the messages in.
* @param messages The messages to save. <code>null</code> or empty
* messages removes any existing ActionMessages in the session.
*
* @since Struts 1.2
*/
protected void saveMessages(
HttpSession session,
ActionMessages messages) {
// Remove any messages attribute if none are required
if ((messages == null) || messages.isEmpty()) {
session.removeAttribute(Globals.MESSAGE_KEY);
return;
}
// Save the messages we need
session.setAttribute(Globals.MESSAGE_KEY, messages);
}
/**
* <p>Save the specified error messages keys into the appropriate session
* attribute for use by the <html:messages> tag (if messages="false")
* or <html:errors>, if any error messages are required. Otherwise,
* ensure that the session attribute is empty.</p>
*
* @param session The session to save the error messages in.
* @param errors The error messages to save. <code>null</code> or empty
* messages removes any existing error ActionMessages in the session.
*
* @since Struts 1.2.7
*/
protected void saveErrors(
HttpSession session,
ActionMessages errors) {
// Remove the error attribute if none are required
if ((errors == null) || errors.isEmpty()) {
session.removeAttribute(Globals.ERROR_KEY);
return;
}
// Save the errors we need
session.setAttribute(Globals.ERROR_KEY, errors);
}
/**
* <p>Save a new transaction token in the user's current session, creating
* a new session if necessary.</p>
*
* @param request The servlet request we are processing
*/
protected void saveToken(HttpServletRequest request) {
token.saveToken(request);
}
/**
* <p>Set the user's currently selected <code>Locale</code> into their
* <code>HttpSession</code>.</p>
*
* @param request The request we are processing
* @param locale The user's selected Locale to be set, or null
* to select the server's default Locale
*/
protected void setLocale(HttpServletRequest request, Locale locale) {
HttpSession session = request.getSession();
if (locale == null) {
locale = Locale.getDefault();
}
session.setAttribute(Globals.LOCALE_KEY, locale);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -