Tuesday 4 June 2013

JSF selectOneListbox tag example

1). JSF selectOneListbox tag Program Structure:

 

2). index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
    <head>
        <title>JSF selectOneListbox</title>
    </head>
    <body>
          <f:view>
              <h:form>
                  <h3><h:outputText value="h:selectOneListbox example" ></h:outputText></h3>
                  <hr>
                  <h3>List Box</h3>
                 <h:selectOneListbox value="#{selectOneListbox.selectedCountry}">
                    <f:selectItem itemValue="INDIA" itemLabel="india" />
                    <f:selectItem itemValue="USA" itemLabel="usa" />
                    <f:selectItem itemValue="ENGLAND" itemLabel="england" />
                    <f:selectItem itemValue="AUSTRALIA" itemLabel="australia" />
                    <f:selectItem itemValue="CANADA" itemLabel="canada" />              
                 </h:selectOneListbox>  
                 <h:commandButton value="Submit" action="result" />                
              </h:form>
          </f:view>
    </body>
</html>

3). SelectOneListbox.java

package com.satyamsoft;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="selectOneListbox", eager=true)
@SessionScoped
public class SelectOneListbox implements Serializable{
   
    private static final long serialVersionUID = 1L;
   
    public String selectedCountry="1";
   
    public String getSelectedCountry() {
        return selectedCountry;
    }
    public void setSelectedCountry(String selectedCountry) {
        this.selectedCountry = selectedCountry;
    }   
}

 

4). result.jsp

  <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
 <html>
    <body>
          <f:view>
              <h:form>
                  <h:panelGrid columns="1">
                       <h:column>
                          <h:dataTable value="#{selectOneListbox.selectedCountry}" var="loc" border="1">
                              <h:column>
                                  <f:facet name="header">
                                      <h:outputText value="Selected Country is:" />
                                </f:facet>
                                <b><h:outputText value="#{loc}"/></b>
                            </h:column>
                          </h:dataTable>
                      </h:column>
                  </h:panelGrid>
              </h:form>
          </f:view>
    </body>
</html>

 

5). OutPut

 

 

No comments:

Post a Comment