Additional Code JSF Zip file - 13 KB

Additional Code Struts Zip file - 22 KB

Listing 1: Struts Bean

package com.kowaldesign.example;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

/**
 * Selection List Example.
 */
public class ExampleForm extends ActionForm
{
   /** Serialization Version Unique Identifier */
   private static final long serialVersionUID = 0L;

   /** @serial List of search types */
   private List forList = new ArrayList();

   /** @serial Selected search type */
   private String forValue = "";

   /** @serial List of search areas */
   private List withinList = new ArrayList();

   /** @serial Selected search areas */
   private String[] withinValues = new String[ 0 ];

   /** @serial List of update periods */
   private List updateList = new ArrayList();

   /** @serial Selected update period */
   private String updateValue = "";

   /** @serial List of languages */
   private List languageList = new ArrayList();

   /** @serial Selected languages */
   private String[] languageValues = new String[ 0 ];

   /**
    * Constructor.
    */
   public ExampleForm()
   {  ResourceBundle r = ResourceBundle.getBundle(
         "com.kowaldesign.example.example" );
      forList.add( new LabelValueBean( 
         r.getString( "allWords"    ), "ALL" ));
      forList.add( new LabelValueBean( 
         r.getString( "anyWords"    ), "ANY" ));
      forList.add( new LabelValueBean( 
         r.getString( "exactPhrase" ), "EXA" ));
      withinList.add( new LabelValueBean( 
         r.getString( "pageTitle" ), "TIT" ));
      withinList.add( new LabelValueBean( 
         r.getString( "pageText"  ), "TEX" ));
      withinList.add( new LabelValueBean( 
         r.getString( "url"       ), "URL" ));
      updateList.add( new LabelValueBean( 
         r.getString( "oneWeek"     ), "1WK" ));
      updateList.add( new LabelValueBean( 
         r.getString( "oneMonth"    ), "1MO" ));
      updateList.add( new LabelValueBean( 
         r.getString( "threeMonths" ), "3MO" ));
      updateList.add( new LabelValueBean( 
         r.getString( "sixMonths"   ), "6MO" ));
      updateList.add( new LabelValueBean( 
         r.getString( "oneYear"     ), "1YR" ));
      updateList.add( new LabelValueBean( 
         r.getString( "threeYears"  ), "3YR" ));
      updateList.add( new LabelValueBean( 
         r.getString( "anyTime"     ), "ANY" ));
      languageList.add( new LabelValueBean( 
         r.getString( "chinese"  ), "CH" ));
      languageList.add( new LabelValueBean( 
         r.getString( "english"  ), "EN" ));
      languageList.add( new LabelValueBean( 
         r.getString( "french"   ), "FR" ));
      languageList.add( new LabelValueBean( 
         r.getString( "german"   ), "GR" ));
      languageList.add( new LabelValueBean( 
         r.getString( "japanese" ), "JA" ));
      languageList.add( new LabelValueBean( 
         r.getString( "korean"   ), "KO" ));
      languageList.add( new LabelValueBean( 
         r.getString( "russian"  ), "RU" ));
      languageList.add( new LabelValueBean( 
         r.getString( "spanish"  ), "SP" ));
   }
   ...

   /**
    * Reset all selected values.
    * <p>
    * @param  mapping  action mapping
    * @param  request  HTTP servlet request
    */
   public void reset( ActionMapping mapping, 
      HttpServletRequest request )
   {  setForValue( "ALL" );
      setWithinValues( new String[ 0 ]);
      setUpdateValue( "6MO" );
      setLanguageValues( new String[ 0 ]);
   }
}


Listing 2: JavaServer Faces Bean

package com.kowaldesign.example;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * Selection List Example.
 */
public class ExampleForm extends Object
{
   /** Serialization Version Unique Identifier */
   private static final long serialVersionUID = 0L;

   /** @serial List of search types */
   private List forList = new ArrayList();
   
   /** @serial Selected search type */
   private String forValue = "";
   
   /** @serial List of search areas */
   private List withinList = new ArrayList();

   /** @serial Selected search areas */
   private String[] withinValues = new String[ 0 ];
   
