ActionConfig浅析
struts-config.xml文件中每一个<action>元素对应着一个org.apache.struts.action.ActionMapping。ActionMapping继承自org.apache.struts.config.ActionConfig
<action-mappings>元素包含零个或多个<action>元素,<action>元素描述了从特定的请求路径到响应的Action类的影射,<action>元素中包含多个<exception>和<forward>子元素,它们分别配置局部的异常处理及请求转发仅当前的Action所访问。
<action>元素中的className是和<action>元素对应的配置元素。默认为org.apache.struts.action.ActionMapping。
<action>元素中的attribute是设置和Action关联的ActionForm Bean在request或session范围内的属性Key。如:Form Bean存在于request范围内,并且此项设为"myBean",那么request.getAttribute("myBean")就可以返回该Bean的实例。此项为可选,它在ActionConfig中对应的属性为String attribute。
<action>元素中的forward是指定转发的URL路径。它在ActionConfig中对应的属性为HashMap forward。
<action>元素中的include是指定包含的URL路径。它在ActionConfig中对应的属性为String include。
<action>元素中的input是指定包含输入表单的URL路径。当表单验证失败时,将把请求转发到该URL。它在ActionConfig中对应的属性为String input。
<action>元素中的name是指定和该Action关联的ActionForm Bean的名字。该名字必须在<form-bean>元素中定义过。此项是可选的。它在ActionConfig中对应的属性为String name。
<action>元素中的path是指定访问Action的路径,它以"/"开头,没有扩展名。它在ActionConfig中对应的属性为String path。
<action>元素中的parameter是指定Action的配置参数。在Action类的execute()方法中,可以调用ActionMapping对象的getParocessor()方法来读取该配置参数。它在ActionConfig中对应的属性为String parameter。
<action>元素中的roles是指定允许调用该Action的安全角色。多个角色之间以逗号隔开。在处理请求时,RequestProcessor会根据该配置项来决定用户是否有调用Action的权限。它在ActionConfig中对应的属性为String roles。
<action>元素中的scope是指定ActionForm Bean的存在范围,可选值为request和session。默认为session。它在ActionConfig中对应的属性为String scope。
<action>元素中的type是指定Action类的完整类名。它在ActionConfig中对应的属性为String type。
<action>元素中的unknown项为true,表示可以处理用户发出的所有无效的Action URL。默认为false。它在ActionConfig中对应的属性为boolean unknown。
<action>元素中的validate是指定是否要先调用ActionForm Bean的validate()方法。默认为true。它在ActionConfig中对应的属性为boolean validate。
ActionConfig类中的HashMap exceptions属性对应的是<action></action>包含的零个到多个<exception>元素的信息
ActionConfig类中方法解析
public class ActionConfig implements Serializable {
protected boolean configured = false;
protected HashMap exceptions = new HashMap();
protected HashMap forwards = new HashMap();
protected ModuleConfig moduleConfig = null;
public ModuleConfig getModuleConfig() {
return (this.moduleConfig);
}
public void setModuleConfig(ModuleConfig moduleConfig) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.moduleConfig = moduleConfig;
}
protected String attribute = null;
public String getAttribute() {
if (this.attribute == null) {
return (this.name);
} else {
return (this.attribute);
}
}
public void setAttribute(String attribute) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.attribute = attribute;
}
protected String forward = null;
public String getForward() {
return (this.forward);
}
public void setForward(String forward) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.forward = forward;
}
protected String include = null;
public String getInclude() {
return (this.include);
}
public void setInclude(String include) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.include = include;
}
protected String input = null;
public String getInput() {
return (this.input);
}
public void setInput(String input) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.input = input;
}
protected String multipartClass = null;
public String getMultipartClass() {
return (this.multipartClass);
}
public void setMultipartClass(String multipartClass) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.multipartClass = multipartClass;
}
protected String name = null;
public String getName() {
return (this.name);
}
public void setName(String name) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.name = name;
}
protected String parameter = null;
public String getParameter() {
return (this.parameter);
}
public void setParameter(String parameter) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.parameter = parameter;
}
protected String path = null;
public String getPath() {
return (this.path);
}
public void setPath(String path) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.path = path;
}
//与用户请求的参数相匹配的前缀
protected String prefix = null;
public String getPrefix() {
return (this.prefix);
}
public void setPrefix(String prefix) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.prefix = prefix;
}
protected String roles = null;
public String getRoles() {
return (this.roles);
}
public void setRoles(String roles) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.roles = roles;
if (roles == null) {
roleNames = new String[0];
return;
}
ArrayList list = new ArrayList();
while (true) {
int comma = roles.indexOf(',');
if (comma < 0)
break;
list.add(roles.substring(0, comma).trim());
roles = roles.substring(comma + 1);
}
roles = roles.trim();
if (roles.length() > 0)
list.add(roles);
roleNames = (String[]) list.toArray(new String[list.size()]);
}
protected String[] roleNames = new String[0];
public String[] getRoleNames() {
return (this.roleNames);
}
protected String scope = "session";
public String getScope() {
return (this.scope);
}
public void setScope(String scope) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.scope = scope;
}
与用户请求的参数相匹配的后缀
protected String suffix = null;
public String getSuffix() {
return (this.suffix);
}
public void setSuffix(String suffix) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.suffix = suffix;
}
protected String type = null;
public String getType() {
return (this.type);
}
public void setType(String type) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.type = type;
}
protected boolean unknown = false;
public boolean getUnknown() {
return (this.unknown);
}
public void setUnknown(boolean unknown) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.unknown = unknown;
}
protected boolean validate = true;
public boolean getValidate() {
return (this.validate);
}
public void setValidate(boolean validate) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.validate = validate;
}
//将<action></action>包含的<exception>元素的信息保存到HahsMap exceptions
public void addExceptionConfig(ExceptionConfig config) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
exceptions.put(config.getType(), config);
}
//将<action>元素下的forward信息全部保存到HashMap forwards中
public void addForwardConfig(ForwardConfig config) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
forwards.put(config.getName(), config);
}
//根据传入的key从excepions中检索一个ExceptionConfig对象
public ExceptionConfig findExceptionConfig(String type) {
return ((ExceptionConfig) exceptions.get(type));
}
//把exceptions中所有的Exceptionconfig对象全部取出放入一个ExceptionConfig数组中,并返回该数组
public ExceptionConfig[] findExceptionConfigs() {
ExceptionConfig results[] = new ExceptionConfig[exceptions.size()];
return ((ExceptionConfig[]) exceptions.values().toArray(results));//
}
//检索ExceptionConfig对象
public ExceptionConfig findException(Class type) {
ExceptionConfig config = null;
while (true) {
//在本地检索ExceptionConfig对象
String name = type.getName();//得到该对象对应实体类的名称
config = findExceptionConfig(name);
if (config != null) {
return (config);
}
//在全局中检索ExceptionConfig对象
config = getModuleConfig().findExceptionConfig(name);
if (config != null) {
return (config);
}
type = type.getSuperclass();//得到该对象对应实体类的父类的名称,再循环检索
if (type == null) {
break;
}
}
return (null); // No handler has been configured
}
//根据<forward>元素中的name属性检索Forwardconfig对象
public ForwardConfig findForwardConfig(String name) {
return ((ForwardConfig) forwards.get(name));
}
//将HashMap forwards中的所有ForwardConfig对象全部保存到一个ForwardConfig数组中,并返回该ForwardConfig数组
public ForwardConfig[] findForwardConfigs() {
ForwardConfig results[] = new ForwardConfig[forwards.size()];
return ((ForwardConfig[]) forwards.values().toArray(results));
}
public void freeze() {
configured = true;
ExceptionConfig[] econfigs = findExceptionConfigs();
for (int i = 0; i < econfigs.length; i++) {
econfigs[i].freeze();
}
ForwardConfig[] fconfigs = findForwardConfigs();
for (int i = 0; i < fconfigs.length; i++) {
fconfigs[i].freeze();
}
}
//从exceptions中删除一个异常类的名字
public void removeExceptionConfig(ExceptionConfig config) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
exceptions.remove(config.getType());//config.getType()异常类的名字
}
/**
* Remove the specified forward configuration instance.
*
* @param config ForwardConfig instance to be removed
*
* @exception IllegalStateException if this module configuration
* has been frozen
*/
public void removeForwardConfig(ForwardConfig config) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
forwards.remove(config.getName());
}
public String toString() {
StringBuffer sb = new StringBuffer("ActionConfig[");
sb.append("path=");
sb.append(path);
if (attribute != null) {
sb.append(",attribute=");
sb.append(attribute);
}
if (forward != null) {
sb.append(",forward=");
sb.append(forward);
}
if (include != null) {
sb.append(",include=");
sb.append(include);
}
if (input != null) {
sb.append(",input=");
sb.append(input);
}
if (multipartClass != null) {
sb.append(",multipartClass=");
sb.append(multipartClass);
}
if (name != null) {
sb.append(",name=");
sb.append(name);
}
if (parameter != null) {
sb.append(",parameter=");
sb.append(parameter);
}
if (prefix != null) {
sb.append(",prefix=");
sb.append(prefix);
}
if (roles != null) {
sb.append(",roles=");
sb.append(roles);
}
if (scope != null) {
sb.append(",scope=");
sb.append(scope);
}
if (suffix != null) {
sb.append(",suffix=");
sb.append(suffix);
}
if (type != null) {
sb.append(",type=");
sb.append(type);
}
return (sb.toString());
}
}
如有遗漏请大家补上,谢谢