im_3772071
im_3772071

Reputation: 11

h:outputLink value escaped

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>

is expected to be translated as:

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

but it is translated as:

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

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.

How can I either:

  1. Tell JSF to not escape h:outputLink value
  2. Tell browser that URL(s) is escaped

Thanks for any help.

Upvotes: 0

Views: 2052

Answers (1)

bhdrk
bhdrk

Reputation: 3495

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

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

xhtml:

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

generated output:

<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();
    }
}

Upvotes: 1

Related Questions