我一直在試圖找出問題很長時間,沒有成功的結果。我在谷歌搜索這個,我嘗試了幾個不同的工作例子,但沒有人幫助我解決我的任務。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;
}
}
它標記已經找到一個「消息」ID的錯誤。這是因爲panelGroup的id爲「Messages」,selectOneMenu的id爲「Messages」。我應該更改哪一個其他id名稱? – 2014-11-21 19:55:33
對不起,我忘了從selectOneMenu中刪除消息ID。我升級了答案。 – Corus 2014-11-21 19:57:05
仍然無效。正如我之前在回答用戶Mooolo時所說的那樣,我認爲在第一個SelectOneMenu更改過程中,「categoryBean.category」變量不會更改爲選定的類別/項目。 – 2014-11-21 20:02:44