Thursday 27 December 2012

JSF SelectManyCheckBox example

1) JSF selectManyCheckbox example 

  

 

 2) index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index.html</title>
    <meta http-equiv="refresh" content="0; URL=index.jsf"/>
  </head>
</html>




3) 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>Select ManyCheckbox</title>
    </head>
    <body>
          <f:view>
              <h:form id="select">
                  <h:panelGrid columns="1">
                       <h:column>
                          <h:outputText value="Select Cities: "/>
                          <h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
                              <f:selectItem itemLabel="Hyderabad" itemValue="Hyderabad" />
                              <f:selectItem itemLabel="Vijayawada" itemValue="Vijayawada" />
                              <f:selectItem itemLabel="Vizag" itemValue="Vizag" />
                              <f:selectItem itemLabel="Guntur" itemValue="Guntur" />
                          </h:selectManyCheckbox>
                      </h:column>
                      <h:column>
                          <h:commandButton value="Submit" action="#{selectManyCheckBoxBean.submit}"/>
                      </h:column>
                  </h:panelGrid>
                  <br/>
                   <h:panelGrid columns="1">
                       <h:column>
                        <!-- <h:outputText value="Selected Cities: "/>  -->
                          <h:dataTable value="#{selectManyCheckBoxBean.selectedItems}" var="loc" border="1">
                              <h:column>
                                  <f:facet name="header">
                                      <h:outputText value="Selected Cities" />
                                </f:facet>
                                <h:outputText value="#{loc}"/>
                            </h:column>
                          </h:dataTable>
                      </h:column>
                  </h:panelGrid>
              </h:form>
          </f:view>
    </body>
</html>


4). web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

   

-->

5). faces-config.xml

 

<?xml version='1.0' encoding='UTF-8'?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
    version="1.2">
    <managed-bean>
        <managed-bean-name>selectManyCheckBoxBean</managed-bean-name>
        <managed-bean-class>com.satyamsoft.SelectManyCheckBoxBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <from-view-id>/index.jsp</from-view-id>
        <navigation-case>
            <from-outcome>selectManyCheckBox</from-outcome>
            <to-view-id>/index.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>  

 

 

6).SelectManyCheckBoxBean.java

 

 

package com.satyamsoft;

import java.util.List;

public class SelectManyCheckBoxBean {

    private List<String> selectedItems;
   
    public SelectManyCheckBoxBean() {
    }

    public List<String> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(List<String> selectedItems) {
        this.selectedItems = selectedItems;
    }
    public String submit(){
        return "satyam";
    }
}  

 

7).OutPut: