如何在<h:selectonemenu> </h:selectonemenu>中显示值

时间:2012-06-18 17:58:26

标签: java jsf jsf-2

我有这个菜单:

<h:selectOneMenu value="#{ApplicationController.settings['SessionTTL']}">
    <f:selectItem itemValue="60" itemLabel="1 hour" />
    <f:selectItem itemValue="30" itemLabel="30 Minutes" />
    <f:selectItem itemValue="20" itemLabel="20 Minutes" />
    <f:selectItem itemValue="10" itemLabel="10 Minutes" />
    <f:selectItem itemValue="custom" itemLabel="Define custom value" />
    <f:ajax render="input" />
</h:selectOneMenu>&nbsp;
<h:panelGroup id="input">
    <h:inputText value="#{ApplicationController.settings['SessionTTL']}" rendered="#{ApplicationController.settings['SessionTTL'] == 'custom'}" required="true" />
</h:panelGroup>     

问题是我无法在菜单标签中看到该值。当我点击自定义并更新它时,我总是得到1 hour。如果我这样修改菜单:

<h:selectOneMenu value="#{ApplicationController.settings['SessionTTL']}">
    <f:selectItem itemValue="#{ApplicationController.settings['SessionTTL']}" itemLabel="#{ApplicationController.settings['SessionTTL']}" />
    <f:selectItem itemValue="60" itemLabel="1 hour" />
    <f:selectItem itemValue="30" itemLabel="30 Minutes" />
    <f:selectItem itemValue="20" itemLabel="20 Minutes" />
    <f:selectItem itemValue="10" itemLabel="10 Minutes" />
    <f:selectItem itemValue="custom" itemLabel="Define custom value" />
    <f:ajax render="input" />
</h:selectOneMenu>&nbsp;
<h:panelGroup id="input">
    <h:inputText value="#{ApplicationController.settings['SessionTTL']}" rendered="#{ApplicationController.settings['SessionTTL'] == 'custom'}" required="true" />
</h:panelGroup>

我总是在菜单标签中获得第一个值。你知道为什么我不能在菜单标签中显示自定义值吗?

PS 这是完整的源代码:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"    
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <ui:insert name="header">           
            <ui:include src="header.xhtml"/>         
        </ui:insert>
    </h:head>
    <h:body>
        <h1><img src="resources/css/images/icon.png" alt="DX-57" /> Settings Center</h1>
        <!-- layer for black background of the buttons -->
        <div id="toolbar" style="margin: 0 auto; width:1180px; height:30px; position:relative;  background-color:black">
            <!-- Include page Navigation -->
            <ui:insert name="Navigation">           
                <ui:include src="Navigation.xhtml"/>         
            </ui:insert>
        </div>  
        <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
            <h:graphicImage alt="General Settings"  style="position:relative; top:-20px; left:9px;"  value="resources/images/logo_general_settings.png" />
        </div>
        <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute;  background-color:transparent; top:105px">

            <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute;  background-color:transparent; top:80px">
                <h:form id="settingsupdate">
                    <p:growl id="growl" showDetail="true" sticky="true" />

                    <div id="settingsdivone" style="width:550px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

                        <h:panelGrid columns="2">
                            <h:panelGroup>Application Timestamp Format</h:panelGroup>
                            <h:panelGroup>
                                <h:selectOneMenu value="#{GeneralController.settings['TimestampFormat']}">
                                    <f:selectItem itemValue="#{GeneralController.settings['TimestampFormat']}" itemLabel="#{GeneralController.settings['TimestampFormat']}" />
                                    <f:selectItem itemValue="yyyy-MM-dd-HH.mm.ss.ffffff" itemLabel="yyyy-MM-dd-HH.mm.ss.ffffff" />
                                    <f:selectItem itemValue="YYYY-MM-DD HH:MM:SS" itemLabel="YYYY-MM-DD HH:MM:SS" />
                                    <f:selectItem itemValue="YYYY-MM-DDThh:mm:ss.sTZD" itemLabel="YYYY-MM-DDThh:mm:ss.sTZD" />
                                    <f:selectItem itemValue="custom" itemLabel="Define custom value" />
                                    <f:ajax render="timestampformat" />
                                </h:selectOneMenu>&nbsp;
                                <h:panelGroup id="timestampformat">
                                    <h:inputText value="#{GeneralController.settings['TimestampFormat']}" rendered="#{GeneralController.settings['TimestampFormat'] == 'custom'}" required="true" />
                                </h:panelGroup>          
                            </h:panelGroup>

                            <h:panelGroup>Performance Tuning</h:panelGroup>
                            <h:panelGroup>
                                <h:selectOneMenu value="#{GeneralController.settings['PerformTunung']}">
                                    <f:selectItem itemValue="#{GeneralController.settings['PerformTunung']}" itemLabel="#{GeneralController.settings['PerformTunung']}" />
                                    <f:selectItem itemValue="10000" itemLabel="Less than 10000 table rows" />
                                    <f:selectItem itemValue="1000000" itemLabel="More than 1000000 table rows" />                                   
                                    <f:selectItem itemValue="custom" itemLabel="Define custom value" />
                                    <f:ajax render="performtuning" />
                                </h:selectOneMenu>&nbsp;
                                <h:panelGroup id="performtuning">
                                    <h:inputText value="#{GeneralController.settings['PerformTunung']}" rendered="#{GeneralController.settings['PerformTunung'] == 'custom'}" required="true" />
                                </h:panelGroup>
                            </h:panelGroup>  

                            <h:panelGroup>Application Initialization State</h:panelGroup>
                            <h:panelGroup>
                                <h:selectOneMenu value="#{GeneralController.settings['InitState']}">
                                    <f:selectItem itemValue="#{GeneralController.settings['InitState']}" itemLabel="#{GeneralController.settings['InitState']}" />
                                    <f:selectItem itemValue="1" itemLabel="Level 1" />
                                    <f:selectItem itemValue="2" itemLabel="Level 2" />
                                    <f:selectItem itemValue="3" itemLabel="Level 3" />
                                    <f:selectItem itemValue="custom" itemLabel="Define custom value" />
                                    <f:ajax render="initstate" />
                                </h:selectOneMenu>&nbsp;
                                <h:panelGroup id="initstate">
                                    <h:inputText value="#{GeneralController.settings['InitState']}" rendered="#{GeneralController.settings['InitState'] == 'custom'}" required="true" />
                                </h:panelGroup>
                            </h:panelGroup>  

                        </h:panelGrid>          

                    </div>   

                    <div id="settingsonedivtwo" style="width:350px; height:400px; position:absolute;  background-color:transparent; top:20px; left:400px">

                    </div>   

                    <div id="settingstwodivthree" style="width:150px; height:60px; position:absolute;  background-color:transparent; top:380px; left:800px">
                        <p:commandButton value="Save Settings" id="ajax" actionListener="#{GeneralController.updateDBSettings}" rendered="true" update="growl,settingsupdate"/>
                    </div> 
                </h:form> 
            </div>  
        </div>
    </h:body>
</html>

1 个答案:

答案 0 :(得分:1)

根据您的更新,您只需要使用JavaScript更新输入文本,无需添加呈现的属性,也不需要包含值的panelGroup。

<script type="text/javascript">
    function updateInputText(ddlChanged, inputTextId) {
        var realId= "settingsupdate:" + inputTextId;
        document.getElementById("realId").value = 
            ddlChanged.options[ddlChanged.selectedIndex].text;
    }
</script>
<h:form id="settingsupdate">
    <!-- another components... -->
    <h:selectOneMenu id="ddlMyOptions" value="#{GeneralController.settings['InitState']}"
        onchange="updateInputText(this, 'txtMyText');">
        <f:selectItem itemValue="1" itemLabel="Level 1" />
        <f:selectItem itemValue="2" itemLabel="Level 2" />
        <f:selectItem itemValue="3" itemLabel="Level 3" />
        <f:selectItem itemValue="custom" itemLabel="Define custom value" />
    </h:selectOneMenu>
    <h:inputText id="txtMyText" />
</h:form>
相关问题