JSF contains and not contains

860 Views Asked by At

Looking for a solution to an old JSF page.

I am trying to use the contains method to render a combobox based on if another column contains the words red box.

If col4 contains the words 'red box' then print 'True', if col4 does not contain the words, then print 'False'.

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" xmlns="http://www.w3.org/1999/xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:c="http://java.sun.com/jstl/core" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richfaces.org/rich">
 <ui:define name="content">
  <rich:panel id="myForm" style="height: 420px;width: 730px;">

    <rich:dataTable id="myTable" value="#{myModel.myTable}" var="table">
    
     <rich:column>
      <f:facet name="header">
       <h:outputText value="Select" />
      </f:facet>
        <c:if test="#{fn:contains(table.col4, 'red box')}">
            <h:outputText value="True" />
        </c:if>
        <c:if test="#{not fn:contains(table.col4, 'red box')}">
            <h:outputText value="False" />
        </c:if>
     </rich:column>

     <rich:column>
      <f:facet name="header">
       <h:outputText value="Comments"/>
      </f:facet>
      <h:inputTextarea id="col4_1" value="#{table.col4}"/>
     </rich:column>

    </rich:dataTable>

  </rich:panel>
 </ui:define>
</ui:composition>
2

There are 2 best solutions below

3
Minh On

I think table.col4.contains('') is Java String's method, not JSF EL so you can't add "!" before expression

This case may be JSTL will help

tablib:

xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
<c:if test="#{not fn:contains(table.col4, 'red box')}">
  <h:selectOneMenu disabled="false" value="#{table.mark}">
    <f:selectItems value="#{comboboxModel.markList}"/>
  </h:selectOneMenu>
</c:if>
0
Quentinb On

The solution was to do this:

<rich:column>
 <f:facet name="header">
  <h:outputText value="Select" />
 </f:facet>
  <h:outputText rendered="${fn:contains(table.col4, 'red box')}" value="True" />
  <h:outputText rendered="${not fn:contains(table.col4, 'red box')}" value="False" />
</rich:column>