本文介绍了h:outputLink值转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在将JSF与glassfish 4.0一起使用.以下代码片段

I am using JSF with glassfish 4.0. The following fragment of code

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    ...skipped...
    </h:head>
    ...skipped...
    <h:outputLink value="#{item.lastInstance.url}" escape="false">
        #{item.lastInstance.refName}
    </h:outputLink>

预计将被翻译为:

    <a href="https://url-unescaped>refname-unescaped</a>

但它被翻译为:

    <a href="https://url-escaped>refname-unescaped</a>

Bean的url和refName都包含UTF-8中的俄语文本,并带有空格和url中不允许的其他符号.但是,那些未转义的链接已在浏览器中经过测试,可以正常使用(Firefox 24.0).转义的序列由浏览器以某种方式解释,并且不起作用.

Bean's url and refName both contains russian text in UTF-8 with spaces and other symbols not allowed in url. But those unescaped links are tested in browsers to work (Firefox 24.0).Escaped sequences are interpreted by browser somehow and doesn't work.

我该怎么办

  1. 告诉JSF不要转义h:outputLink值
  2. 告诉浏览器该网址已被删除

感谢您的帮助.

推荐答案

如果我没有误会,您只是想告诉JSF不要转义h:outputLink值".

if I don't misunderstand you just want to "Tell JSF to not escape h:outputLink value".

使用html< a/>标记而不是< h:outputLink/>

use html <a/> tag instead of <h:outputLink/>

xhtml:

<h:outputLink value="русский.doc">русский.doc</h:outputLink>
<a href="#{jsfUrlHelper.getViewUrl('/русский.doc')}">русский.doc</a>

生成的输出:

<a href="%40%43%41%41%3A%38%39.doc">русский.doc</a> <!-- h:outputLink -->
<a href="http://localhost:8080/MyApp/русский.doc">русский.doc</a> <!-- a tag -->

JsfUrlHelper.java

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
public class JsfUrlHelper {

    public String getViewUrl(String viewId) {
        return getViewUrl(viewId, null);
    }

    public String getViewUrl(String viewId, String urlParams) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        String contextPath = ctx.getExternalContext().getRequestContextPath();

        StringBuilder url = new StringBuilder(100);

        url.append(getRootUrl(ctx));
        url.append(contextPath);
        url.append(viewId);

        if (urlParams != null && urlParams.length() > 0) {
            url.append("?");
            url.append(urlParams);
        }

        return url.toString();
    }

    public String getRootUrl(FacesContext ctx) {
        HttpServletRequest request = (HttpServletRequest) ctx.getExternalContext().getRequest();

        StringBuilder url = new StringBuilder(100);
        String scheme = request.getScheme();

        int port = request.getServerPort();

        url.append(scheme);
        url.append("://");
        url.append(request.getServerName());

        if (port > 0 && ((scheme.equalsIgnoreCase("http") && port != 80) || (scheme.equalsIgnoreCase("https") && port != 443))) {
            url.append(':');
            url.append(port);
        }

        return url.toString();
    }
}

这篇关于h:outputLink值转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 22:05