以下是该方案:
用户获得具有两个不同标签的TabView
组件。第一个选项卡包含对象Person
的输入字段。第二个选项卡包含对象Book
的输入字段。除此之外,第二个选项卡显示Book
个对象的列表。
用户应先输入Person
的详细信息,然后切换到第二个标签页并将Book
添加到图书清单中。让我们保持这一点。
以下表单嵌入在模板中,其中包含TabView
:
<h:form id="personBookForm">
<p:log id="personBookLog"></p:log>
<p:panel id="personToggleablePanel" toggleable="true" collapsed="false"
header="Passionate book lovers" toggleSpeed="300">
<f:facet name="actions">
<p:commandButton id="savePersonBookButton"
action="#{personController.savePerson}" icon="ui-icon-disk"
process="@form" />
</f:facet>
<p:tabView id="personBookTabView">
<p:tab id="personTab" title="Person">
<p:panelGrid id="personPanelGrid" columns="1">
<p:inputText id="nameInput"
value="#{personController.currentPerson.name}" />
<p:inputText id="surnameInput"
value="#{personController.currentPerson.surname}" />
</p:panelGrid>
</p:tab>
<p:tab id="bookTab" title="Books">
<p:panelGrid id="bookPanelGrid">
<p:inputText id="titleInput"
value="#{personController.currentBook.title}" />
</p:panelGrid>
<p:dataTable id="bookTable" var="book"
value="#{personController.bookList}" rowKey="#{book.id}"
selectionMode="single" selection="#{personController.currentBook}">
<f:facet name="header">
<p:commandButton id="addBookButton" icon="ui-icon-plusthick"
style="float:right" />
<p:commandButton id="removeBookButton" icon="ui-icon-minusthick"
style="float:right" />
<p:commandButton id="refreshBookTableButton"
icon="ui-icon-refresh" style="float:right;" />
</f:facet>
<p:column headerText="Title">
<h:outputText value="#{book.title}" />
</p:column>
</p:dataTable>
</p:tab>
</p:tabView>
</p:panel>
</h:form>
控制器类PersonController.java
:
@Named("personController")
@ViewScoped
public class PersonController implements Serializable {
private Person currentPerson;
private Book currentBook;
private List<Book> bookList;
public PersonController() { }
@PostConstruct
public void init() {
currentPerson = new Person();
currentBook = new Book();
bookList = new ArrayList<>();
}
public void savePerson() {
System.out.println("Saving person " + currentPerson);
}
public Person getCurrentPerson() {
return currentPerson;
}
public void setCurrentPerson(Person currentPerson) {
this.currentPerson = currentPerson;
}
public Book getCurrentBook() {
return currentBook;
}
public void setCurrentBook(Book currentBook) {
this.currentBook = currentBook;
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
}
Book.java
:
public class Book {
private Long id;
private String title;
public Book() { }
public Book(Long id, String title) {
this.id = id;
this.title = title;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
}
Person.java
:
public class Person {
private String name;
private String surname;
private List<Book> books;
public Person() { }
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
@Override
public String toString() {
return name + " " + surname;
}
}
现在,每次提交表单时,Book
对象(请参阅控制器类中的currentBook
)都设置为null。
我发现的是,如果
,它没有任何区别process=@form
已设置Enter
或点击commandButton
时提交表单(为什么会这样,反正?!)Book
对象的值form
Panel
元素
Panel
我没想到Book
对象被设置为null
。
如果从视图中删除dataTable
,则所有内容均按预期工作。
为什么?这是理想的行为吗?有人可以解释一下会发生什么吗?
提前多多感谢。