I'm new in JSF and I have some strange problems in displaying conditional parts in form.
我是JSF的新手,在表单中显示条件部分有一些奇怪的问题。
My Facelet:
我的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 exampel (1 or 2) but nothing else will display on the form??? What is wrong with my code???
当我选择一个项目时,该值将显示为exampel(1或2),但没有其他东西会显示在表单上???我的代码有什么问题?
2 个解决方案
#1
5
Use the rendered
attribute (<h:outputText rendered="#{index.animal eq 1}" />
) for conditional displaying of components. Using JSTL is tricky with JSF.
使用渲染属性(
#2
3
Unless you really understand the JSF view life cycle, do not use c:foreach and c:if, or any alternatives. JSF is a component framework and so facelets are not really a templating language. If you try to use it like one, you will come away frustrated (possibly blaming the innocent framework).
除非您真正理解JSF视图的生命周期,否则不要使用c:foreach和c:如果,或者任何其他选择。JSF是一个组件框架,所以facelets不是真正的模板语言。如果你试着用它,你会感到很沮丧(可能会责怪无辜的框架)。
JSF view is created (simplifying a bit) once per interaction with user, no matter how many requests are there. Once it is created, you cannot add or remove components (again, simplifying), you can only change their state (for example rendered / not rendered). Since the whole point of JSF is to encapsulate the view as a static entity, not as a program that generates HTML from time to time, even if you could use some kind of if or choose, you would not know nor could control when it is executed (after reading request params? before reading request params? before or after validation? before or after conversion?).
不管有多少请求,只要与用户进行交互,就会创建JSF视图(简化一些)。一旦它被创建,您就不能添加或删除组件(再次,简化),您只能更改它们的状态(例如呈现/未呈现)。由于JSF的重点是封装视图作为一个静态的实体,而不是作为一个程序,生成HTML不时,甚至如果您可以使用某种选择,你不知道也不能控制时执行(阅后请求参数?之前读请求参数吗?验证之前或之后?之前或之后转换?)
I would write your code as:
我把你的代码写成:
@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();
}
}
And then:
然后:
<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}
#1
5
Use the rendered
attribute (<h:outputText rendered="#{index.animal eq 1}" />
) for conditional displaying of components. Using JSTL is tricky with JSF.
使用渲染属性(
#2
3
Unless you really understand the JSF view life cycle, do not use c:foreach and c:if, or any alternatives. JSF is a component framework and so facelets are not really a templating language. If you try to use it like one, you will come away frustrated (possibly blaming the innocent framework).
除非您真正理解JSF视图的生命周期,否则不要使用c:foreach和c:如果,或者任何其他选择。JSF是一个组件框架,所以facelets不是真正的模板语言。如果你试着用它,你会感到很沮丧(可能会责怪无辜的框架)。
JSF view is created (simplifying a bit) once per interaction with user, no matter how many requests are there. Once it is created, you cannot add or remove components (again, simplifying), you can only change their state (for example rendered / not rendered). Since the whole point of JSF is to encapsulate the view as a static entity, not as a program that generates HTML from time to time, even if you could use some kind of if or choose, you would not know nor could control when it is executed (after reading request params? before reading request params? before or after validation? before or after conversion?).
不管有多少请求,只要与用户进行交互,就会创建JSF视图(简化一些)。一旦它被创建,您就不能添加或删除组件(再次,简化),您只能更改它们的状态(例如呈现/未呈现)。由于JSF的重点是封装视图作为一个静态的实体,而不是作为一个程序,生成HTML不时,甚至如果您可以使用某种选择,你不知道也不能控制时执行(阅后请求参数?之前读请求参数吗?验证之前或之后?之前或之后转换?)
I would write your code as:
我把你的代码写成:
@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();
}
}
And then:
然后:
<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}