<< 我收藏的链接(24) | 首页 | Ubuntu安装memcached的“C compiler cannot create executables”问题 >>

在AppModule里配置Tapestry5支持UTF-8国际化和Servlet开发

Tapestry5必须有一个"module builder class",很典型地,经常是叫"AppModule",AppModule经常用来定义一些新服务、覆盖原始服务、或者为服务更改配置。常用的是让Tapestry5支持UTF-8的request Encoding、忽略一些路径过滤来支持Servlet和其他Servlet Filter的开发等。下面的代码可以加在AppModule中:


    //设置HTTPServletRequest CharacterEncodingUTF-8,支持中文以及国际化

    public void contributeRequestHandler( 

            OrderedConfiguration configuration, 

            @InjectService("EncodingFilter") 

            RequestFilter encodingFilter) { 

        configuration.add("EncodingFilter", encodingFilter); 

    } 

 

    public RequestFilter buildEncodingFilter(@InjectService("RequestGlobals") 

    final RequestGlobals requestGlobals) { 

        return new RequestFilter() { 

            public boolean service(Request request, Response response, 

                    RequestHandler handler) throws IOException { 

 

                requestGlobals.getHTTPServletRequest().setCharacterEncoding( 

                        "UTF-8"); 

                return handler.service(request, response); 

            } 

        }; 

    } 

   

    //配置忽略某些路径不让Tapestry5处理,支持Servlet和自定义Filter

    public static void contributeIgnoredPathsFilter(Configuration<String> configuration)

    {

    configuration.add("/feed.*");

    configuration.add(".*html");

    }   

 


Tapestry5通过在web.xml下定义tapestry.app-package指向的Java包里寻找AppModule,也就是<filter-name>加上"Module"字符。

下面是官方文档里Tapsetry IoC 配置

Tapestry IoC Configuration

Most other configuration occurs inside your application's module builder class. The application module builder will often define new services, provide overrides of services, or make contributions to service configurations.

Tapestry looks for a module builder class in the services package (under the root package). It capitalizes the <filter-name> and appends "Module". In the previous example, the module builder class would be org.example.myapp.services.AppModule.

If such a class exists, it is added to the IoC Registry. It is not an error for your application to not have a module, though any non-trivial application will have a module.

标签 : , ,



发表评论 发送引用通报