简体   繁体   English

从代码中获取SelectOneMenu的选定项

[英]Get SelectOneMenu 's selected item from code

I'm trying to get a primefaces SelectOneMenu selected item's name from code: 我正在尝试从代码中获取素数SelectOneMenu所选项目的名称:

This is my SelectOneMenu: 这是我的SelectOneMenu:

FacesContext.getCurrentInstance().getViewRoot().findComponent("formMain:somSelect");

The Component is found. 找到组件。 I already tried to cast it into a SelectOneMenu but I won't get a method like "getSelectedValue()" which is written in the user manual in "client side api". 我已经尝试过将其转换为SelectOneMenu,但不会获得类似“ getSelectedValue()”的方法,该方法在用户手册的“客户端api”中编写。 I also tried: 我也尝试过:

FacesContext.getCurrentInstance().getViewRoot().findComponent("formMain:somSelect").getAttributes().get("label");

But this returns NPE. 但这将返回NPE。

<p:selectOneMenu id="somSelect" value="#{userManagerBean.somValue}" valueChangeListener="#{userManagerBean.somListener}" styleClass="selecters">
<f:selectItems value="#{userSelectBean.userList}" />
</p:selectOneMenu>

UserManagerBean.java UserManagerBean.java

@ManagedBean
@RequestScoped
public class UserManagerBean {

    private String somValue;
    private String selectedUser;
    private List<User> userData;    
    private List<User> users;

    public UserManagerBean() {  

    }

    public String getSomValue(){
        return somValue;        
    }

    public void setSomValue(String somValue){
        this.somValue = somValue;
    }

    // Getter for Table Content
    public List<User> getUserData() {
        return userData;
    }

Any ideas? 有任何想法吗?

€: The problem is that the selected item is only returned in the getter if I call a method and an ajax update: €:问题在于,如果我调用方法和ajax更新,则仅在getter中返回所选项目:

<p:ajax update="panelMain" listener="#{userManagerBean.changeEvent}" />

But I don't get an correct selected item on page load ( item = null ). 但是我在页面加载时没有获得正确的选定项目(item = null)。

As you are using list of beans to populate f:selectItems you should add converter or provide itemValue attribute. 在使用bean列表填充f:selectItems ,应添加转换器或提供itemValue属性。 In your case as value is String provide itemValue with some String identifier of your User bean: 在您的情况下,值是StringitemValue提供User Bean的一些String标识符:

<f:selectItems value="#{userSelectBean.userList}" var="u" itemValue="#{u.code}" itemLabel="#{u.name}"/>

Change properties code and name to those which you have in your User bean. 将属性codename更改为User Bean中的属性codename Be shore that itemValue points to String as your value in backing bean is String . 支持itemValue指向String因为您在后备bean中的值是String

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

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