You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSF结合OCPSoft Rewrite:URL解码功能失效问题

问题

我配置了一条简单的Rewrite规则:

Join.path("/user/security/{code}").to("/user/security.xhtml")

其中code是用户提供的唯一编码。当code包含空格(如'B P')时,URL变为/user/security/B+P,但Rewrite传递的查询参数是"B+P"而非"B P",直接访问/user/security.xhtml?code=B+P却能正常工作。请问如何处理Rewrite中的特殊字符?

解决方法

  • 手动解码参数
    直接在security.xhtml对应的后端代码里,对code参数做URL解码。+本来就是URL里空格的编码形式,用URLDecoder.decode(code, "UTF-8")就能把B+P转成B P,简单直接。

  • 修改Rewrite规则,提前处理编码
    给Rewrite规则加个参数处理器,让它先把路径里的编码值解码再传递:

    Join.path("/user/security/{code}")
        .to("/user/security.xhtml")
        .with("code", ctx -> URLDecoder.decode(ctx.getPathParameters().get("code"), "UTF-8"));
    

    这样Rewrite传递给页面的就是解码后的B P,不用页面再额外处理。

  • 全局配置编码策略
    如果用的是OCPsoft这类Rewrite框架,还能全局设置编码规则,让所有路径参数自动按UTF-8解码:

    ConfigurationBuilder.begin()
        .addRule(Join.path("/user/security/{code}").to("/user/security.xhtml"))
        .setEncoding("UTF-8");
    

内容的提问来源于stack exchange,提问作者grigouille

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 05:43:10