【问题标题】:How to map undefined amount of path parameters to request parameters in Ocpsoft Rewrite?如何将未定义数量的路径参数映射到 Ocpsoft Rewrite 中的请求参数?
【发布时间】:2020-09-20 18:56:19
【问题描述】:

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

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

我有以下问题:

我有一个页面: /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("/portal/mypage.jsf") .withInboundCorrection())

这适用于一个参数。

但是我怎样才能对多个参数执行此操作,所以 URL 是: /page/{parameter1}/{parameter2} ....

有什么想法吗?

【问题讨论】:

  • 这可能对你有帮助:ocpsoft.org/opensource/…
  • 谢谢,不过应该是URL重写规则吧?
  • 只是为了说清楚:你想用动态数量的参数重写/page/p1/p2/p3/ ...到页面/portal/mypage.jsf?p1=?&p2=?&p3=?& ...
  • 是的,正确.... 那我该如何调整规则呢? .addRule(Join.path("/page/{parameter1}").to("/portal/mypage.jsf") .withInboundCorrection())

标签: jsf dynamic path-parameter ocpsoft-rewrite


【解决方案1】:

重写 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() 方法返回一个不可变的映射。

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

其余的应该是不言自明的。


另见

Ocpsoft: How to modify parameters

Modify request parameter with servlet filter

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2017-07-06
    • 2020-11-25
    • 2011-02-14
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多