📄 action.java
字号:
/*
* $Id: Action.java 164530 2005-04-25 03:11:07Z niallp $
*
* Copyright 2000-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.struts.action;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import org.apache.struts.Globals;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.TokenProcessor;
/**
* <p>An <strong>Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request. The controller (RequestProcessor) will select an
* appropriate Action for each request, create an instance (if necessary),
* and call the <code>execute</code> method.</p>
*
* <p>Actions must be programmed in a thread-safe manner, because the
* controller will share the same instance for multiple simultaneous
* requests. This means you should design with the following items in mind:
* </p>
* <ul>
* <li>Instance and static variables MUST NOT be used to store information
* related to the state of a particular request. They MAY be used to
* share global resources across requests for the same action.</li>
* <li>Access to other resources (JavaBeans, session variables, etc.) MUST
* be synchronized if those resources require protection. (Generally,
* however, resource classes should be designed to provide their own
* protection where necessary.</li>
* </ul>
*
* <p>When an <code>Action</code> instance is first created, the controller
* will call <code>setServlet</code> with a non-null argument to
* identify the servlet instance to which this Action is attached.
* When the servlet is to be shut down (or restarted), the
* <code>setServlet</code> method will be called with a <code>null</code>
* argument, which can be used to clean up any allocated resources in use
* by this Action.</p>
*
* @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
*/
public class Action {
/**
* <p>An instance of <code>TokenProcessor</code> to use for token functionality.</p>
*/
private static TokenProcessor token = TokenProcessor.getInstance();
// :TODO: We can make this variable protected and remove Action's token methods
// or leave it private and allow the token methods to delegate their calls.
// ----------------------------------------------------- Instance Variables
/**
* <p>The system default Locale.</p>
*
* @deprecated Use Locale.getDefault directly. This will be removed after
* Struts 1.2.
*/
protected static Locale defaultLocale = Locale.getDefault();
// :TODO: Remove after Struts 1.2
/**
* <p>The servlet to which we are attached.</p>
*/
protected ActionServlet servlet = null;
// ------------------------------------------------------------- Properties
/**
* <p>Return the servlet instance to which we are attached.</p>
*/
public ActionServlet getServlet() {
return (this.servlet);
}
/**
* <p>Set the servlet instance to which we are attached (if
* <code>servlet</code> is non-null), or release any allocated resources
* (if <code>servlet</code> is null).</p>
*
* @param servlet The new controller servlet, if any
*/
public void setServlet(ActionServlet servlet) {
this.servlet = servlet;
// :FIXME: Is this suppose to release resources?
}
// --------------------------------------------------------- Public Methods
/**
* <p>Process the specified non-HTTP request, and create the
* corresponding non-HTTP response (or forward to another web
* component that will create it), with provision for handling
* exceptions thrown by the business logic.
* Return an {@link ActionForward} instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.</p>
*
* <p>The default implementation attempts to forward to the HTTP
* version of this method.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The non-HTTP request we are processing
* @param response The non-HTTP response we are creating
*
* @exception Exception if the application business logic throws
* an exception.
* @since Struts 1.1
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
ServletRequest request,
ServletResponse response)
throws Exception {
try {
return execute(
mapping,
form,
(HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}
}
/**
* <p>Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it),
* with provision for handling exceptions thrown by the business logic.
* Return an {@link ActionForward} instance describing where and how
* control should be forwarded, or <code>null</code> if the response
* has already been completed.</p>
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
*
* @exception Exception if the application business logic throws
* an exception
* @since Struts 1.1
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return null;
}
// ---------------------------------------------------- Protected Methods
/**
* Adds 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.
* Initialize the attribute if it has not already been.
* Otherwise, ensure that the request attribute is not set.
*
* @param request The servlet request we are processing
* @param messages Messages object
* @since Struts 1.2.1
*/
protected void addMessages(
HttpServletRequest request,
ActionMessages messages) {
if (messages == null){
// bad programmer! *slap*
return;
}
// get any existing messages from the request, or make a new one
ActionMessages requestMessages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
if (requestMessages == null){
requestMessages = new ActionMessages();
}
// add incoming messages
requestMessages.add(messages);
// if still empty, just wipe it out from the request
if (requestMessages.isEmpty()) {
request.removeAttribute(Globals.MESSAGE_KEY);
return;
}
// Save the messages
request.setAttribute(Globals.MESSAGE_KEY, requestMessages);
}
/**
* Adds the specified errors keys into the appropriate request attribute
* for use by the <html:errors> tag, if any messages are required.
* Initialize the attribute if it has not already been. Otherwise, ensure
* that the request attribute is not set.
*
* @param request The servlet request we are processing
* @param errors Errors object
* @since Struts 1.2.1
*/
protected void addErrors(
HttpServletRequest request,
ActionMessages errors) {
if (errors == null){
// bad programmer! *slap*
return;
}
// get any existing errors from the request, or make a new one
ActionMessages requestErrors = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
if (requestErrors == null){
requestErrors = new ActionMessages();
}
// add incoming errors
requestErrors.add(errors);
// if still empty, just wipe it out from the request
if (requestErrors.isEmpty()) {
request.removeAttribute(Globals.ERROR_KEY);
return;
}
// Save the errors
request.setAttribute(Globals.ERROR_KEY, requestErrors);
}
/**
* <p>Generate a new transaction token, to be used for enforcing a single
* request for a particular transaction.</p>
*
* @param request The request we are processing
*/
protected String generateToken(HttpServletRequest request) {
return token.generateToken(request);
}
/**
* <p>Return the default data source for the current module.</p>
*
* @param request The servlet request we are processing
*
* @since Struts 1.1
*/
protected DataSource getDataSource(HttpServletRequest request) {
return (getDataSource(request, Globals.DATA_SOURCE_KEY));
}
/**
* <p>Return the specified data source for the current module.</p>
*
* @param request The servlet request we are processing
* @param key The key specified in the <code><data-sources></code>
* element.
*
* @since Struts 1.1
*/
protected DataSource getDataSource(HttpServletRequest request, String key) {
// Identify the current module
ServletContext context = getServlet().getServletContext();
ModuleConfig moduleConfig =
ModuleUtils.getInstance().getModuleConfig(request, context);
return (DataSource) context.getAttribute(key + moduleConfig.getPrefix());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -