Package com.opensymphony.xwork2.config.entities

Examples of com.opensymphony.xwork2.config.entities.ResultTypeConfig


    public void setUp() {
        provider = new ClasspathConfigurationProvider(new String[]{"org.apache.struts2.config"});
        config = new DefaultConfiguration();
        PackageConfig strutsDefault = new PackageConfig("struts-default");
        strutsDefault.addResultTypeConfig(new ResultTypeConfig("dispatcher", ServletDispatcherResult.class.getName(), "location"));
        strutsDefault.setDefaultResultType("dispatcher");
        config.addPackageConfig("struts-default", strutsDefault);
        PackageConfig customPackage = new PackageConfig("custom-package");
        customPackage.setNamespace("/custom");
        config.addPackageConfig("custom-package", customPackage);
View Full Code Here


        // resultValue 有两种可能:
        // 1.一种直接是字面量(比如 dispatcher:/xxx.html)
        // 2.另一种是 json 参数(比如 dispatcher:{'location': '/xxx.html'} )

        ResultTypeConfig typeConfig = resultTypes.get(resultType);
        //如果没有默认参数
        if (Strings.isNullOrEmpty(typeConfig.getDefaultResultParam())) {
            ResultConfig.Builder resultBuilder = createResultConfigFromResultType(resultCode, typeConfig);
            // 只有返回类型, 例如是 "json"
            if (resultCode.length() == resultType.length()) {
                return resultBuilder.build();
            }
            //或者 ":" 后面有 json 参数
            if (resultCode.indexOf(":") == resultType.length()) {
                String resultParam = resultCode.substring(resultType.length() + 1);
                addParamByJSON(resultBuilder, resultParam);
                return resultBuilder.build();
            }
        }
        //如果有默认参数
        else {
            //  如果是 "type:" 这种形式
            if (resultCode.indexOf(":") == resultType.length()) {
                ResultConfig.Builder resultBuilder = createResultConfigFromResultType(resultCode, typeConfig);

                // 拿到参数部分(":" 后面的部分)
                String resultParam = resultCode.substring(resultType.length() + 1);
                String location = null; // 通常是 defaultParam
                if (isJSONObject(resultParam)) { //判断是否是 json 格式
                    /**
                     * 如果是 json 形式传递 param(并非 struts json 插件)的返回结果,
                     * 如果有 location ,也需要进行路径相对路径或绝对路径转换
                     */
                    Map<String, String> params = addParamByJSON(resultBuilder, resultParam);
                    location = params.get(typeConfig.getDefaultResultParam());
                } else {
                    location = resultParam;
                }

                // 页面路径的转换如果是相对路径, 但除过重定向
                if(!resultType.equals("redirect")){
                    location = parsePath(contentBase, routeMapping, location);
                }
                resultBuilder.addParam(typeConfig.getDefaultResultParam(), location);
                return resultBuilder.build();
            }
        }

        return null;
View Full Code Here

    assertEquals(resultTypesConfigMap.size(), 2);
    assertTrue(resultTypesConfigMap.containsKey("result1"));
    assertTrue(resultTypesConfigMap.containsKey("result2"));
    assertFalse(resultTypesConfigMap.containsKey("result3"));
   
    ResultTypeConfig result1ResultTypeConfig = (ResultTypeConfig) resultTypesConfigMap.get("result1");
    Map result1ParamsMap = result1ResultTypeConfig.getParams();
    ResultTypeConfig result2ResultTypeConfig = (ResultTypeConfig) resultTypesConfigMap.get("result2");
    Map result2ParamsMap = result2ResultTypeConfig.getParams();
   
    assertEquals(result1ResultTypeConfig.getName(), "result1");
    assertEquals(result1ResultTypeConfig.getClazz(), MockResult.class.getName());
    assertEquals(result2ResultTypeConfig.getName(), "result2");
    assertEquals(result2ResultTypeConfig.getClazz(), MockResult.class.getName());
    assertEquals(result1ParamsMap.size(), 3);
    assertEquals(result2ParamsMap.size(), 2);
    assertTrue(result1ParamsMap.containsKey("param1"));
    assertTrue(result1ParamsMap.containsKey("param2"));
    assertTrue(result1ParamsMap.containsKey("param3"));
View Full Code Here

    public void testResultTypes() throws ConfigurationException {
        final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-results.xml";
        ConfigurationProvider provider = buildConfigurationProvider(filename);

        // setup expectations
        ResultTypeConfig chainResult = new ResultTypeConfig.Builder("chain", ActionChainResult.class.getName()).build();
        ResultTypeConfig mockResult = new ResultTypeConfig.Builder("mock", MockResult.class.getName()).build();

        // execute the configuration
        provider.init(configuration);
        provider.loadPackages();
View Full Code Here

        String defaultResult = packageContext.getFullDefaultResultType();

        for (Map.Entry<String, ResultConfig> entry : results.entrySet()) {

            if (entry.getValue() == null) {
                ResultTypeConfig resultTypeConfig = packageContext.getAllResultTypeConfigs().get(defaultResult);
                entry.setValue(new ResultConfig.Builder(null, resultTypeConfig.getClassName()).build());
            }
        }
    }
View Full Code Here

    }

    public void testClassPath() throws Exception {
        ServletContext context = EasyMock.createNiceMock(ServletContext.class);

        ResultTypeConfig resultType = new ResultTypeConfig.Builder("freemarker", "org.apache.struts2.dispatcher.ServletDispatcherResult").
                defaultResultParam("location").build();
        PackageConfig packageConfig = new PackageConfig.Builder("package").
                defaultResultType("dispatcher").addResultTypeConfig(resultType).build();

        this.conventionsService = new ConventionsServiceImpl("/WEB-INF/component");
View Full Code Here

        assertEquals(1, results.get("foo").getParams().size());
        assertEquals("/WEB-INF/component/no-annotation-foo.ftl", results.get("foo").getParams().get("location"));
    }

    private PackageConfig createPackageConfigBuilder(String namespace) {
        ResultTypeConfig resultType = new ResultTypeConfig.Builder("dispatcher", "org.apache.struts2.dispatcher.ServletDispatcherResult").
                addParam("key", "value").addParam("key1", "value1").defaultResultParam("location").build();

        ResultTypeConfig redirect = new ResultTypeConfig.Builder("redirectAction",
                "org.apache.struts2.dispatcher.ServletActionRedirectResult").defaultResultParam("actionName").build();

        ResultTypeConfig ftlResultType = new ResultTypeConfig.Builder("freemarker",
                "org.apache.struts2.views.freemarker.FreemarkerResult").defaultResultParam("location").build();

        return new PackageConfig.Builder("package").
                namespace(namespace).
                defaultResultType("dispatcher").
View Full Code Here

                pkgConfig.namespace(firstParent.getNamespace());
            }
        }


        ResultTypeConfig defaultResultType = packageLoader.getDefaultResultType(pkgConfig);
        ActionConfig actionConfig = new ActionConfig.Builder(actionPackage, actionName, cls.getName())
                .addResultConfigs(new ResultMap<String,ResultConfig>(cls, actionName, defaultResultType))
                .build();
        pkgConfig.addActionConfig(actionName, actionConfig);
    }