   /** @serial List of update periods */
   private List updateList = new ArrayList();
   
   /** @serial Selected update period */
   private String updateValue = "";
   
   /** @serial List of languages */
   private List languageList = new ArrayList();
   
   /** @serial Selected languages */
   private String[] languageValues = new String[ 0 ];
   
   /**
    * Constructor.
    */
   public ExampleForm()
   {  ResourceBundle r = ResourceBundle.getBundle( 
         "com.kowaldesign.example.example" );
      forList.add( new SelectItem( 
         "ALL", r.getString( "allWords"    ), "" ));
      forList.add( new SelectItem( 
         "ANY", r.getString( "anyWords"    ), "" ));
      forList.add( new SelectItem( 
         "EXA", r.getString( "exactPhrase" ), "" ));
      withinList.add( new SelectItem( 
         "TIT", r.getString( "pageTitle" ), "" ));
      withinList.add( new SelectItem( 
         "TEX", r.getString( "pageText"  ), "" ));
      withinList.add( new SelectItem( 
         "URL", r.getString( "url"       ), "" ));
      updateList.add( new SelectItem( 
         "1WK", r.getString( "oneWeek"     ), "" ));
      updateList.add( new SelectItem( 
         "1MO", r.getString( "oneMonth"    ), "" ));
      updateList.add( new SelectItem( 
         "3MO", r.getString( "threeMonths" ), "" ));
      updateList.add( new SelectItem( 
         "6MO", r.getString( "sixMonths"   ), "" ));
      updateList.add( new SelectItem( 
         "1YR", r.getString( "oneYear"     ), "" ));
      updateList.add( new SelectItem( 
         "3YR", r.getString( "threeYears"  ), "" ));
      updateList.add( new SelectItem( 
         "ANY", r.getString( "anyTime"     ), "" ));
      languageList.add( new SelectItem( 
         "CH", r.getString( "chinese"  ), "" ));
      languageList.add( new SelectItem( 
         "EN", r.getString( "english"  ), "" ));
      languageList.add( new SelectItem( 
         "FR", r.getString( "french"   ), "" ));
      languageList.add( new SelectItem( 
         "GR", r.getString( "german"   ), "" ));
      languageList.add( new SelectItem( 
         "JA", r.getString( "japanese" ), "" ));
      languageList.add( new SelectItem( 
         "KO", r.getString( "korean"   ), "" ));
      languageList.add( new SelectItem( 
         "RU", r.getString( "russian"  ), "" ));
      languageList.add( new SelectItem( 
         "SP", r.getString( "spanish"  ), "" ));
      setForValue( "ALL" );
      setWithinValues( new String[ 0 ]);
      setUpdateValue( "6MO" );
      setLanguageValues( new String[ 0 ]);
   }
   ...
}


Listing 3: Struts JSP

	<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>

<HTML>
<HEAD>
   <TITLE>Selection List Examples</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<fmt:setBundle basename="com.kowaldesign.example.example"/>

<html:form action="/exampleWrite.do">
<table border="0" cellpadding="0" cellspacing="10">
   <tr valign="top">
   
      <%-- Pattern 1:  One Selection from Few --%>
      <td align="right">
         <fmt:message key="searchFor"/>
      </td>
      <td>
         <logic:iterate name="exampleForm" property="forList" id="item">
            <html:radio property="forValue" idName="item" value="value">
    <bean:write name="item" property="label"/><br />
            </html:radio>
         </logic:iterate>
      </td>
   
      <%-- Pattern 3:  Multiple Selections from Few --%>
      <td align="right">
         <fmt:message key="searchWithin"/>
      </td>
      <td>
         <logic:iterate name="exampleForm" property="withinList" id="item">
            <html:multibox property="withinValues">
               <bean:write name="item" property="value"/>
            </html:multibox>
            <bean:write name="item" property="label"/><br/>
         </logic:iterate>
      </td>
   </tr>
   
   <%-- Pattern 2:  One Selection from Many --%>
   <tr>
      <td />
      <td align="right">
         <fmt:message key="updated"/>
      </td>
      <td>
         <html:select property="updateValue" size="4">
            <html:optionsCollection property="updateList"/>
         </html:select>
      </td>
      <td />
   </tr>
   <tr valign="top">
   
      <%-- Pattern 4:  Multiple Selections from Many --%>
      <td align="right">
         <fmt:message key="languages"/>
      </td>
      <td>
         <html:select property="languageValues" size="4" multiple="true">
            <html:optionsCollection property="languageList"/>
         </html:select>
      </td>
   
      <%-- Pattern 5:  Multiple Selections from Many --%>
      <td align="right">
         <fmt:message key="languages"/>
      </td>
      <td>
         <div style="border:1px solid #7f9db9;
            overflow:auto; height:70px; width:90px;
            font-family:Lucida Grande; font-size:8pt;">
            <logic:iterate name="exampleForm"
               property="languageList" id="item">
               <html:multibox property="languageValues">
                  <bean:write name="item" property="value"/>
               </html:multibox>
               <bean:write name="item" property="label"/><br />
            </logic:iterate>
         </div>
      </td>
   </tr>
   
   <tr align="middle" valign="top">
      <td colspan="4">
         <input type="button" value="<fmt:message key='submit'/>" />
      </td>
   </tr>
