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



目前我正在尝试以下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/{参数1}/{参数2} ....

有什么想法吗?

重写 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 过滤器修改请求参数

最新更新