/*
* $Id: RequestUtils.java 76098 2004-11-17 07:07:32Z mrdon $
*
* Copyright 1999-2004 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.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionServletWrapper;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.DynaActionFormClass;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.upload.MultipartRequestWrapper;
/**
* <p>General purpose utility methods related to processing a servlet request
* in the Struts controller framework.</p>
*
* @version $Rev: 76098 $ $Date: 2004-11-17 07:07:32 +0000 (Wed, 17 Nov 2004) $
*/
public class RequestUtils {
// ------------------------------------------------------- Static Variables
/**
* <p>Commons Logging instance.</p>
*/
protected static Log log = LogFactory.getLog(RequestUtils.class);
// --------------------------------------------------------- Public Methods
/**
* <p>Create and return an absolute URL for the specified context-relative
* path, based on the server and context information in the specified
* request.</p>
*
* @param request The servlet request we are processing
* @param path The context-relative path (must start with '/')
*
* @return absolute URL based on context-relative path
*
* @exception MalformedURLException if we cannot create an absolute URL
*/
public static URL absoluteURL(HttpServletRequest request, String path)
throws MalformedURLException {
return (new URL(serverURL(request), request.getContextPath() + path));
}
/**
* <p>Return the <code>Class</code> object for the specified fully qualified
* class name, from this web application's class loader.</p>
*
* @param className Fully qualified class name to be loaded
* @return Class object
*
* @exception ClassNotFoundException if the class cannot be found
*/
public static Class applicationClass(String className) throws ClassNotFoundException {
// Look up the class loader to be used
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = RequestUtils.class.getClassLoader();
}
// Attempt to load the specified class
return (classLoader.loadClass(className));
}
/**
* <p>Return a new instance of the specified fully qualified class name,
* after loading the class from this web application's class loader.
* The specified class <strong>MUST</strong> have a public zero-arguments
* constructor.</p>
*
* @param className Fully qualified class name to use
*
* @return new instance of class
* @exception ClassNotFoundException if the class cannot be found
* @exception IllegalAccessException if the class or its constructor
* is not accessible
* @exception InstantiationException if this class represents an
* abstract class, an interface, an array class, a primitive type,
* or void
* @exception InstantiationException if this class has no
* zero-arguments constructor
*/
public static Object applicationInstance(String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
return (applicationClass(className).newInstance());
}
/**
* <p>Create (if necessary) and return an <code>ActionForm</code> instance appropriate
* for this request. If no <code>ActionForm</code> instance is required, return
* <code>null</code>.</p>
*
* @param request The servlet request we are processing
* @param mapping The action mapping for this request
* @param moduleConfig The configuration for this module
* @param servlet The action servlet
*
* @return ActionForm instance associated with this request
*/
public static ActionForm createActionForm(
HttpServletRequest request,
ActionMapping mapping,
ModuleConfig moduleConfig,
ActionServlet servlet) {
// Is there a form bean associated with this mapping?
String attribute = mapping.getAttribute();
if (attribute == null) {
return (null);
}
// Look up the form bean configuration information to use
String name = mapping.getName();
FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
if (config == null) {
log.warn("No FormBeanConfig found under '" + name + "'");
return (null);
}
ActionForm instance = lookupActionForm(request, attribute, mapping.getScope());
// Can we recycle the existing form bean instance (if there is one)?
try {
if (instance != null && canReuseActionForm(instance, config)) {
return (instance);
}
} catch(ClassNotFoundException e) {
log.error(servlet.getInternal().getMessage("formBean", config.getType()), e);
return (null);
}
return createActionForm(config, servlet);
}
private static ActionForm lookupActionForm(HttpServletRequest request, String attribute, String scope)
{
// Look up any existing form bean instance
if (log.isDebugEnabled()) {
log.debug(
" Looking for ActionForm bean instance in scope '"
+ scope
+ "' under attribute key '"
+ attribute
+ "'");
}
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(scope)) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
return (instance);
}
/**
* <p>Determine whether <code>instance</code> of <code>ActionForm</code> is
一个WEBWORK的例子
需积分: 0 55 浏览量
更新于2008-09-02
收藏 892KB ZIP 举报
WebWork是一个基于Java的开源MVC(模型-视图-控制器)框架,它在Web应用程序开发中扮演着重要角色。这个例子很可能是为了演示WebWork框架的基本使用和功能。让我们深入了解一下WebWork及其相关概念。
**WebWork简介**
WebWork是由OpenSymphony团队开发的一个轻量级Web应用框架,它在早期版本中提供了与Struts类似的功能,但后来演进为更现代化的框架,比如现在的Struts 2,两者有着密切的关系。WebWork强调代码的简洁性和可测试性,通过强大的拦截器(Interceptors)机制和强大的数据绑定功能,使开发者能够更高效地构建Web应用程序。
**MVC模式**
在WebWork中,MVC模式被用来分离业务逻辑、数据和用户界面。模型(Model)负责管理应用程序的数据和业务逻辑,视图(View)用于展示数据,而控制器(Controller)处理用户的请求并协调模型和视图之间的交互。
**WebWork核心组件**
1. **Action**: 表示一个用户操作,它处理请求并返回结果。Action类是业务逻辑的载体,可以使用注解或XML配置来定义。
2. **Interceptor**: 拦截器是WebWork的一个重要特性,它们在Action执行前和执行后进行拦截,实现如日志、事务管理、权限验证等功能。
3. **ValueStack**: 这是一个对象栈,用于存储Action上下文中的数据。它可以方便地在Action之间共享数据,并简化视图渲染。
4. **Result**: 结果是Action执行后的状态,通常用于决定跳转到哪个视图,或者执行其他操作。
5. **Annotations**: WebWork支持丰富的注解,允许开发者在代码中直接声明Action、Interceptor和Result等配置,减少XML配置文件的使用。
**配置文件**
在WebWork应用中,主要的配置文件有`struts.xml`,用于定义Action、Interceptor Stack和Result类型等。这个文件是整个应用的中枢,定义了请求如何映射到Action以及如何处理Action的结果。
**视图层**
WebWork通常与JSP、FreeMarker或Velocity等模板引擎配合使用,生成动态HTML。通过OGNL(Object-Graph Navigation Language)表达式,可以从ValueStack中轻松访问和展示数据。
**文件结构**
在提供的压缩包文件中,`org`可能代表的是组织结构,包含WebWork框架相关的类文件。这些文件可能包括Action实现类、拦截器、配置类以及其他支持类。通过查看这些源代码,我们可以了解WebWork框架如何被实际应用。
总结起来,"一个WEBWORK的例子"意味着你将有机会看到一个实际的WebWork项目是如何工作的,这将帮助你理解MVC模式、拦截器、ValueStack以及如何配置和使用WebWork框架。通过分析`org`目录下的文件,你可以深入学习WebWork的内部机制和最佳实践。