Tapestry5.1.0.5升级到Tapestry5.2.4初体验 -hxzon

Tapestry5.1.0.5升级到Tapestry5.2.4初体验 -hxzon

1,URLRewriterRule废弃,改用PageRenderLinkTransformer,
而PageRenderLinkTransformer返回值为PageRenderRequestParameters,
在处理css,js,图片文件时,路径改前缀为/assets/appVersionNum/ctx/时,
出现找不到/assets/页面的错误。最后只能通过构造一个伪页面解决。

路径重写代码:

    public PageRenderRequestParameters decodePageRenderRequest(Request request) {
        String path = request.getPath();
        // delete whitespace
        path = StringUtils.deleteWhitespace(path);
        logger.trace("path incoming:" + path);
        if (path.toLowerCase().contains("web-inf")) {
            logger.debug("path contains web-info,rewrite to root");
            return new PageRenderRequestParameters(Index.class.getSimpleName(), new EmptyEventContext(), false);
        }
        if (path.contains("/css/") && !path.contains("/assets/")) {
            path = path.substring(path.indexOf("/css"));
            logger.debug("path contains css ,rewrite to assets path:" + path);
            return new PageRenderRequestParameters(pageName, new OneStringEventContext(path), false);
        }
        return null;
    }

AssetDispatcher是在PageRenderLinkTransformer之前运行。

只处理静态文件的伪页面:

public class StaticSourceFile {
    @Inject
    private Logger logger;

    @OnEvent(value = EventConstants.ACTIVATE)
    protected Object response(String path) {
        InputStream targetFile = null;
        try {
            targetFile = FileUtils.openInputStream(new File(WebUtil.contextPath + "/" + path));
            return new FileStreamResponse("", targetFile);
        } catch (Exception e) {
            logger.error("文件不存在:{}", e.getMessage());
        }
        return null;
    }
}

2,自己写的TextFieldEx类在升级时出错,

    @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.TRANSLATE)
    private FieldTranslator<Object> translate;

    @Parameter(defaultPrefix = BindingConstants.VALIDATE)
    private FieldValidator<Object> validate;

等域为空。

加入下述代码解决:

    //hxzon:fix bug when update to tapestry 5.2,but why?
    final Binding defaultTranslate() {
        return defaultProvider.defaultTranslatorBinding("value", resources);
    }

    final Binding defaultValidate() {
        return defaultProvider.defaultValidatorBinding("value", resources);
    }

3,AppModule下述代码出错,出错原因为自己的组件库包名与tapestry5.2.4的核心库包名共同字段少于2。

public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration) {
  configuration.add(new LibraryMapping("core", "org.hxzon.tapestry5"));
}

解决的方法一是改包名为org.apache.tapestry5.hxzon,但我的代码并非apache tapestry5的一部分。

解决的方法二是不用core,这样就导致以前的全部模板文件中,例如t:textfieldEx全部要改为t:h.textfieldEx。

这是此次升级最不舒服的地方。还是希望有方法将自己的库加入core中,不必用h.这样的前缀。

另外org.hxzon.tapestry5下的page无法识别。

PS:阅读faq时发现写成configuration.add(new LibraryMapping("", "org.hxzon.tapestry5"));

可以解决问题。但代码如下:

public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration) {
configuration.add(new LibraryMapping("h", "org.hxzon.tapestry5"));
configuration.add(new LibraryMapping("", "org.hxzon.tapestry5"));
}

前一次添加h前缀的库,是因为不这么做,它的组件所用的js文件等,路径会被写成

/material5/assets/3//components/SplitPageBar.js

导致js文件找不到。加入后路径为/material5/assets/3/h/components/SplitPageBar.js。

不知是否有更好的方法。
How do I store my page classes in a different package?

Tapestry is very rigid here; you can't. Page classes must go in root-package.pages, component classes in root-package.components, etc.

You are allowed to create sub-packages, to help organize your code better and more logically. For example, you might have _root-package.pages.account.ViewAccount, which would have the page name "account/viewaccount" 1

In addition, it is possible to define additional root packages for the application:

public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration) {
       configuration.add(new LibraryMapping("", "com.example.app.tasks"));
       configuration.add(new LibraryMapping("", "com.example.app.chat"));
}

LibraryMappings are used to resolve a library prefix to one or more package names. The empty string represents the application itself; the above example adds two additional root packages; you might see additional pages under com.example.app.tasks.pages, for example.

Tapestry doesn't check for name collisions, and the order the packages are searched for pages and components is not defined. In general, if you can get by with a single root package for your application, that is better.




你可能感兴趣的:(apache,Web,css,tapestry,Go)