config包之ActionConfig浅析

本文剖析了Struts框架中ActionConfig的作用与实现细节,包括其如何映射请求路径到Action类,以及如何通过<action>元素配置属性、转发路径等。
部署运行你感兴趣的模型镜像

 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());

    }
}

如有遗漏请大家补上,谢谢

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

优化一下 当前获取是从yml文件获取 ehs: sign-workflow: CREATE: CREATE: MANAGER_APPROVE CANCEL: CANCEL MANAGER_APPROVE: APPROVE: PIE_APPROVE REJECT: CREATE PIE_APPROVE: APPROVE: QE_APPROVE REJECT: CREATE QE_APPROVE: CLOSE: CLOSE REJECT: CREATE state-role-mapping: CREATE: - type: user PIE_APPROVE: - type: role name: PIE QE_APPROVE: - type: role name: QE @Component @ConfigurationProperties(prefix = "surroundingx.ehs") public class SignWorkflowConfig { private Map<String, Map<String, String>> signWorkflow; private Map<String, List<AssigneeInfoConfig>> stateRoleMapping; public Map<String, Map<String, String>> getSignWorkflow() { return signWorkflow; } public void setSignWorkflow(Map<String, Map<String, String>> signWorkflow) { this.signWorkflow = signWorkflow; } public Map<String, List<AssigneeInfoConfig>> getStateRoleMapping() { return stateRoleMapping; } public void setStateRoleMapping(Map<String, List<AssigneeInfoConfig>> stateRoleMapping) { this.stateRoleMapping = stateRoleMapping; } } @Service public class EhsWorkFlowServiceImpl implements EhsWorkFlowService { private final SignWorkflowConfig workflowConfig; @Autowired public EhsWorkFlowServiceImpl(SignWorkflowConfig workflowConfig) { this.workflowConfig = workflowConfig; } @Override public String getNextState(String currentState, String action) { // 去除空格、转大写、下划线替换空格 String normalizedState = toUpperSnakeCase(currentState); // 转为全大写 String normalizedAction = action == null ? null : action.toUpperCase(); Map<String, Map<String, String>> workflow = workflowConfig.getSignWorkflow(); Map<String, String> transitions = workflow.get(normalizedState); if (transitions == null) { throw new JyongException(JyongExceptionCodeEnum.ERROR, "未知状态: " + normalizedState); } String nextState = transitions.get(normalizedAction); if (nextState == null) { throw new JyongException(JyongExceptionCodeEnum.ERROR, "状态 " + normalizedState + " 不支持操作: " + normalizedAction); } return nextState; } /** * 获取下一节点的审批人信息(支持多个审批人,含 type 和 name) */ public List<AssigneeInfoConfig> getNextAssignees(String nextState) { // 如果是 MANAGER_APPROVE 状态,无需校验,直接返回空列表或根据需求处理 if (SystemConstant.MANAGER_APPROVE.equals(nextState)) { return new ArrayList<>(); // 或者根据业务需求返回特定审批人 } Map<String, List<AssigneeInfoConfig>> mapping = workflowConfig.getStateRoleMapping(); List<AssigneeInfoConfig> assignees = mapping.get(nextState); if (assignees == null || assignees.isEmpty()) { throw new JyongException(JyongExceptionCodeEnum.ERROR, "未找到状态 " + nextState + " 对应的审批人"); } return assignees; } private String toUpperSnakeCase(String input) { if (input == null || input.isEmpty()) { return input; } return input.replaceAll("\\s+", "_") // 替换空格为下划线 .toUpperCase(); } } 现在改成了从下面的方法获取: List<GsonConfig> signWorkFlow = configService.getConfig(EhsFmCodeConfigEnum.SIGN_WORKFLOW.getConfigType(), true).getConfigs(); signWorkFlow = {ArrayList@11875} size = 4 0 = {GsonConfig@11910} "GsonConfig(configName=CREATE, configType=folder, configValue=, configs=[GsonConfig(configName=CREATE, configType=string, configValue=MANAGER_APPROVE, configs=null, configId=7837), GsonConfig(configName=CANCEL, configType=string, configValue=CANCEL, configs=null, configId=7837)], configId=7836)" configName = "CREATE" configType = "folder" configValue = "" configs = {ArrayList@11921} size = 2 0 = {GsonConfig@11929} "GsonConfig(configName=CREATE, configType=string, configValue=MANAGER_APPROVE, configs=null, configId=7837)" configName = "CREATE" configType = "string" configValue = "MANAGER_APPROVE" configs = null configId = {Long@11936} 7837 id = {Long@11937} 7841 disabled = false createTime = {Date@11938} "Wed Nov 19 14:56:20 GMT+08:00 2025" updateTime = {Date@11939} "Wed Nov 19 14:56:20 GMT+08:00 2025" updateUser = null createUser = null 1 = {GsonConfig@11930} "GsonConfig(configName=CANCEL, configType=string, configValue=CANCEL, configs=null, configId=7837)" configName = "CANCEL" configType = "string" configValue = "CANCEL" configs = null configId = {Long@11945} 7837 id = {Long@11946} 7842 disabled = false createTime = {Date@11947} "Wed Nov 19 14:56:27 GMT+08:00 2025" updateTime = {Date@11948} "Wed Nov 19 14:56:27 GMT+08:00 2025" updateUser = null createUser = null configId = {Long@11922} 7836 id = {Long@11923} 7837 disabled = false createTime = {Date@11924} "Wed Nov 19 14:55:42 GMT+08:00 2025" updateTime = {Date@11925} "Wed Nov 19 14:55:42 GMT+08:00 2025" updateUser = null createUser = null 1 = {GsonConfig@11911} "GsonConfig(configName=MANAGER_APPROVE, configType=folder, configValue=, configs=[GsonConfig(configName=APPROVE, configType=string, configValue=PIE_APPROVE, configs=null, configId=7838), GsonConfig(configName=REJECT, configType=string, configValue=CREATE, configs=null, configId=7838)], configId=7836)" 2 = {GsonConfig@11912} "GsonConfig(configName=PIE_APPROVE, configType=folder, configValue=, configs=[GsonConfig(configName=REJECT, configType=string, configValue=CREATE, configs=null, configId=7839), GsonConfig(configName=APPROVE, configType=string, configValue=QE_APPROVE, configs=null, configId=7839)], configId=7836)" 3 = {GsonConfig@11913} "GsonConfig(configName=QE_APPROVE, configType=folder, configValue=, configs=[GsonConfig(configName=REJECT, configType=string, configValue=CREATE, configs=null, configId=7840), GsonConfig(configName=CLOSE, configType=string, configValue=CLOSE, configs=null, configId=7840)], configId=7836)" List<GsonConfig> stateRoleMapping = configService.getConfig(EhsFmCodeConfigEnum.STATE_ROLE_MAPPING.getConfigType(), true).getConfigs(); stateRoleMapping = {ArrayList@11880} size = 3 0 = {GsonConfig@11952} "GsonConfig(configName=CREATE, configType=folder, configValue=, configs=[GsonConfig(configName=user, configType=string, configValue=user, configs=null, configId=7850)], configId=7849)" configName = "CREATE" configType = "folder" configValue = "" configs = {ArrayList@11960} size = 1 0 = {GsonConfig@11968} "GsonConfig(configName=user, configType=string, configValue=user, configs=null, configId=7850)" configName = "user" configType = "string" configValue = "user" configs = null configId = {Long@11973} 7850 id = {Long@11974} 7853 disabled = false createTime = {Date@11975} "Wed Nov 19 14:58:35 GMT+08:00 2025" updateTime = {Date@11976} "Wed Nov 19 14:59:12 GMT+08:00 2025" updateUser = null createUser = null configId = {Long@11961} 7849 id = {Long@11962} 7850 disabled = false createTime = {Date@11963} "Wed Nov 19 14:58:03 GMT+08:00 2025" updateTime = {Date@11964} "Wed Nov 19 14:58:03 GMT+08:00 2025" updateUser = null createUser = null 1 = {GsonConfig@11953} "GsonConfig(configName=PIE_APPROVE, configType=folder, configValue=, configs=[GsonConfig(configName=role, configType=string, configValue=PIE, configs=null, configId=7851)], configId=7849)" 2 = {GsonConfig@11954} "GsonConfig(configName=QE_APPROVE, configType=folder, configValue=, configs=[GsonConfig(configName=role, configType=string, configValue=QE, configs=null, configId=7852)], configId=7849)"
最新发布
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值