?? actionservlet.java
字號:
* @throws ServletException if we cannot configure ourselves correctly
*/
public void init() throws ServletException {
final String configPrefix = "config/";
final int configPrefixLength = configPrefix.length() - 1;
// Wraps the entire initialization in a try/catch to better handle
// unexpected exceptions and errors to provide better feedback
// to the developer
try {
initInternal();
initOther();
initServlet();
initChain();
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
initModuleConfigFactory();
// Initialize modules as needed
ModuleConfig moduleConfig = initModuleConfig("", config);
initModuleMessageResources(moduleConfig);
initModulePlugIns(moduleConfig);
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
moduleConfig.freeze();
Enumeration names = getServletConfig().getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (!name.startsWith(configPrefix)) {
continue;
}
String prefix = name.substring(configPrefixLength);
moduleConfig =
initModuleConfig(prefix,
getServletConfig().getInitParameter(name));
initModuleMessageResources(moduleConfig);
initModulePlugIns(moduleConfig);
initModuleFormBeans(moduleConfig);
initModuleForwards(moduleConfig);
initModuleExceptionConfigs(moduleConfig);
initModuleActions(moduleConfig);
moduleConfig.freeze();
}
this.initModulePrefixes(this.getServletContext());
this.destroyConfigDigester();
} catch (UnavailableException ex) {
throw ex;
} catch (Throwable t) {
// The follow error message is not retrieved from internal message
// resources as they may not have been able to have been
// initialized
log.error("Unable to initialize Struts ActionServlet due to an "
+ "unexpected exception or error thrown, so marking the "
+ "servlet as unavailable. Most likely, this is due to an "
+ "incorrect or missing library dependency.", t);
throw new UnavailableException(t.getMessage());
}
}
/**
* <p>Saves a String[] of module prefixes in the ServletContext under
* Globals.MODULE_PREFIXES_KEY. <strong>NOTE</strong> - the "" prefix for
* the default module is not included in this list.</p>
*
* @param context The servlet context.
* @since Struts 1.2
*/
protected void initModulePrefixes(ServletContext context) {
ArrayList prefixList = new ArrayList();
Enumeration names = context.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
if (!name.startsWith(Globals.MODULE_KEY)) {
continue;
}
String prefix = name.substring(Globals.MODULE_KEY.length());
if (prefix.length() > 0) {
prefixList.add(prefix);
}
}
String[] prefixes =
(String[]) prefixList.toArray(new String[prefixList.size()]);
context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
}
/**
* <p>Process an HTTP "GET" request.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
/**
* <p>Process an HTTP "POST" request.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @throws IOException if an input/output error occurs
* @throws ServletException if a servlet exception occurs
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
// --------------------------------------------------------- Public Methods
/**
* <p>Remember a servlet mapping from our web application deployment
* descriptor, if it is for this servlet.</p>
*
* @param servletName The name of the servlet being mapped
* @param urlPattern The URL pattern to which this servlet is mapped
*/
public void addServletMapping(String servletName, String urlPattern) {
if (servletName == null) {
return;
}
if (servletName.equals(this.servletName)) {
if (log.isDebugEnabled()) {
log.debug("Process servletName=" + servletName
+ ", urlPattern=" + urlPattern);
}
this.servletMapping = urlPattern;
}
}
/**
* <p>Return the <code>MessageResources</code> instance containing our
* internal message strings.</p>
*
* @return the <code>MessageResources</code> instance containing our
* internal message strings.
* @since Struts 1.1
*/
public MessageResources getInternal() {
return (this.internal);
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Gracefully terminate use of any modules associated with this
* application (if any).</p>
*
* @since Struts 1.1
*/
protected void destroyModules() {
ArrayList values = new ArrayList();
Enumeration names = getServletContext().getAttributeNames();
while (names.hasMoreElements()) {
values.add(names.nextElement());
}
Iterator keys = values.iterator();
while (keys.hasNext()) {
String name = (String) keys.next();
Object value = getServletContext().getAttribute(name);
if (!(value instanceof ModuleConfig)) {
continue;
}
ModuleConfig config = (ModuleConfig) value;
if (this.getProcessorForModule(config) != null) {
this.getProcessorForModule(config).destroy();
}
getServletContext().removeAttribute(name);
PlugIn[] plugIns =
(PlugIn[]) getServletContext().getAttribute(Globals.PLUG_INS_KEY
+ config.getPrefix());
if (plugIns != null) {
for (int i = 0; i < plugIns.length; i++) {
int j = plugIns.length - (i + 1);
plugIns[j].destroy();
}
getServletContext().removeAttribute(Globals.PLUG_INS_KEY
+ config.getPrefix());
}
}
}
/**
* <p>Gracefully release any configDigester instance that we have created.
* </p>
*
* @since Struts 1.1
*/
protected void destroyConfigDigester() {
configDigester = null;
}
/**
* <p>Gracefully terminate use of the internal MessageResources.</p>
*/
protected void destroyInternal() {
internal = null;
}
/**
* <p>Return the module configuration object for the currently selected
* module.</p>
*
* @param request The servlet request we are processing
* @return The module configuration object for the currently selected
* module.
* @since Struts 1.1
*/
protected ModuleConfig getModuleConfig(HttpServletRequest request) {
ModuleConfig config =
(ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
if (config == null) {
config =
(ModuleConfig) getServletContext().getAttribute(Globals.MODULE_KEY);
}
return (config);
}
/**
* <p>Look up and return the {@link RequestProcessor} responsible for the
* specified module, creating a new one if necessary.</p>
*
* @param config The module configuration for which to acquire and return
* a RequestProcessor.
* @return The {@link RequestProcessor} responsible for the specified
* module,
* @throws ServletException If we cannot instantiate a RequestProcessor
* instance a {@link UnavailableException} is
* thrown, meaning your application is not loaded
* and will not be available.
* @since Struts 1.1
*/
protected synchronized RequestProcessor getRequestProcessor(
ModuleConfig config) throws ServletException {
RequestProcessor processor = this.getProcessorForModule(config);
if (processor == null) {
try {
processor =
(RequestProcessor) RequestUtils.applicationInstance(config.getControllerConfig()
.getProcessorClass());
} catch (Exception e) {
throw new UnavailableException(
"Cannot initialize RequestProcessor of class "
+ config.getControllerConfig().getProcessorClass() + ": "
+ e);
}
processor.init(this, config);
String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
getServletContext().setAttribute(key, processor);
}
return (processor);
}
/**
* <p>Returns the RequestProcessor for the given module or null if one
* does not exist. This method will not create a RequestProcessor.</p>
*
* @param config The ModuleConfig.
* @return The <code>RequestProcessor</code> for the given module, or
* <code>null</code> if one does not exist.
*/
private RequestProcessor getProcessorForModule(ModuleConfig config) {
String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
return (RequestProcessor) getServletContext().getAttribute(key);
}
/**
* <p>Initialize the factory used to create the module configuration.</p>
*
* @since Struts 1.2
*/
protected void initModuleConfigFactory() {
String configFactory =
getServletConfig().getInitParameter("configFactory");
if (configFactory != null) {
ModuleConfigFactory.setFactoryClass(configFactory);
}
}
/**
* <p>Initialize the module configuration information for the specified
* module.</p>
*
* @param prefix Module prefix for this module
* @param paths Comma-separated list of context-relative resource path(s)
* for this modules's configuration resource(s)
* @return The new module configuration instance.
* @throws ServletException if initialization cannot be performed
* @since Struts 1.1
*/
protected ModuleConfig initModuleConfig(String prefix, String paths)
throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + prefix
+ "' configuration from '" + paths + "'");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -