如何获得OneSelectMenu的价值

时间:2017-03-15 08:53:57

标签: jsf primefaces

我是新的jsf。我根据上传的txt动态填充组合框。但当ı想要获得选定的组合框值。我研究它并应用我的代码,但它不起作用。所选字符串始终返回null。

        <p:dataTable id="cellEditingTable" var="message"
        value="#{messageTableController.menuList}" paginator="true"
        paginatorPosition="bottom" editable="true" editMode="cell">
        <f:facet name="header">  
        PRE_INSTALL
        </f:facet>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Value" />
            </f:facet>
            <h:outputText value="#{message.value}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Assign" />
            </f:facet>
            <p:selectOneMenu value="#{messageTableController.selected}"
                style="width:96%">
                <f:selectItem itemLabel="#{message.assign}" itemValue="1" />
                <f:selectItem itemLabel="#{message.combo}" itemValue="2" />
            </p:selectOneMenu>
        </p:column>
    </p:dataTable>
    <br />
    <h:commandButton value="Submit Training"
        action="#{messageTableController.submitTraining}">
    </h:commandButton>

我的班级

@ManagedBean
@ViewScoped
@SessionScoped
public class MessageTableController implements Serializable {

private static final long serialVersionUID = 20111020L;

private UploadedFile file;
private File f;
private BufferedReader br;
private String line;
private String[] str;       
private String selected = "s";
private List<preList> menuList;


public String getSelected() {
    return selected;
}

public void setSelected(String selected) {
    this.selected = selected;
}

public List<preList> getMenuList() {
    return menuList;
}

public void setKullaniciList(List<preList> menuList) {
    this.menuList = menuList;
}

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void upload() throws IOException {
    if (file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        readUploadedFile();
    }
}

public void readUploadedFile() throws IOException {
    f = new File(file.getFileName());
    br = new BufferedReader(new FileReader(f));
    menuList = new ArrayList<preList>();
    while ((line = br.readLine()) != null) {
        if (line.contains("PI")) {
            str = line.split("=");
            if(str[1].contains("DISABLE")){
                menuList.add(new preList(str[0], str[1], "ENABLE"));
            }
            else if(str[1].contains("ENABLE")){
                menuList.add(new preList(str[0], str[1], "DISABLE"));
            }
            else if(str[1].contains("YES")){
                menuList.add(new preList(str[0], str[1], "NO"));
            }
            else if(str[1].contains("NO")){
                menuList.add(new preList(str[0], str[1], "YES"));
            }
            else{
                menuList.add(new preList(str[0], str[1], ""));
            }
        }
    }
    br.close();
}

public String submitTraining(){
    return "selectedOptions";
}


public class preList {

    private String value;

    private String assign;

    private String combo;

    public preList(String value, String assign, String combo) {
        this.assign = assign;
        this.value = value;
        this.combo = combo;
    }
   //set get methods...

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">


<h:outputText value="#{messageTableController.selected}" />

</html>

Output photo当我点击提交按钮时,空页面被打开

1 个答案:

答案 0 :(得分:1)

您的ManagedBean不应该同时具有视图和会话范围,它或者是或者。我还没有尝试在添加两个注释时会发生什么,但似乎它应用了视图范围,因为在您导航到第二个xhtml文件之后,所选的值不再存在(它不相同) view - &gt;你的bean的新实例)。 我希望我可以帮助你:)。