2

我使用 netbeans 7.2.1。JSF CRUD 生成的代码对实际应用程序有效吗?我创建了一个测试数据库并使用了 netbeans CRUD 生成器。它使用DataModelandPaginationHelper而不是 Lists 进行 CRUD 操作。有一个实体Test.java,一个TestFacade.javaTestController.java。和 jsf 文件list.xhtml, edit.xhtml, view.xhtml, create.xhtml. 我添加了一个NemdQuery实体文件:

@Entity
@NamedQuery(name = "Test.findByTestCriteria", query = "SELECT t FROM Test t WHERE t.testCriteria = true")
public class Test implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue    
    @Column(name = "id")
    private Integer id;    
    @Column(name = "title")
    private String title;
    @Column(name = "testCrieteria")
    private boolean testCrieteria; 

public Test() {
}

//Getters and Setters

并在 TestFacade.java 中创建了查询:

@Stateless
public class TestFacade extends AbstractFacade<Test> {
    @PersistenceContext(unitName = "testPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
    return em;
}

public TestFacade() {
    super(Test.class);
}

public List<Test> testCriteria(){
    Query q = em.createNamedQuery("Test.findByTestCriteria",Test.class);        
    return q.getResultList();
}    
}

我在 TestController.java 中添加了一个方法来检索 testCriteria Query:

@ManagedBean(name = "testController")
@SessionScoped
public class TestController implements Serializable {

    private Test current;
    private DataModel items = null;
    private DataModel testCriteria = null;
    @EJB
    private com.test.TestFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public Test getSelected() {
    if (current == null) {
        current = new Test();
        selectedItemIndex = -1;
    }
    return current;
}

public PaginationHelper getPagination() {
    if (pagination == null) {
        pagination = new PaginationHelper(10) {
            @Override
            public int getItemsCount() {
                return getFacade().count();
            }

            @Override
            public DataModel createPageDataModel() {
                return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
            }
        };
    }
    return pagination;
}

public DataModel getItems() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    return items;
}
public String prepareList() {
    recreateModel();
    return "List";
}

public String prepareView() {
    current = (Test) getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "View";
}
//getting testCriteria items
public DataModel getTestCriteria(){                
    if(items == null){
        items = getPagination().createPageDataModel();
    }
    testCriteria = new ListDataModel(ejbFacade.testCriteria());         
    return testCriteria;        
}
//custom view page
public String viewTest(){
    current = (Test) getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "ViewTest?faces-redirect=true";
}
//custom viewTestCriteria
public String viewTestCriteria(){
    current = (Test) getTestCriteria ().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "ViewTest?faces-redirect=true";
}

p:dataGrid并在 index.xhtml 中检索 testCriteria :

<h:form>
                        <p:dataGrid value="#{testController.testCriteria()}" var="item" columns="4">                                
                            <p:column>
                                <h:panelGrid columns="1">                                                                                
                                    <h:commandLink id="viewTestCriteria" value="#{item.title}" action="#{testController.viewTestCriteria()}"/>
                                </h:panelGrid>
                            </p:column>
                        </p:dataGrid>
                    </h:form>

使用此代码,所有 testCriteria 数据都存在于 index.xhtml 中,但是当我单击 commadnButton 来查看它们时,它们都显示第一个项目。似乎DataGrid它没有得到该selected项目。如果我刷新包含所有测试数据的 List.xhtml,然后返回 index.xhtml 并按 commandLink,它会引发 NoRowAvailable 异常。

我希望我已经清楚地说明了我的问题,并且我将不胜感激任何指南,因为我是这项技术的新手。

更新:经过两天的谷歌搜索和研究,我想到了使用<f:setPropertyActionListener value="#{item.id}" target="#{testController.selected.id}"><f:param name="#{testController.selected.id}" value="#{item.id}">但它没有用。

更新:到目前为止,我几乎确信没有选择任何行,因此数据网格返回第一行。但我仍然不确定如何修改viewTestCriteria()以正确设置current项目selectedItemIndex

4

0 回答 0