【问题标题】:set a managed bean property from composite component从复合组件设置托管 bean 属性
【发布时间】:2013-06-19 20:09:00
【问题描述】:

我有一个名为 Person 的模型类和一个包含人员列表的视图范围托管 bean PersonController

我创建了一个复合组件来获取此人员列表。我想要做的是直接从复合组件设置名为 TestCompositeComponent 的其他托管 bean 中的人员列表..任何解决方案?..这是我的代码:

@ManagedBean
@ViewScoped
public class PersonController {

    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    @PostConstruct
    public void init() {
        persons = new ArrayList<>();
        Person person1 = new Person();
        person1.setFirstname("blah");
        person1.setLastname("blah");

        Person person2 = new Person();
        person2.setFirstname("blah");
        person2.setLastname("blah");

        persons.add(person1);
        persons.add(person2);

    }

}



@ManagedBean
@ViewScoped
public class TestCompositeComponentController  {


    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }


}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html >
<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:composite="http://java.sun.com/jsf/composite">

<composite:interface componentType="testCompositeController">

    <composite:attribute name="persons" />


</composite:interface>

<composite:implementation>

    <h:outputText value="composite"></h:outputText>
</composite:implementation>

</html>


<!DOCTYPE html >
<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:test="http://java.sun.com/jsf/composite/test"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
    <test:test persons="#{personController.persons}" />
</h:body>
</html>

【问题讨论】:

    标签: jsf-2 managed-bean composite-component


    【解决方案1】:

    是否要将从 PersonController 传递到 TestCompositeComponentController 的 List 人员更改回 PersonController?

    如果是这种情况,请将 PersonController 的作用域更改为 @SessionScoped 并将其注入到 TestCompositeComponentController。这样您就可以修改 PersonController 中的人员。

    @ManagedBean
    @ViewScoped
    public class TestCompositeComponentController  {
    
    
        private List<Person> persons;
        @Inject PersonController personController;
    
        public List<Person> getPersons() {
            return persons;
        }
    
        public void setPersons(List<Person> persons) {
            this.persons = persons;
        }
    
        public void updateTheOtherList() {
           personController.setPersons(this.persons);
        }
    }
    

    在实际情况下,复合组件中的修改将保存在数据库中,当您返回时,更改将通过人员列表上的新提取可见。

    【讨论】:

    • @Inject 不是仅 CDI 注释吗?这也适用于 @ManagedProperty 吗?
    • 我的建议是你在 TestCompositeComponentController 中独立处理人员,最后通过 CDI 更新 PersonController。
    猜你喜欢
    • 2014-11-03
    • 2011-12-30
    • 2011-07-10
    • 1970-01-01
    • 2011-02-20
    • 2014-10-29
    • 2012-07-31
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多