转:关于beetl集成struts2 +convention插件没法识别beetl模板的处理方案

转自 jplus 文章 html

今天第一次接触这个模板引擎,感受很是不错,平时都是使用spring mvc开发,目前教带学生作一个小项目,使用struts+guice+mybatis,因而想试试这个模板引擎开开实际使用状况。
由于目前项目彻底采用零配置方案,就出现了一个问题
根据Beetl使用手册的说明:
须要在struts2配置文件里添加result-types作以下配置

<package name="default" namespace="/" extends="struts-default"> .......
<result-types>
<result-type name="beetl" class="org.beetl.ext.struts2.Struts2BeetlActionResult" default="true" />
</result-types>
<action name="HelloWorld" class="example.HelloWorld"> <result>/hello.html</result>
</action> ........ </package>

在常规的基于struts配置文件的开发过程当中是彻底没有问题的,可是若是一旦与 convention集成将没法彻底享受beetl的功能,除非使用注解完成复杂配置。
比较方便的是struts2是开源的,根据对源代码的大体分析主要问题有两点

1.添加convention默认结果视图类型
java

<constant name="struts.convention.default.parent.package"
    value="ssia" />
<constant name="struts.convention.relative.result.types" value="dispatcher,velocity,freemarker,beetl"/>
<package name="ssia" namespace="/" extends="convention-default">
    <result-types>
        <result-type name="beetl"
            class="org.beetl.ext.struts2.Struts2BeetlActionResult" default="true" />
    </result-types>
</package>




2.修改convention视图文件扫描扩展名,将咱们本身的btl文件也做为convention的视图
<bean type="org.apache.struts2.convention.ConventionsService" name="beetlService" class="com.ssia.web.struts.plugin.convention.beetl.ConventionsBeetlService"/>
<constant name="struts.convention.conventionsService" value="beetlService"/>
public class ConventionsBeetlService extends ConventionsServiceImpl {

@Inject
public ConventionsBeetlService(
        @Inject("struts.convention.result.path") String resultPath) {
    super(resultPath);
}
 public Map<String, ResultTypeConfig> getResultTypesByExtension(PackageConfig packageConfig) {
        Map<String, ResultTypeConfig> results = packageConfig.getAllResultTypeConfigs();

        Map<String, ResultTypeConfig> resultsByExtension = super.getResultTypesByExtension(packageConfig);
        resultsByExtension.put("btl", results.get("beetl"));
        return resultsByExtension;
    }
}




通过上述的修改与配置struts2-convention插件与beetl模板引擎将能够融合工做了,spring mvc则不会出现此类状况
相关文章
相关标签/搜索