p:dataTable选择的值setter不起作用

时间:2014-12-26 13:36:35

标签: jsf primefaces

我是JSF和PrimeFaces的新手,我无法理解为什么我的选择值的设定者没有设置:

我已经获得了主要的XHTML,其中点击<p:commandButton>后我想要更改所选行。但是,在更改数据设定器setSelectedBook()上的行选择后,请在入口处获取null值。

我已为selection设置了rowKey<p:dataTable>

看起来像:

主XTML:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      >

    <body>
        <ui:composition template="./../../WEB-INF/pagesTemplate.xhtml">
            <form>
                <ui:define name="content">

                    <p:dataTable id="eventsDT" var="book" value="#{bookHolder.books}"
                                 selectionMode= "single" selection="#{bookController.selectedBook}"
                                 rowKey="#{book.id}">

                        <p:ajax event="rowSelect"/>
                        <p:ajax event="rowDblselect" listener="#{bookController.onDoubleRowSelect}"  />

                        <p:column headerText="ID">
                            <h:outputText value="#{book.id}" />
                        </p:column>

                        <p:column headerText="Title">
                            <h:outputText value="#{book.longName}" />
                        </p:column>

                    </p:dataTable>
                </ui:define>

                <ui:define name="right">
                    <h:panelGrid columns="1" cellpadding="5">
                        <p:commandButton id="btnEdit" value="Edit"
                                         action="#{navigationController.moveToBookEditPageSimple}"
                                         style="width: 100px;height: 28px; margin-left: 10%"/>
                    </h:panelGrid>
                </ui:define>

            </form>
        </ui:composition>
    </body>
</html>

无法获得价值的BookController:

@Named("bookController")
@SessionScoped
@Local(BookView.class)
public class BookController implements Serializable {

    public BookController() {
        navigator = new NavigationController();
    }

    NavigationController navigator;

    @Inject
    BookHolder bookHolder;

    private Book selectedBook;

    public Book getSelectedBook() {
        return selectedBook;
    }

    public void setSelectedBook(Book selectedBookValue) {
        this.selectedBook = selectedBookValue;
    }

    public void onDoubleRowSelect(SelectEvent event) throws IOException {
        String str = navigator.showPage("BookEdit");
        FacesContext.getCurrentInstance().getExternalContext().redirect(str + ".xhtml");
    }      
}

包含数据的BookHolderBean:

@Named("bookHolder")
@ApplicationScoped
public class BookHolder {

    public BookHolder() {
    }

    @PostConstruct
    void setValues() {
        this.books = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            this.books.add(new Book(new ParkType(i, i,
                    "Book #: " + i + " (long name)",
                    "Book #: " + i + " (short name)",
                    "Book #: " + i + " (note)")));

        }
    }

    private List<Book> books;

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> parkTypes) {
        this.books = parkTypes;
    }
}

课本:

public class Book extends MainEntitie<Book> implements Serializable, SelectableDataModel<Book> {
    @Inject
    BookHolder bookHolder;

    private final String longName;
    private final String shortName;

    public Book(BookBase sd) {
        super(sd.getId(), false, false, sd.getNote());
        this.longName = sd.getLongName();
        this.shortName = sd.getShortName();
    }

    public String getLongName() {
        return longName;
    }

    public String getShortName() {
        return shortName;
    }

    @Override
    public int getId() {
        return super.getId();
    }

    @Override
    public Object getRowKey(Book t) {
        return t.getId();
    }

    @Override
    public Book getRowData(String string) {
        for (Book app : bookHolder.getBooks()) {
            if (app.getId() == Integer.parseInt(string)) {
                return app;
            }
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

由于您使用ui:define,我认为您的模板文件中存在ui:includepagesTemplate.xhtml)。 如果是这样,那很好,但是你所包含的页面有一些不必要的东西,例如html和body标签(顺便说一句:使用h:body)。没必要。

请参阅second part of this posting了解如何将xhtml文件包含到另一个文件中。

您会注意到ui:define的内容有点&#34;剪切&粘贴&#34;进入你的模板文件。 那么,您的form代码呢?它被忽略了(至少,我猜它不会成为你的数据表的父级)。 将h:form(再次使用JSF&#39; s h:form而不是HTML form)作为ui:define的子项, 像:

<ui:composition template="/WEB-INF/template.xhtml"
xmlns=.....>
   <ui:define name="content">
      <h:form>
         <p:dataTable ...>
            ...
         </p:dataTable>
      </h:form>
   </ui:define>
</ui:composition>