config包之ActionConfig淺析
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());
}
}
如有遺漏請大家補上,謝謝