1

我想为我正在开发的 JSF 项目使用 arquillian warp 测试框架。我知道我需要使用 CDI 注释而不是 JSF 注释才能使其正常工作。我正在使用@ViewScopedbean,所以我在我的项目中包含了接缝面来处理这个问题(我在 JBoss 7 上运行)。我已经修改了要使用的 bean,@Named并且在我使用的地方@PostConstruct我已经将它放入构造函数中,这一切似乎都很好。

当我使用 a 访问视图时,selectOneMenu它永远不会有任何列表项。这是视图和bean的代码。

看法:

<h:selectOneMenu value="#{ngoBean.ngo.country}" >
    <f:selectItems value="#{ngoBean.countryValues}" />
</h:selectOneMenu>

豆:

import com.a.Facade;
import com.a.CountryEnum;
import com.a.GoverningBody;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 */
@Named("ngoBean")
@ViewScoped
public class NgoBean implements Serializable {

    private GoverningBody ngo = new GoverningBody();
    private List<GoverningBody> ngoList;
    private boolean edit;

    private List<SelectItem> countryValues;

    @EJB(beanName = "NgoFacadeImpl")
    private Facade<GoverningBody> ngoController;

    public NgoBean(){
    }

    @PostConstruct
    public void init(){
        //TODO this is a bad way of loading db data i should change it
        ngoList = ngoController.findAll();

        countryValues = initCountryValues();
    }

    public void add(){
        ngoList.add(ngoController.save(ngo));

        //reset the variable
        ngo = new GoverningBody();
    }

    public void edit(GoverningBody item) {
        this.ngo = item;
        edit = true;
    }

    public void save() {
        ngo = ngoController.update(ngo);
        edit = false;
    }

    public void delete(GoverningBody item) {
        ngoController.delete(item);
        ngoList.remove(item);
    }

    public List<GoverningBody> getNgoList() {
        return ngoList;
    }

    public GoverningBody getNgo() {
        return ngo;
    }

    public boolean isEdit() {
        return edit;
    }

    public List<SelectItem> getCountryValues() {
        return countryValues;
    }

    public void setCountryValues(List<SelectItem> countryValues) {
        this.countryValues = countryValues;
    }

    public List<SelectItem> initCountryValues() {
        List<SelectItem> items = new ArrayList<>(CountryEnum.values().length);
        int i = 0;
        for(CountryEnum g: CountryEnum.values()) {
            items.add(new SelectItem(g, g.getName()));
        }

        System.out.println("items = " + items);
        return items;
    }
}

我尝试用注释方法,@Factory("countryValues")但这似乎没有帮助。

4

1 回答 1

0

此问题与症状无关。问题的根本原因是定位不正确,beans.xml这应该是在战争的 WEB-INF 目录中,而不是耳朵的 META-INF 目录。

我还更改了seam-faces依赖项以使用 apache CODI,这不是必需的,但我认为这个使用@ViewAccessScoped而不是@ViewScoped不同的名称不那么模棱两可。

于 2012-12-16T18:13:10.460 回答