如何在JSF EL中将引号附加到字符串 [英] How do I append a quote to a string in JSF EL

查看:79
本文介绍了如何在JSF EL中将引号附加到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSF页面上列出了合作伙伴列表。

I got a JSF page with a list of partners.

页面以两种模式打开,即默认模式和选择模式,这是使用choiceMode请求参数设置的。

The page is opened in two modes, default and choice mode, that is set with choiceMode request parameter.

当未定义 choiceMode 参数时,它只是一个列表,我可以在其中单击一行,它将导航到该页面并带有行的详细信息

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

如果 choiceMode = 1 表示该页面在<$ c中打开$ c>< iframe> ,嵌套在父窗口中

If choiceMode="1" it means that the page is opened in an <iframe>, nested in a parent window

当用户单击一行时,它不会打开该行的页面,但运行JavaScript将选定的行参数发送到父窗口

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

问题是要构建JavaScript(在选择模式下使用)。在默认模式下,它必须仅为 ,但在选择模式下,必须类似于

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?

我可以使用<$ c $构建类似 rowOnClick(5,Partner#5)(不带引号)的东西c> concat 方法,但是引号似乎是一个问题。我试过& quote; \' \ 无效

I can build things like rowOnClick(5, Partner #5) (without quotes) with concat method, but quotes appeared to be a problem. I have tried &quote; and \' and \" and nothing works

页面代码:

<?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>

复合组件:

<?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(',&quote;').concat(cc.attrs.rowName).concat('&quote;)')}" 
                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>


推荐答案

onclick="#{cc.attrs.onclick==''?'':cc.attrs.onclick.concat('(').concat(cc.attrs.rowId).concat(',&quote;').concat(cc.attrs.rowName).concat('&quote;)')}" 

此是一种非常笨拙的方法。只需将< c:if> < f:attribute> 一起使用即可动态设置属性。

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>

还要注意,您不需要将整个属性值放在单个EL表达式中。相反,您可以只在一个属性值中内联多个EL表达式。

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.

这篇关于如何在JSF EL中将引号附加到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