如何在Ocpsoft Rewrite中将未定义数量的路径参数映射到请求参数?

时间:2020-06-02 10:17:59

标签: jsf dynamic path-parameter ocpsoft-rewrite

目前,我正在尝试以下JSF-Lib:

https://www.ocpsoft.org/rewrite/examples/

我有以下问题:

我有一个页面: /page.jsf

在我的页面中,只有多个参数。 例如。我有: -参数1 -parameter2

String parameter1 = FacesContext.getCurrentInstance().getExternalContext()
                    .getRequestParameterMap().get("parameter1");

            String parameter2 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                    .get("parameter2");

目前,我了解可以将其添加到我的UrlConfigProvider类中:

.addRule(Join.path(“ / page / {parameter1}”)。to(“ / portal / mypage.jsf”) .withInboundCorrection())

这适用于一个参数。

但是如何对多个参数执行此操作,因此URL为: / page / {parameter1} / {parameter2} ....

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

重写API并未针对此问题带来本地解决方案。


开球示例

.addRule()
.when(/* your condition */)
.perform(new HttpOperation() {
    @Override
    public void performHttp(HttpServletRewrite httpServletRewrite, EvaluationContext evaluationContext) {
        // this is the default wrapper
        HttpRewriteWrappedRequest request = ((HttpRewriteWrappedRequest) httpServletRewrite.getRequest());

        // get the uri (example: '/index/p1/p2')
        String uri = httpServletRewrite.getRequest().getRequestURI();

        // split by slash
        String[] split = uri.split("/");

        // this is example specific
        // split value 0 is empty and split value 1 is the page (e.g. 'index')
        // for every folder increment the index
        // for '/pages/index' the start index should 3
        for (int i = 2; i < split.length; i++) {
            String s = split[i];

            // the request parameter is by default an immutable map
            // but this returns a modifiable
            request.getModifiableParameters().put("prefix" + (i - 1), new String[]{s});
        }
    }
});

说明

唯一重要的部分是HttpOperation。默认情况下,ServletRequest包装在HttpRewriteWrappedRequest中。

初始化后,默认的HttpServletRequest不允许更改参数。方法getParameterMap()返回一个不变的映射。

getParameterMap()中的

HttpRewriteWrappedRequest也返回一个不变的映射。但是getModifiableMap()显然会返回可修改的地图。

其余的应该不言自明。


另请参见

Ocpsoft: How to modify parameters

Modify request parameter with servlet filter