JSF SelectManyCheckBox Tag example
<h:selectManyCheckbox> tag in JSF is used for selecting the list of CheckBoxes
1) JSF selectManyCheckbox example structure.
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>
<body>
<f:view>
<h:form id="select">
<h:panelGrid columns="1">
<h:column>
<b><h:outputText value="Select Fruits: "/></b>
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="Apple" itemValue="Apple"/>
<f:selectItem itemLabel="Banana" itemValue="Banana" />
<f:selectItem itemLabel="Orange" itemValue="Orange" />
<f:selectItem itemLabel="Mango" itemValue="Mango" />
</h:selectManyCheckbox>
</h:column>
<h:column>
<h:commandButton value="Submit" action="#{selectManyCheckBoxBean.submit}"/>
</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>1</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>/selectManyCheckBoxResult.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(){
System.out.println("List : " + this.selectedItems);
return "selectManyCheckBox";
}
}
7). selectManyCheckBoxResult.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 id="select">
<h:panelGrid columns="1">
<h:column>
<h:dataTable value="#{selectManyCheckBoxBean.selectedItems}" var="loc" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Selected Fruits" />
</f:facet>
<h:outputText value="#{loc}"/>
</h:column>
</h:dataTable>
</h:column>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
8).OutPut:
No comments:
Post a Comment