java 如何从 jsf 中的数据库表中填充 h:selectOneMenu ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13880314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:19:11  来源:igfitidea点击:

how to populate h:selectOneMenu from the database table in jsf ?

javajsfselectonemenu

提问by Hirren Gamit

I have a category table

我有一个类别表

 categoryId
 catName
 description
 image 

I want to populate the <h:selectOneMenu>with the itemLabelcategoryNameand it's value with categoryId.

我想填充<h:selectOneMenu>itemLabelcategoryName和它与价值categoryId

It should be done with the ManagedBeanhow can i do this ??

应该用ManagedBean我该怎么做??

采纳答案by nayef

you can use a list of SelectItem for this. you will need a method that generates a list of selectitems like the following one in you managed bean,

您可以为此使用 SelectItem 列表。您将需要一种方法来在您的托管 bean 中生成如下所示的选择项列表,

public List<SelectItem> getAllCatagories(){

   List<SelectItem> items = new ArrayList<SelectItem>();
   List<Category> categoryList = dao.getAllCategory();
    for(Category category: categotyList){
       items.add(new SelectItem(category.getCategoryId(), category.getName()));
   }
   return items;
}

and use it like this

并像这样使用它

<h:selectOneMenu value="#{controllerBean.selectedCategory}" >
            <f:selectItems value="#{controllerBean.allCategories}"/>
</h:selectOneMenu>

回答by nayef

try something like this

尝试这样的事情

xhtml

html

    <h:form>
        <h:panelGrid>
            <h:selectOneMenu value="#{myMB.id}">
                <f:selectItem itemLabel="Please select one" itemValue="#{null}" />
                <f:selectItems value="#{myMB.items}" />
            </h:selectOneMenu>
        </h:panelGrid>
        <h:commandButton action="#{myMB.go}" value="Go"/>
    </h:form>

mb

mb

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

@ManagedBean
@ViewScoped
public class MyMB implements Serializable{

    private static final long serialVersionUID = 1L;
    private List<SelectItem> items = new ArrayList <SelectItem> ();
    private Long id;

    @PostConstruct
    public void init(){
        SelectItem si = new SelectItem();
        si.setLabel("My Label");
        si.setValue(666L);

        items.add(si);
    }

    public List<SelectItem> getItems() {
        return items;
    }

    public void setItems(List<SelectItem> items) {
        this.items = items;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void go(){
        System.out.println(id);
    }
}

回答by cн?dk

You can just use a loop to get data from your list like this:

您可以使用循环从列表中获取数据,如下所示:

<h:outputText value="Role :" />
<p:selectOneMenu value="#{myBean.id_Role}">
    <c:forEach var="role" items="#{myBean.rolesList}">
        <f:selectItem itemLabel="${role.name_Role}" itemValue="${role.id_Role}" />
    </c:forEach>
</p:selectOneMenu>

And in your bean:

在你的豆子中:

private List<Role> rolesList;
//And then fill the list from database 

In my case I use hibernate to get data from the database, it worked for me.

就我而言,我使用休眠从数据库中获取数据,它对我有用。

I hope it's helpful!

我希望它有帮助!

回答by Fritz

You have to make use of the f:selectItemstag:

你必须使用f:selectItems标签:

<h:selectOneMenu value="#{yourBean.itemValue}">                     
    <f:selectItems value="#{yourBean.yourItems}" />
</h:selectOneMenu>

Then, YourBeanneeds to have a Mapfield, storing the values fetched from the database where the key of the map (which I suggest to be a String) is the label and the value is the associated object.

然后,YourBean需要有一个Map字段,存储从数据库中获取的值,其中映射的键(我建议是字符串)是标签,值是关联的对象。

Map<String,YourObject> yourItems = new HashMap<String,YourObject>();

public Map<String,YourObject> getYourItems() {
    return yourItems;
}