【问题标题】:Get JSF Drop down value and save to database [duplicate]获取JSF下拉值并保存到数据库[重复]
【发布时间】:2016-01-15 17:00:11
【问题描述】:

我在数据库中有两个表:BookCategory。现在我想创建一个页面,用户可以在其中将书籍添加到 Book 表中,但要从 Category 表中选择适当的类别。

我可以将 book 添加到表格中,但我无法在 Book 表格中保存价值 category

[

如您所见,Book 表中的 category外键category_id 来自 Category 表格。

这里是模型类:

图书模型

@Entity
@Table(name = "book")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b"),
@NamedQuery(name = "Book.findByBookId", query = "SELECT b FROM Book b WHERE b.bookId = :bookId")})

public class Book implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "book_id")
private Integer bookId;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "name")
private String name;
@Lob
@Size(max = 65535)
@Column(name = "description")
private String description;
@JoinColumn(name = "category", referencedColumnName = "category_id")
@ManyToOne
private Category category;

public Book() {
}

public Book(Integer bookId) {
    this.bookId = bookId;
}

public Book(Integer bookId, String name) {
    this.bookId = bookId;
    this.name = name;
}

public Integer getBookId() {
    return bookId;
}

public void setBookId(Integer bookId) {
    this.bookId = bookId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (bookId != null ? bookId.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Book)) {
        return false;
    }
    Book other = (Book) object;
    if ((this.bookId == null && other.bookId != null) || (this.bookId != null && !this.bookId.equals(other.bookId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.biblioteka.app.domen.Book[ bookId=" + bookId + " ]";
}

}

类别模型

 @Entity
 @Table(name = "category")
 @XmlRootElement
 @NamedQueries({
@NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
@NamedQuery(name = "Category.findByCategoryId", query = "SELECT c FROM Category c WHERE c.categoryId = :categoryId")})

 public class Category implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "category_id")
private Integer categoryId;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "name")
private String name;
@Lob
@Size(max = 65535)
@Column(name = "description")
private String description;
@OneToMany(mappedBy = "category")
private Collection<Book> bookCollection;

public Category() {
}

public Category(Integer categoryId) {
    this.categoryId = categoryId;
}

public Category(Integer categoryId, String name) {
    this.categoryId = categoryId;
    this.name = name;
}

public Integer getCategoryId() {
    return categoryId;
}

public void setCategoryId(Integer categoryId) {
    this.categoryId = categoryId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@XmlTransient
public Collection<Book> getBookCollection() {
    return bookCollection;
}

public void setBookCollection(Collection<Book> bookCollection) {
    this.bookCollection = bookCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (categoryId != null ? categoryId.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Category)) {
        return false;
    }
    Category other = (Category) object;
    if ((this.categoryId == null && other.categoryId != null) || (this.categoryId != null && !this.categoryId.equals(other.categoryId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.biblioteka.app.domen.Category[ categoryId=" + categoryId + " ]";
}

}

现在我有 JSF 页面,我可以在其中将书籍添加到数据库中。我有将类别加载到其中的下拉列表。用户应该选择一个类别并将书籍保存到表格中。

这是来自 JSF addBook 页面的代码。

  <p:layoutUnit position="center">
            <h:form>
                <p:inputText value="#{bookBean.name}" a:placeholder="Ime  knjige"></p:inputText><br/>
                <p:inputText value="#{bookBean.description}" a:placeholder="Opis knjige"></p:inputText><br/>
                <p:selectOneMenu value="#{bookBean.category}">
                    <f:selectItems value="#{categoryBean.allCategories}" var="c"
                                   itemLabel="#{c.name}" itemValue="#{c.categoryId}"/>

                </p:selectOneMenu>
                <b/><b/>
                <p:commandButton value="Dodaj knjigu" action="#{bookBean.addBook()}"/>
            </h:form>
        </p:layoutUnit>

如您所见,我使用 selectOneMenu 和值 bookBean.category 然后我不确定我需要在 selectItems 中设置什么值>.

这是BookBean代码:

 @ManagedBean
 @ApplicationScoped
 public class BookBean {

String name;
String description;
int categoryId;
Category category;

@Inject
public BookEJB bookEJB;

public void addBook(){

    Book book = new Book();
    book.setName(name);
    book.setDescription(description);
    book.setCategory(category);

    bookEJB.addBook(book);
}

public List<Book> getAllBooks(){
    return bookEJB.getAll();
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getCategoryId() {
    return categoryId;
}

public void setCategoryId(int categoryId) {
    this.categoryId = categoryId;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public BookEJB getBookEJB() {
    return bookEJB;
}

public void setBookEJB(BookEJB bookEJB) {
    this.bookEJB = bookEJB;
}

}

【问题讨论】:

    标签: jsf jsf-2 selectonemenu


    【解决方案1】:

    试试这个:

     <f:selectItems value="#{categoryBean.allCategories}" var="c"
                                   itemLabel="#{c.name}" itemValue="#{c}"/>
    

    列出的项目名称将是类别名称,类别将分配给bookBean.category,这可以设置为书籍类别并持久化。

    希望这会有所帮助。

    【讨论】:

    • 我已经尝试过了,但不知何故它不起作用。你还有什么想法吗?
    • 下一次,不要说“它不起作用”,而是在错误消息上用谷歌搜索。错误信息就是答案。这只是找到解释的问题。你可以找到stackoverflow.com/q/4734580
    • @BalusC 谢谢,这有帮助。现在我得到了 NPE,因为 Converter 中的 ManagedBean 似乎总是为空。我该如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 2014-12-25
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多