requestMap 包裝了request
params include the parameters of the request or mapping
webwork中上下文對象ContextMap的構造方法
public Map createContextMap(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping, ServletContext context) {
// request map wrapping the http request objects
Map requestMap = new RequestMap(request);
// parameters map wrapping the http paraneters.
//首先從mapping.getParams()獲取,如果為空,則從request中獲取
Map params = null;
if (mapping != null) {
params = mapping.getParams();
}
Map requestParams = new HashMap(request.getParameterMap());
if (params != null) {
params.putAll(requestParams);
} else {
params = requestParams;
}
// session map wrapping the http session
Map session = new SessionMap(request);
// application map wrapping the ServletContext
Map application = new ApplicationMap(context);
//上下文ContextMap 包括了reqest的封裝,params,session,application,request,response,Servcontext
return createContextMap(requestMap, params, session, application, request, response, context);
}
request封裝的類(lèi)
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.webwork.dispatcher;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
/**
* A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
*
* @author Patrick Lightbody
* @author Bill Lynch (docs)
*/
public class RequestMap extends AbstractMap implements Serializable {
Set entries;
private HttpServletRequest request;
/**
* Saves the request to use as the backing for getting and setting values
*
* @param request the http servlet request.
*/
public RequestMap(final HttpServletRequest request) {
this.request = request;
}
/**
* Removes all attributes from the request as well as clears entries in this map.
*/
public void clear() {
Enumeration keys = request.getAttributeNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
request.removeAttribute(key);
}
}
/**
* Returns a Set of attributes from the http request.
*
* @return a Set of attributes from the http request.
*/
public Set entrySet() {
if (entries == null) {
entries = new HashSet();
Enumeration enumeration = request.getAttributeNames();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = request.getAttribute(key);
entries.add(new Entry() {
public boolean equals(Object obj) {
Entry entry = (Entry) obj;
return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
}
public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
public Object setValue(Object obj) {
request.setAttribute(key.toString(), obj);
return value;
}
});
}
}
return entries;
}
/**
* Returns the request attribute associated with the given key or <tt>null</tt> if it doesn‘t exist.
*
* @param key the name of the request attribute.
* @return the request attribute or <tt>null</tt> if it doesn‘t exist.
*/
public Object get(Object key) {
return request.getAttribute(key.toString());
}
/**
* Saves an attribute in the request.
*
* @param key the name of the request attribute.
* @param value the value to set.
* @return the object that was just set.
*/
public Object put(Object key, Object value) {
entries = null;
request.setAttribute(key.toString(), value);
return get(key);
}
/**
* Removes the specified request attribute.
*
* @param key the name of the attribute to remove.
* @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
*/
public Object remove(Object key) {
entries = null;
Object value = get(key);
request.removeAttribute(key.toString());
return value;
}
}
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
Map extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new action
OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
if (stack != null) {
//create a copy of the exist stack
extraContext.put(ActionContext.VALUE_STACK, new OgnlValueStack(stack));
}
try {
String namespace = mapping.getNamespace();
String name = mapping.getName();
String method = mapping.getMethod();
String id = request.getParameter(XWorkContinuationConfig.CONTINUE_PARAM);
if (id != null) {
// remove the continue key from the params - we don‘t want to bother setting
// on the value stack since we know it won‘t work. Besides, this breaks devMode!
Map params = (Map) extraContext.get(ActionContext.PARAMETERS);
params.remove(XWorkContinuationConfig.CONTINUE_PARAM);
// and now put the key in the context to be picked up later by XWork
extraContext.put(XWorkContinuationConfig.CONTINUE_KEY, id);
}
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);
// 執行方法可以不是execute(),在這里設置
proxy.setMethod(method);
// 將值棧放入到reqeust中保存
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (stack != null) {
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
LOG.error("Could not find action", e);
sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
} catch (Exception e) {
String msg = "Could not execute action";
LOG.error(msg, e);
throw new ServletException(msg, e);
}
}