gpt4 book ai didi

java - JSF 不工作 : Select SelectOneMenu And Dynamically Update Other SelectOneMenu With Ajax

转载 作者:行者123 更新时间:2023-12-02 05:14:42 25 4
gpt4 key购买 nike

我很长时间以来一直试图找出问题所在,但没有成功的结果。我在 Google 中搜索此内容,并尝试了几个不同的工作示例,但没有一个可以帮助我解决我的任务。

问题是,当“类别”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>

bean :

    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 位于同一位置。当组件未渲染时,无法找到其 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 类中,您需要将对象转换为字符串,并将该字符串再次转换为同一对象。您可以在这里查看更多信息: How create a custom coverter in JSF 2?

关于java - JSF 不工作 : Select SelectOneMenu And Dynamically Update Other SelectOneMenu With Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27064452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com