简体   繁体   English

如何将类别与 ManagedBean 和 xhtml 页面绑定

[英]How to bind categories with ManagedBean and xhtml page

I'm new to programming and I wanna ask: if I have a managedBean(with difference characteristics) and I want to add different categories, showing them on the xhtml page.. how can I bind those categories with managedBean and xhtml and put products in the right category after user add the product ?我是编程新手,我想问:如果我有一个 managedBean(具有不同的特性)并且我想添加不同的类别,将它们显示在 xhtml 页面上..如何将这些类别与 managedBean 和 xhtml 绑定并放置产品用户添加产品后在正确的类别中?

here is a part of my xhtml code:这是我的 xhtml 代码的一部分:

<ui:define name="content">
        <h2>New auction</h2>
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel value="Name:" />
                <h:inputText value="#{newAuctionWizard.auction.name}" />

                <h:outputLabel value="Description:" />
                <h:inputTextarea value="#{newAuctionWizard.auction.description}" />

                <h:commandButton value="Cancel" action="#{newAuctionWizard.cancel()}" />    
                <h:commandButton value="Details" action="newAuctionDetails" />  
            </h:panelGrid>
            <h:messages style="color: red" />
        </h:form>
    </ui:define>

and part of the java code:和部分java代码:

private static final long serialVersionUID = -38089703767395198L;

private Long id;
@Size(min=2, max=30, message = "The auction's name: minim {min} and maximum {max} caractere")
private String name;
@Size(max=1000, message = "The auction's description must be of maximum {max} characters")
private String description;
@Min(value = 1, message = "Original price should be at least 1 RON")
@NotNull(message = "Highest Bid")
private Long originalPrice;
private String location;

private User owner;
private Bid highestBid;

private List<Bid> bids = new ArrayList<Bid>();

I want the user to be able to choose a category and put its product in the right category when adding an auction.. for each category shall I make a Java class ?我希望用户能够在添加拍卖时选择一个类别并将其产品放在正确的类别中。我应该为每个类别创建一个 Java 类吗?

Thank you谢谢

In this post i will try to answer about your question (even if it's not very clear for me)在这篇文章中,我将尝试回答您的问题(即使对我来说不是很清楚)

in your category entity you will make the list of product that you will work with it will look like this在您的类别实体中,您将使您将使用的产品列表如下所示

categoryEntity.java类别实体.java

@Entity
@EntityListeners(QuerySessionLog.class)
@Table(name = "category")

....
@OneToMany(mappedBy = "category",  fetch = FetchType.LAZY , cascade = CascadeType.ALL ,orphanRemoval = true)
private List<productEntity> listProducts ;

Don't forget the getter and setter of your list不要忘记列表的 getter 和 setter

and in your productEntity it will look like this在您的 productEntity 中,它看起来像这样

productEntity.java产品实体.java

...
@JoinColumn(name = "category")
@ManyToOne(fetch = FetchType.LAZY)
private categoryEntity category; 

one more time don't forget the getter and setter再一次不要忘记 getter 和 setter

and finnaly you can access to this data throw category.listProducts最后你可以访问这个数据抛出category.listProducts

to show your informations you can found various example in the primefaces web site primefaces web site for example dataTable ...要显示您的信息,您可以在 primefaces 网站primefaces 网站中找到各种示例,例如 dataTable ...

Hope that helped you.希望对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM