这很奇怪。我的 JSF 页面中有一个标记,其中包含一个包含 + 号的参数。单击生成的超链接时,URL 会将 + 号转换为空格,因为这就是 URL 中空格的表示方式。
是否有任何方法可以对此参数进行编码以显示“%2b”(这是 urlencoded 字符串)而不是 +?我正在使用 JSF 1.2
<hx:requestLink styleClass="requestLink" id="link31"
action="#{sellingMarginBean.changeView}">
<h:outputText styleClass="outputText" id="text81"
value="#{varsummaryDataList.tier.description}"></h:outputText>
<f:param
value="#{varsummaryDataList.tier.tierCode}"
name="tierCode" id="param51"></f:param>
</hx:requestLink>
如果我在输出到屏幕之前更改 tierCode 的值以将任何“+”替换为“%2b”,则这可以工作,但这充其量只是一种黑客行为,因为这意味着在我的 Tier 域对象上创建自定义方法或循环通过summaryDataList并执行替换。
提前致谢
史蒂夫
最佳答案
根据this post作者:BalusC:
JSP/Servlet request: during request processing an average application server will by default use the ISO 8859-1 character encoding to URL-decode the request parameters. You need to force the character encoding to UTF-8 yourself. First this: "URL encoding" must not to be confused with "character encoding". URL encoding is merely a conversion of characters to their numeral representations in the %xx format, so that special characters can be passed through URL without any problems. The client will URL-encode the characters before sending them to the server. The server should URL-decode the characters using the same character encoding.
所以您的客户端和服务器可能没有使用相同的 URL(URI) 编码。最好的办法是强制服务器本身使用 UTF-8 编码。这取决于您使用的服务器。
您还可以使用 JSTL 的 fn:replace
对于您的参数,作为替代但更“hacky”的解决方案。请记住在命名空间集中定义 JSTL 标记库 (xmlns:fn="http://java.sun.com/jsp/jSTL/functions"
)。
<f:param
value="#{fn:replace(varsummaryDataList.tier.tierCode, '+', '%2b'}"
name="tierCode" id="param51" />
另请参阅:
关于java - 显示包含 + 号的 JSF requestLink 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23764617/