JSF <c: if <c: select problem

I am new to JSF and I have some weird problems displaying conditional parts in a form.

My facelet:

<h:form id="animalForm">
    <h:selectOneRadio id="animal" onchange="submit()" value="#{index.animal}">
      <f:selectItem itemLabel="Cat" itemValue="1"/>
      <f:selectItem itemLabel="Dog" itemValue="2"/>
    </h:selectOneRadio>
  </h:form>
  <h:outputText value="#{index.animal}"/>
  <c:if test="#{index.animal eq 1}">
    <h:outputText value="Cat"/>
  </c:if>
  <c:if test="#{index.animal eq 2}">
    <h:outputText value="Dog"/>
  </c:if>

And My Bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean(name = "index")
@RequestScoped
public class IndexBean {
  public static final int CAT = 1;
  public static final int DOG = 2;
  private int animal;

  public IndexBean() {

  }

  public int getAnimal() {
    return animal;
  }

  public void setAnimal(int animal) {
    this.animal = animal;
  } 

}

When I select one item, the value will be displayed for the exam (1 or 2), but nothing will be displayed on the form? What is wrong with my code ???

+3
source share
2 answers

Use the rendered( <h:outputText rendered="#{index.animal eq 1}" />) attribute to conditionally display components. Using JSTL is difficult with JSF.

+5
source

JSF, c: foreach c: if . JSF - , facelets . , (, ).

JSF ( ) , . ( , ), (, rendered/not rendered). JSF , , , HTML, - if select, , ( ? ? ?).

:

@ManagedBean(name = "index")
@RequestScoped
public class IndexBean {
  public enum Animal {
      Cat, Dog;
  }
  private Animal animal;

  public Animal getAnimal() {
    return animal;
  }

  public void setAnimal(Animal animal) {
    this.animal = animal;
  } 

  public Animal[] getAnimals(){
      return Animal.values();
  }
}

:

  <h:form id="animalForm">
    <h:selectOneRadio id="animal" onchange="submit()" value="#{index.animal}">
        <f:selectItems value="#{index.animals}"/>
    </h:selectOneRadio>
  </h:form>
  The animal is: #{index.animal}
+3

Source: https://habr.com/ru/post/1782802/


All Articles