简体   繁体   English

Jsf 2.0填充h:selectOneMenu进入页面

[英]Jsf 2.0 Populate h:selectOneMenu to access page

I am newbie in JSF 2.0, I worked in JSF 1.1 and 1.2 and I populate selectOneMenu in constructor of Managed bean's page. 我是JSF 2.0的新手,我在JSF 1.1和1.2中工作,并且在Managed bean页面的构造函数中填充selectOneMenu。 For when users to acces to page the List is populate. 当用户访问页面时,将填充列表。 example below. 下面的例子。 I put the same in JSF 2.0 but is not work, the selectOneMenu appears empty. 我在JSF 2.0中添加了相同的内容,但无法正常工作,selectOneMenu显示为空。

<h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}">
  <f:selectItems value="#{PersonBean.status}"/>
</h:selectOneMenu>

In constructor's managed bean I put: 在构造函数的托管bean中,我输入:

public class PersonBean {    
    private SelectItem[] status=new SelectItem[0];

    public PersonBean () {
        detallePersonas= new ArrayList();

 status= new SelectItem[3];
 status[0]=new SelectItem("S","Single");
 status[1]=new SelectItem("M","Married");
 status[2]=new SelectItem("D","Divorced");
    }
}
  1. Editor Netbeans 6.8 (JSF 2.0 default configuration wizard) 编辑器Netbeans 6.8(JSF 2.0默认配置向导)
  2. No Exception Error 无异常错误
  3. Never run the constructor PersonBean (I put a breakpoint and never stops) 永远不要运行构造函数PersonBean(我放了一个断点,永不停止)
  4. There are other ways to populate selects to load the page 还有其他方法可以填充选择以加载页面

This is the complete code: 这是完整的代码:

index.xhtml 的index.xhtml

<?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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Person</title>
    </h:head>
    <h:body>
        <h:form id="frmPerson">
            <h:outputLabel id="lblStatus" value="Status:"/>
            <h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}">
                <f:selectItems value="#{PersonBean.status}"/>
            </h:selectOneMenu>
        </h:form>
    </h:body>
</html>

PersonBean.java PersonBean.java

package com.prueba.backingbean;

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

/**
 *
 * @author Administrador
 */
@ManagedBean(name = "Person")
@ViewScoped
public class PersonBean {

    private String idStatus;
    private SelectItem[] status = new SelectItem[0];

    public PersonBean() {
        status = new SelectItem[3];
        status[0] = new SelectItem("S", "Single");
        status[1] = new SelectItem("M", "Married");
        status[2] = new SelectItem("D", "Divorced");
    }

    /**
     * @return the idStatus
     */
    public String getIdStatus() {
        return idStatus;
    }

    /**
     * @param idStatus the idStatus to set
     */
    public void setIdStatus(String idStatus) {
        this.idStatus = idStatus;
    }

    /**
     * @return the status
     */
    public SelectItem[] getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(SelectItem[] status) {
        this.status = status;
    }
}
          private Map<String,String>status = new HashMap<String,String>();
          .......
          status.put("S", "Single");
          status.put("M", "Married");
          status.put("D", "Divorced");

in JSF PAGE: 在JSF PAGE中:

         <h:selectOneMenu value="#{personBean.idStatus}"  >

            <f:selectItems value="#{personBean.status}"/>                

         </h:selectOneMenu>

It should be "personBean" instead of "PersonBean" (first letter should be lowercase). 它应该是“ personBean”而不是“ PersonBean”(首字母应小写)。 You also need getter for status ( getStatus() ) and setter/getter for idStatus ( setIdStatus() / getIdStatus() ). 您还需要状态的getter( getStatus() )和idStatus的setter / getter( setIdStatus() / getIdStatus() )。 Are they there? 他们在吗?

Have you tried using List instead of Array of SelectItems. 您是否尝试过使用List而不是SelectItems数组。 Below code might help you. 下面的代码可能会帮助您。

private List<SelectItem> status = new ArrayList<SelectItem>();

status.add(new SelectItem("S","Single"));
status.add(new SelectItem("M","Married"));
status.add(new SelectItem("D","Divorced"));

/*
     SETTER / GETTER
*/

Look here: 看这里:

@ManagedBean(name = "Person")
@ViewScoped
public class PersonBean {

You've declared the managed bean name as Person . 您已将托管bean名称声明为Person So it's in JSF EL available as #{Person} . 因此它在JSF EL中以#{Person} Yet you're attempting to access it as #{PersonBean} . 但是,您尝试以#{PersonBean}身份访问它。 Because such a bean does not exist, the menu remains empty. 由于不存在这样的bean,因此菜单保持为空。

You've 3 options: 您有3种选择:

  1. Rename #{PersonBean} by #{Person} in your JSF page. 在您的JSF页面中用#{Person}重命名#{PersonBean}

  2. Rename @ManagedBean(name = "Person") to @ManagedBean(name = "PersonBean") in your managed bean. 在托管bean中将@ManagedBean(name = "PersonBean")重命名@ManagedBean(name = "Person") @ManagedBean(name = "PersonBean")

  3. Get rid of bean name and use the default JSF naming conventions. 摆脱bean名称,并使用默认的JSF命名约定。 Ie just use @ManagedBean without a name and use #{personBean} in your JSF page (in essence, the bean's class name with first character lowercased). 也就是说,只使用不带name @ManagedBean并在JSF页面中使用#{personBean} (本质上是使用第一个字符小写的Bean类名)。

Option 3 is preferred. 选项3是首选。

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

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