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

问题描述 投票:1回答:1

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

https:/www.ocpsoft.orgrewriteexamples

我有以下问题。

我有一个page:page.jsf

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

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

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

目前我的理解是,我可以在我的UrlConfigProvider类中添加这个。

.addRule(Join.path("page{parameter1}").to("portalmypage.jsf") .withInboundCorrection())

这对一个参数是有效的。

但我如何才能对多个参数进行操作,这样URL就变成:page{parameter1}{parameter2}......。

有什么好办法吗?

jsf dynamic path-parameter ocpsoft-rewrite
1个回答
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。如何修改参数

用servlet过滤器修改请求参数

© www.soinside.com 2019 - 2024. All rights reserved.