我有一个带有selectOneMenu的表单,其中包含两个选项和一个inputText,如果用户选择第二个选项,则必须禁用该表单。我认为我应该使用ajax和inputText的更新,这是我做的,但是不起作用。这是我的代码,请帮帮我。感谢
<h:outputText value="Modalita"></h:outputText>
<p:selectOneMenu value="#{vociCostoBean.selected.modalita}" immediate="true">
<f:selectItem itemLabel="Importo" itemValue="0"/>
<f:selectItem itemLabel="Quantita" itemValue="1"/>
<p:ajax update="uc" />
</p:selectOneMenu>
<h:outputText value="Costo Unitario"></h:outputText>
<p:inputText id="uc" disabled="#{vociCostoBean.selected.modalita !='1'}" value="#{vociCostoBean.selected.CUnitario}" />
<br></br>
和这个vociCostoBean:
@ManagedBean
@SessionScoped
public class VociCostoBean {
@EJB
private CostoBeanRemote cust;
private List<VociCosto> list;
private VociCosto selected= new VociCosto();
private boolean UcDisabled=true;
@PostConstruct
public void init(){
setList(new ArrayList<VociCosto>());
setList(cust.getAll());
//selected.setModalita("0");
}
public String newCosto(){
return "editCosto";
}
public void onRowSelect(){
FacesContext fc = FacesContext.getCurrentInstance();
System.out.println("|||||"+ getSelected().getNome());
try {
fc.getExternalContext().redirect("editCosto.jsf");
} catch (IOException e) {
}
}
public VociCosto getSelected() {
return selected;
}
public void setSelected(VociCosto selected) {
this.selected = selected;
}
public void setCust(CostoBeanRemote cust) {
this.cust = cust;
}
public List<VociCosto> getList() {
return list;
}
public void setList(List<VociCosto> list) {
this.list = list;
}
public boolean isUcDisabled() {
return UcDisabled;
}
public void setUcDisabled(boolean ucDisabled) {
this.UcDisabled = ucDisabled;
}
}
这是VociCosto.java
package it.bway.timerep.entities;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;
/**
* The persistent class for the VOCI_COSTO database table.
*
*/
@Entity
@Table(name="VOCI_COSTO")
public class VociCosto implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID_COSTO", unique=true, nullable=false)
private int idCosto;
@Column(name="C_UNITARIO")
private int cUnitario;
@Column(length=1)
private String modalita;
@Column(length=50)
private String nome;
//bi-directional many-to-one association to NoteSpese
@OneToMany(mappedBy="vociCosto", fetch=FetchType.EAGER)
private Set<NoteSpese> noteSpeses;
public VociCosto() {
}
public int getIdCosto() {
return this.idCosto;
}
public void setIdCosto(int idCosto) {
this.idCosto = idCosto;
}
public int getCUnitario() {
return this.cUnitario;
}
public void setCUnitario(int cUnitario) {
this.cUnitario = cUnitario;
}
public String getModalita() {
return this.modalita;
}
public void setModalita(String modalita) {
this.modalita = modalita;
}
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Set<NoteSpese> getNoteSpeses() {
return this.noteSpeses;
}
public void setNoteSpeses(Set<NoteSpese> noteSpeses) {
this.noteSpeses = noteSpeses;
}
@Override
public boolean equals(Object obj) {
try{
VociCosto toCompare = (VociCosto) obj;
if (idCosto==toCompare.getIdCosto()) return true;
return false;
} catch (Exception e){
return false;
}
}
@Override
public int hashCode(){
return idCosto;
}
}
答案 0 :(得分:0)
你所做的与你期望的完全相反。你应该改变
#{vociCostoBean.selected.modalita != '1'}
到
#{vociCostoBean.selected.modalita == '1'}
工作测试用例
查看:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<h:outputText value="Modalita"></h:outputText>
<p:selectOneMenu value="#{selectMenuActions.value}" immediate="true">
<f:selectItem itemLabel="Importo" itemValue="0"/>
<f:selectItem itemLabel="Quantita" itemValue="1"/>
<p:ajax update="uc" />
</p:selectOneMenu>
<h:outputText value="Costo Unitario"></h:outputText>
<p:inputText id="uc" disabled="#{selectMenuActions.value == '1'}" value="#{vociCostoBean.selected.CUnitario}" />
<br></br>
</h:form>
</h:body>
</html>
Bean:
@ManagedBean
@ViewScoped
public class SelectMenuActions
{
private String m_sValue;
public void setValue(String p_sValue)
{
m_sValue = p_sValue;
}
public String getValue()
{
return m_sValue;
}
}