View Full Code Here

    @SuppressWarnings(value = {"unchecked"})
    protected ResultConfig createResultConfig(Class<?> actionClass, ResultInfo info,
            PackageConfig packageConfig, Result result) {
        // Look up by the type that was determined from the annotation or by the extension in the
        // ResultInfo class
        ResultTypeConfig resultTypeConfig = packageConfig.getAllResultTypeConfigs().get(info.type);
        if (resultTypeConfig == null) {
            throw new ConfigurationException("The Result type [" + info.type + "] which is" +
                " defined in the Result annotation on the class [" + actionClass + "] or determined" +
                " by the file extension or is the default result type for the PackageConfig of the" +
                " action, could not be found as a result-type defined for the Struts/XWork package [" +
                packageConfig.getName() + "]");
        }

        // Add the default parameters for the result type config (if any)
        HashMap<String, String> params = new HashMap<String, String>();
        if (resultTypeConfig.getParams() != null) {
            params.putAll(resultTypeConfig.getParams());
        }

        // Handle the annotation
        if (result != null) {
            params.putAll(StringTools.createParameterMap(result.params()));
        }

        // Map the location to the default param for the result or a param named location
        if (info.location != null) {
            String defaultParamName = resultTypeConfig.getDefaultResultParam();
            if (!params.containsKey(defaultParamName)) {
                params.put(defaultParamName, info.location);
            }
        }

        return new ResultConfig.Builder(info.name, resultTypeConfig.getClassName()).addParams(params).build();
    }
View Full Code Here

        String determineType(String location, PackageConfig packageConfig,
                Map<String, ResultTypeConfig> resultsByExtension) {
            int indexOfDot = location.lastIndexOf(".");
            if (indexOfDot > 0) {
                String extension = location.substring(indexOfDot + 1);
                ResultTypeConfig resultTypeConfig = resultsByExtension.get(extension);
                if (resultTypeConfig != null) {
                    return resultTypeConfig.getName();
                } else
                    throw new ConfigurationException("Unable to find a result type for extension [" + extension + "] " +
                        "in location attribute [" + location + "].");
            } else {
                return packageConfig.getFullDefaultResultType();
View Full Code Here

TOP

Related Classes of com.opensymphony.xwork2.config.entities.ResultTypeConfig

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.