</table>

</html:form>
</BODY>
</HTML>


Listing 4: JavaServer Faces JSP

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<HTML>
<HEAD>
   <TITLE>Selection List Examples</TITLE>
   <link rel="stylesheet" type="text/css" href='<%= 
      request.getContextPath() + "/stylesheet.css" %>'>
</HEAD>
<BODY BGCOLOR="white">
<f:loadBundle 
   basename="com.kowaldesign.example.example" var="bundle"/>

<f:view>
   <h:form id="form">
      <h:panelGrid columns="1" rowClasses="center">
      <h:panelGrid columns="5">
   
         <%-- Pattern 1:  One Selection from Few --%>
         <h:outputText value="#{bundle.searchFor}" />
         <h:selectOneRadio value="#{example.forValue}" layout="pageDirection" id="for">
            <f:selectItems value="#{example.forList}"/>
         </h:selectOneRadio>
         <h:outputText value="" />
   
         <%-- Pattern 3:  Multiple Selections from Few --%>
         <h:outputText value="#{bundle.searchWithin}" />
         <h:selectManyCheckbox value="#{example.withinValues}" 
            layout="pageDirection" id="within">
            <f:selectItems value="#{example.withinList}"/>
         </h:selectManyCheckbox>
      </h:panelGrid>
 
      <%-- Pattern 2:  One Selection from Many --%>
      <h:panelGrid columns="2">
         <h:outputText value="#{bundle.updated}" /> 
         <h:selectOneListbox value="#{example.updateValue}" size="4" id="update">
            <f:selectItems value="#{example.updateList}"/>
         </h:selectOneListbox>
      </h:panelGrid>
      <h:panelGrid columns="5">
         
         <%-- Pattern 4:  Multiple Selections from Many --%>
         <h:outputText value="#{bundle.languages}" />
         <h:selectManyListbox size="4" id="lang4"
            value="#{example.languageValues}">
            <f:selectItems value="#{example.languageList}"/>
         </h:selectManyListbox>
         <h:outputText value="" />
         
         <%-- Pattern 5:  Multiple Selections from Many --%>
         <h:outputText value="#{bundle.languages}" />
         <h:panelGroup>
            <f:verbatim>
               <div style="border:1px solid #7f9db9;
               overflow:auto; height:70px; width:110px;
               font-family:Lucida Grande; font-size:8pt;">
            </f:verbatim>
            <h:selectManyCheckbox value="#{example.languageValues}" 
               layout="pageDirection" id="lang5">
               <f:selectItems value="#{example.languageList}"/>
            </h:selectManyCheckbox>
            <f:verbatim>
               </div>
            </f:verbatim>
         </h:panelGroup>
      </h:panelGrid>

      <h:panelGrid columns="1">
         <h:commandButton action="#{example.submit}" value="#{bundle.submit}" />
      </h:panelGrid>
      </h:panelGrid>
   </h:form>
</f:view>
</BODY>
</HTML>

Additional Code JSF Zip file - 13 KB

Additional Code Struts Zip file - 22 KB