问题
I got a JSF page with a list of partners.
The page is opened in two modes, default and choice mode, that is set with choiceMode request parameter.
When choiceMode
parameter is undefined it's just a list where I can click a row and it will navigate to the page with the row's details
If choiceMode="1"
it means that the page is opened in an <iframe>
, nested in a parent window
When the user clicks a row, it won't open the row's page, but run a JavaScript sending the chosen row parameters to the parent window
The problem is to build up the JavaScript, used when it's the choice mode. In default mode, it must be just ""
, but in the choice mode it must be something like
rowOnClick(5, "Partner #5")
How do I put the double quotes in it?
I can build things like rowOnClick(5, Partner #5)
(without quotes) with concat
method, but quotes appeared to be a problem. I have tried "e;
and \'
and \"
and nothing works
The page's code:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ez="http://java.sun.com/jsf/composite/ezcomp"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Partners</title>
<script>
function cellOnClick(id, name) {
parent.document.getElementById("form:parentId").value = id;
parent.document.getElementById("form:parentName").value = name;
}
</script>
</h:head>
<f:metadata>
<f:viewParam name="choiceMode" value="#{partnersManagedBean.choiceMode}"/>
</f:metadata>
<h:body>
<c:choose>
<c:when test="#{partnersManagedBean.choiceMode == '1'}">
<c:set var="onclick" value="cellOnClick"/>
</c:when>
<c:otherwise>
<c:set var="outcome" value="partner"/>
</c:otherwise>
</c:choose>
<h:dataTable value="#{partnersManagedBean.partnersList}" var="item">
<h:column>
<f:facet name="header">
Name
</f:facet>
<ez:cell id="name" value="#{item.value.name}" rowId="#{item.value.id}" rowName="#{item.value.name}" outcome="#{outcome}" onclick="#{onclick}"/>
</h:column>
</h:dataTable>
</h:body>
</html>
Composite component:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<cc:interface>
<cc:attribute name="value" required="true"/>
<cc:attribute name="rowId" required="true"/>
<cc:attribute name="rowName" required="true"/>
<cc:attribute name="outcome" default=""/>
<cc:attribute name="onclick" default=""/>
</cc:interface>
<cc:implementation>
<h:link id="nameLink"
outcome="#{cc.attrs.outcome}"
onclick="#{cc.attrs.onclick==''?'':cc.attrs.onclick.concat('(').concat(cc.attrs.rowId).concat(',"e;').concat(cc.attrs.rowName).concat('"e;)')}"
class="cell-link"
style="cursor: pointer">
<f:param name="id" value="#{cc.attrs.rowId}"/>
<div class="cell-div">
#{cc.attrs.value}
</div>
</h:link>
</cc:implementation>
</html>
回答1:
onclick="#{cc.attrs.onclick==''?'':cc.attrs.onclick.concat('(').concat(cc.attrs.rowId).concat(',"e;').concat(cc.attrs.rowName).concat('"e;)')}"
This is a terribly clumsy approach. Just use <c:if>
with <f:attribute>
to dynamically set an attribute.
<h:link ...>
<c:if test="#{not empty cc.attrs.onclick}">
<f:attribute name="onclick" value="#{cc.attrs.onclick}(#{cc.attrs.rowId},'#{cc.attrs.rowName}')" />
</c:if>
...
</h:link>
Also note that you don't need to put the entire attribute value in a single EL expression. Instead, you can just inline multiple EL expressions in a single attribute value.
回答2:
Use "
, i.e. without the e
for "
, and '
for '
.
来源:https://stackoverflow.com/questions/17008504/how-do-i-append-a-quote-to-a-string-in-jsf-el