2014-11-21 70 views
0

我一直在試圖找出問題很長時間,沒有成功的結果。我在谷歌搜索這個,我嘗試了幾個不同的工作例子,但沒有人幫助我解決我的任務。JSF不工作:選擇SelectOneMenu並動態更新其他SelectOneMenu With Ajax

問題是,當「類別」selectOneMenu更改時,「消息」selectOneMenu不會更新。它不加載所選類別的消息。

在HTML中,我只有兩個selectOneMenu元素。在服務類中,我創建用戶,類別和消息並將它們添加到鏈接列表中。

HTML

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html"> 

    <h:head> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
    </h:head> 

    <h:body> 
     <h:form> 
      <h:outputText value="You have no categories made yet..." rendered="#{empty categoryBean.categories}" /> 
      <h:selectOneMenu value="#{categoryBean.category}" rendered="#{not empty categoryBean.categories}"> 
       <f:selectItems value="#{categoryBean.categories}" /> 
       <f:ajax event="change" render="messages" listener="#{categoryBean.changeCategory}" /> 
      </h:selectOneMenu> 

      <h:outputText value="This category has no messages yet..." rendered="#{empty categoryBean.messages}" /> 
      <h:selectOneMenu value="#{categoryBean.message}" id="messages" rendered="#{not empty categoryBean.messages}"> 
       <f:selectItems value="#{categoryBean.messages}" /> 
      </h:selectOneMenu> 
     </h:form> 
    </h:body> 
</html> 

豆:

package beans; 

    @Named(value = "categoryBean") 
    @SessionScoped 
    public class CategoryBean implements Serializable { 
     private List<Message> messages; 
     private Category category; 
     private Message message; 

     @Inject 
     private Service service; 
     /** 
     * Creates a new instance of CategoryBean 
     */ 
     public CategoryBean() { 
      category = new Category(); 
     } 

     public void setCategory(Category category) { 
      this.category = category; 
     } 

     public Category getCategory(){ 
      return this.category; 
     } 

     public List<Category> getCategories() { 
      return service.getCategories(); 
     } 

     public List<Message> getMessages() { 
      return this.category.getMessageList(); 
     } 

     public Message getMessage() { 
      return message; 
     } 

     public void setMessage(Message message) { 
      this.message = message; 
     } 

     public void changeCategory() { 
      this.messages = category.getMessageList(); 
     } 
    } 

服務:

package service; 

@Named(value = "service") 
@ApplicationScoped 
public class Service { 
    private final ArrayList<User> users = new ArrayList<>(); 

    public Service() { 
     User u1 = new User("Mikolaj", "Stasinski", "mikolaj", "123456", 2); 
     User u2 = new User("Toms", "Bugna", "toms", "789012", 2); 
     User u3 = new User("Algie", "Tiempo", "algiemay", "123456", 1); 

     users.add(u1); 
     users.add(u2); 
     users.add(u3); 

     Category c1 = new Category(u1, "SADP", "Advanced Java"); 
     Category c2 = new Category(u1, "SADP Kaj", "JSF"); 
     Category c3 = new Category(u2, "CNDS", "Networking"); 

     u1.addCategory(c1); 
     u1.addCategory(c2); 
     u2.addCategory(c3); 

     Message m1 = new Message(u3, c3, "This is a message."); 
     Message m2 = new Message(u3, c1, "Another Message1"); 
     Message m3 = new Message(u3, c1, "Another Message2"); 

     u3.addMessage(m1); 
     u3.addMessage(m2); 
     u3.addMessage(m3); 

     c1.addMessage(m2); 
     c1.addMessage(m3); 
     c3.addMessage(m1); 
     c2.addMessage(m3); 
    } 

    public List<User> getUsers(){ 
     return new ArrayList<>(users); 
    } 

    public List<Category> getCategories(){ 
     ArrayList<Category> categoryList = new ArrayList<>(); 

     for (User u: users) { 
      for (Category cat: u.getCategoryList()) { 
       categoryList.add(cat); 
      } 
     } 

     return categoryList; 
    } 
} 

回答

1

問題是渲染的條件與id在同一個地方。未呈現組件時,無法找到其ID。然後,第一個選擇無法正確刷新第二個選擇。插入帶有渲染條件的組件的容器內的用於刷新ID:

<h:form> 
    <h:outputText 
     value="You have no categories made yet..." 
     rendered="#{empty categoryBean.categories}" /> 

    <h:selectOneMenu 
     value="#{categoryBean.category}" 
     rendered="#{not empty categoryBean.categories}"> 
     <f:selectItems value="#{categoryBean.categories}" /> 
     <f:ajax event="change" 
      render="messages" 
      listener="#{categoryBean.changeCategory}" /> 
    </h:selectOneMenu> 

    <h:panelGroup id="messages"> 
     <h:outputText 
      value="This category has no messages yet..." 
      rendered="#{empty categoryBean.messages}" /> 
     <h:selectOneMenu 
      value="#{categoryBean.message}" 
      rendered="#{not empty categoryBean.messages}"> 
      <f:selectItems value="#{categoryBean.messages}" /> 
     </h:selectOneMenu> 
    </h:panelGroup> 

</h:form> 

更新:第二個問題是您選擇的管理對象。 selectOneMenu在內部使用字符串來管理這些值,以使用您需要實現的對象併爲每種類型使用一個Converter。在此Converter類中,您需要將字符串中的對象轉換爲同一對象中的此字符串。你可以在這裏看到更多的信息: How create a custom coverter in JSF 2?

+0

它標記已經找到一個「消息」ID的錯誤。這是因爲panelGroup的id爲「Messages」,selectOneMenu的id爲「Messages」。我應該更改哪一個其他id名稱? – 2014-11-21 19:55:33

+0

對不起,我忘了從selectOneMenu中刪除消息ID。我升級了答案。 – Corus 2014-11-21 19:57:05

+0

仍然無效。正如我之前在回答用戶Mooolo時所說的那樣,我認爲在第一個SelectOneMenu更改過程中,「categoryBean.category」變量不會更改爲選定的類別/項目。 – 2014-11-21 20:02:44

1

這可能是因爲簽名你監聽方法是不正確的,它應該是這樣的:

public void changeCategory(AjaxBehaviorEvent event) {  
    this.messages = category.getMessageList(); 
}  

看一看這篇文章The f:ajax listener method in h:selectOneMenu is not executed,並按照工作實例。

+0

我試過這個,只是再試一次。不。這是行不通的。仍然沒有改變第二個SelectOneMenu。有任何想法嗎? – 2014-11-21 18:09:09

+0

問題是,首先SelectOneMenu更改categoryBean.category變量不會更改爲選定的類別。我如何解決這個問題? – 2014-11-21 18:26:50