欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2 更改默认resulttype

程序员文章站 2022-08-10 21:54:29
最近碰到一个小要求,想让一些action返回的resulttype默认为freemarker,开始是想在action上配置全局的result,然后name用一个表达式,然后type="fr...
最近碰到一个小要求,想让一些action返回的resulttype默认为freemarker,开始是想在action上配置全局的result,然后name用一个表达式,然后type="freemarker",

 

但是经过实验,发现不行,因为这些配置是在struts2一启动的时候就已经实例化了,再在action中使用表达式起不了作用,最后会报异常.

 

于是去跟踪,最后在packageconfig这个类中找到了相关的代码

 

[java]  

public string getfulldefaultresulttype() {  

        if ((defaultresulttype == null) && !parents.isempty()) {  

            for (packageconfig parent : parents) {  

                string parentdefault = parent.getfulldefaultresulttype();  

  

                if (parentdefault != null) {  

                    return parentdefault;  

                }  

            }  

        }  

  

        return defaultresulttype;  

    }  

 

这个是一个递归查询,如果本包没有配置,就会查找父包,最后会找到default-package中,这个里面指定了为dispatcher.

好吧,代码是找到了,那么如何更改呢,

 

只需要在包中重新申明一下这个result-type

 

[html] 

<result-type name="freemarker"  

                class="org.apache.struts2.views.freemarker.freemarkerresult"  

                default="true" />  

并指定为default,

在解析这个包的时候就会将这个设置为默认的result-type.

 

相关代码在com.opensymphony.xwork2.config.providers.xmlconfigurationprovider类的addresulttypes方法中.

[java]  

protected void addresulttypes(packageconfig.builder packagecontext, element element) {  

        nodelist resulttypelist = element.getelementsbytagname("result-type");  

  

        for (int i = 0; i < resulttypelist.getlength(); i++) {  

            element resulttypeelement = (element) resulttypelist.item(i);  

            string name = resulttypeelement.getattribute("name");  

            string classname = resulttypeelement.getattribute("class");  

            string def = resulttypeelement.getattribute("default");  

  

            location loc = domhelper.getlocationobject(resulttypeelement);  

  

            class clazz = verifyresulttype(classname, loc);  

            if (clazz != null) {  

                string paramname = null;  

                try {  

                    paramname = (string) clazz.getfield("default_param").get(null);  

                }  

                catch (throwable t) {  

                    // if we get here, the result type doesn't have a default param defined.  

                }  

                resulttypeconfig.builder resulttype = new resulttypeconfig.builder(name, classname).defaultresultparam(paramname)  

                        .location(domhelper.getlocationobject(resulttypeelement));  

  

                map<string, string> params = xmlhelper.getparams(resulttypeelement);  

  

                if (!params.isempty()) {  

                    resulttype.addparams(params);  

                }  

                packagecontext.addresulttypeconfig(resulttype.build());  

  

                // set the default result type  

                if ("true".equals(def)) {  

                    packagecontext.defaultresulttype(name);  

                }  

            }  

        }  

    }  

 

这段代码用来解析struts2的配置文件,获得result-type元素,然后,解析相关元素,如果设置了default为true,则会将这个result-type设置为默认的.