1

我已经实施了以下选择列表:

<p:pickList id="pickList" value="#{reportConfiguratorBean.dualListVars}" var="cRVariable" itemValue="#{cRVariable}" itemLabel="#{cRVariable.varName}" converter="#{cRImageTypeConverter}" immediate="true" rendered="true" >
    <f:facet name="sourceCaption">Available Variables</f:facet>
    <f:facet name="targetCaption">Associated Variables</f:facet>                
</p:pickList>                       
<f:facet name="footer"> 
    <p:commandButton id='varassociate' action="#{reportConfiguratorBean.setAssocImTypVariables()}" value='Associate' process="@this,pickList" />
</f:facet>

选择列表已正确填充(使用数据库中的 getAssocImTypVariables())。但我的问题我无法捕获用户更改的选项列表值会更改源列表和目标列表。我正在尝试使用 commandButton 方法“setAssocImTypVariables”来捕获更改,如下所示:

public void setAssocImTypVariables() {      
    System.out.println(">>>>>>>>>>>>>>> entered");          
    List<CRVariable> sourceVariables = this.dualListVars.getSource();
    List<CRVariable> targetVariables = this.dualListVars.getTarget();       

    System.out.println(dualListVars.getSource());
    System.out.println(dualListVars.getTarget());

    for (CRVariable sourceVariable:sourceVariables) {
        System.out.println(">>>>>>>>>>>>> I am a source variable: " + sourceVariable.getVarName());
    }       
    for (CRVariable targetVariable:targetVariables) {
        System.out.println(">>>>>>>>>>>>> I am a target variable: " + targetVariable.getVarName());
    }       
    }

因此,例如,如果我有一个带有 INITIAL source = (Obj1,Obj2,Obj3,Obj5,Obj7) 和 target = (Obj4,Obj6) 的选项列表,我将“Obj1”从源移动到目标但在我的控制台中我得到:

--------- 输入 [] []

所以我的 dualListVars(源和目标)没有填充!我有两个用于源和目标的空列表...

所以我的方法不能感知选项列表的变化......有什么想法吗?我是 java + primefaces 的新手,所以它可能是非常基本的东西:(

我还附上了getAssocImTypesOnLoad()方法:

public void getAssocImTypesOnLoad() {               
    Long imTypeId = Long.parseLong(virtualId);      
    List<CRVariable> source;
    List<CRVariable> target;        
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;      
    try 
    {
        tx = session.beginTransaction();            
        String hq3 = "select distinct v from CRVariable v join v.crimagetypes t where t.id in (:itid)";
        Query query3 = session.createQuery(hq3);
        query3.setParameter("itid",imTypeId);
        target = query3.list();
        String hq4 = "select v FROM CRVariable v WHERE v.id not in (" +
                "select distinct v1.id " +
                "from CRVariable v1 " +
                "join v1.crimagetypes t2 " +
                "where t2.id in (:itid))";
        Query query4 = session.createQuery(hq4);
        query4.setParameter("itid",imTypeId);
        source = query4.list();
        dualListVars = new DualListModel<CRVariable>(source, target);
        tx.commit();
    } 
    catch (Exception e) 
    {
        if (tx != null) 
        {
            e.printStackTrace();
        }
    } 
    finally 
    {
       session.close();
    }
}
4

3 回答 3

0

你为什么在 上使用immediate=true属性commandButton

如果我没记错的话,根据balusC 的一篇文章,请在此处查看immediate=true命令按钮上的apply model to values phase.

也许这就是为什么你的模型没有更新的原因?

(只是通过查看代码来猜测 - 我没有复制这个。)

于 2012-11-22T16:09:18.730 回答
0

一个正在运行的选项列表示例 picklist.xhtml

<!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:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">


<h:head></h:head>

<h:body>
<h:form>
    <h3>Basic PickList</h3>
    <p:pickList value="#{pickListBean.cities}" var="city"
        itemLabel="#{city}" itemValue="#{city}" />
<p:commandButton  value="Submit" action="#{pickListBean.displayList}" />    


</h:form>

</h:body>
</html>   

PickListBean.java

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import org.primefaces.model.DualListModel;

@ManagedBean
public class PickListBean {

   private DualListModel<String> cities;

   public PickListBean() {

  //Cities
  List<String> citiesSource = new ArrayList<String>();
  List<String> citiesTarget = new ArrayList<String>();

  citiesSource.add("Istanbul");
  citiesSource.add("Ankara");
  citiesSource.add("Izmir");
  citiesSource.add("Antalya");
  citiesSource.add("Bursa");
//  citiesTarget.add("T1");


  cities = new DualListModel<String>(citiesSource, citiesTarget);
   }

   public DualListModel<String> getCities() {
      return cities;
   }
   public void setCities(DualListModel<String> cities) {
      this.cities = cities;
   }
   public void displayList(){

     System.out.println("values in list are" +cities.getTarget());


   }


}
于 2013-07-16T09:47:49.253 回答
0

Take a closer look to the primefaces showcase : Primefaces picklist

Call the dualListVars directly. #{reportConfiguratorBean.dualListVars} is resolved to reportConfiguratorBean.getDualListVars() since dualListVars is a property (eg. a property with a getter and a setter).

<p:pickList id="pickList" value="#{reportConfiguratorBean.dualListVars}" var="cRVariable" itemValue="#{cRVariable}" itemLabel="#{cRVariable.varName}" converter="#{cRImageTypeConverter}" immediate="true" rendered="true" >
    <f:facet name="sourceCaption">Available Variables</f:facet>
    <f:facet name="targetCaption">Associated Variables</f:facet>                
</p:pickList> 

The action attribute is meant for business logic. You can catch here the source and target list. The process attribute tell JSF which components to process. In your case, you want the component picklist and commandButton to be processed. You can also process the whole form with process="@form".

<p:commandButton id='varassociate' action="#{reportConfiguratorBean.readMyAssocImTypVariables}" value='Associate' ajax='false' process="@this,pickList" immediate="true" />

The readMyAssocImTypVariables function :

 public void readMyAssocImTypVariables() {      
    List<CRVariable> sourceVariables = this.dualListVars.getSource();
    List<CRVariable> targetVariables = this.dualListVars.getTarget();       
    for (CRVariable sourceVariable:sourceVariables) {
        System.out.println(">>>>>>>>>>>>> I am a source variable: " + sourceVariable.getVarName());
    }       
    for (CRVariable targetVariable:targetVariables) {
        System.out.println(">>>>>>>>>>>>> I am a target variable: " + targetVariable.getVarName());
     }       
  }

Unrelated : Do NOT put business logic into getters/setters ! They are called multiple time by JSF.

于 2012-11-22T15:36:14.970 回答