Thursday, June 21, 2012

show/hide table in jsp struts

 <logic:equal name="searchPatientFormBean" property="showPatientSearchResult" value="true">  
 <table>  
 ..data..  
 </table>  
 </logic:equal>  
where name="searchPatientFormBean" is the form bean name given in the <action>

property="showPatientSearchResult" is the attribute of form bean whose value is set to false intially.

The table will be displayed only if showPatientSearchResult=true

Customized JSF table

Problem:
To create a data table in JSF in which the first row data spans across the columns ie only one column in the first row and the second row had 8 columns and it should alternate like that. This is not possible with h:dataTable tag

Reason:
The h:dataTable tag renderds data column wise

Solution:
To create such a table with column span use jstl along with plain html and jsf components

Sample Code:
 <c:forEach items="#{HospInvProfBean.productList}" var="product" varStatus="status"> /> <tr><td colspan="8" class="genText">  
  <h:outputText value="#{product.productDescription}"/></td> </tr>  
  <tr class="dtlLineCen">  
  <td style="width: 10%;">  
 <span class="genText">Minimum</span></td>  
 <td><h:inputText size="4" value="#{product.oposMin}" converter="javax.faces.Long" converterMessage="Not a number"/></td>  
  <td><h:inputText size="4" value="#{product.aposMin}" converter="javax.faces.Long" converterMessage="Not a number" /></td>  
 <td><h:inputText size="4" value="#{product.bposMin}" required="true" /></td>              
  <td><h:inputText size="4" value="#{product.abposMin}" required="true" /></td>     
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.onegMin}" required="true" /></td>              
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.anegMin}" required="true" /></td>              
 <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.bnegMin}" required="true" /></td>             
 <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.abnegMin}" required="true" /></td></tr>             
 <tr class="dtlLineAltCen">  
 <td style="width: 10%;"><span class="genText">Optimal</span></td>            <td><h:inputText size="4" value="#{product.oposOpt}" required="true" /></td>  
  <td><h:inputText size="4" value="#{product.aposOpt}" required="true" /></td>  
  <td><h:inputText size="4" value="#{product.bposOpt}" required="true" /></td>  
 <td><h:inputText size="4" value="#{product.abposOpt}" required="true" /></td>  
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.onegOpt}" required="true"/></td>  
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.anegOpt}" required="true"/></td>  
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.bnegOpt}" required="true"/></td>  
  <td><h:inputText size="4" rendered="#{product.aboGroup}" value="#{product.abnegOpt}" required="true"/></td>  
 </c:forEach>  

Custom Converter Spring 2.5 / Spring webflow


1. Dispatcher Servlet configuration

 <bean id="viewResolver"  
           class="org.springframework.web.servlet.view.ResourceBundleViewResolver"  
           p:basename="views" />  
 <bean id="tilesConfigurer"  
           class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"  
           p:definitions="/WEB-INF/tiles-defs.xml" />  
 <flow:flow-builder-services id="flowBuilderServices"  
           view-factory-creator="viewFactoryCreator" conversion-service="conversionService" />  

2. Write Convert Class

java.util.Calendar to String Custom Converter / String to java.util.Calendar Custom Converter

 import java.text.DateFormat;  
 import java.text.ParseException;  
 import java.text.SimpleDateFormat;  
 import java.util.Calendar;  
 import org.springframework.binding.convert.converters.TwoWayConverter;  
 public class CalendarStringTwoWayConverter implements TwoWayConverter {  
      @Override  
      public Class getSourceClass() {  
           return Calendar.class;  
      }  
      @Override  
      public Class getTargetClass() {  
           return String.class;  
      }  
      @Override  
      public Object convertSourceToTargetClass(Object source, Class targetClass)  
                throws Exception {  
           String date = null;  
           DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");  
           if(null!=source){  
                date= dateFormat.format(((Calendar) source).getTime()).toString();  
           }  
           return date;  
      }  
      @Override  
      public Object convertTargetToSourceClass(Object target, Class sourceClass)  
                throws Exception {  
            Calendar cal = Calendar.getInstance();  
         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");  
         try {  
                 cal.setTime(sdf.parse((String) target));  
            } catch (ParseException e) {  
                 e.printStackTrace();  
            }  
           return cal;  
      }  
 }  

3. Register Converter

 import org.springframework.binding.convert.service.DefaultConversionService;  
 import org.springframework.stereotype.Component;  
 @Component("conversionService")  
 public class ApplicationConversionService extends DefaultConversionService {  
      @Override  
      protected void addDefaultConverters() {  
           super.addDefaultConverters();  
           addDefaultAliases();  
           addConverter("calenderStringTwoWayConverter", new CalendarStringTwoWayConverter());  
           addConverter("enumStringTwoWayConverter", new EnumStringTwoWayConverter());  
           addConverter("timeStringTwoWayConverter", new TimeStringTwoWayConverter());  
      }  
 }  

Use in flow registry : Bind  properties with converter
 <view-state id="review" view="review" model="reviewModel">  
      <binder>  
           <binding property="state" converter="enumStringTwoWayConverter"/>            
           <binding property="time" converter="timeStringTwoWayConverter"/>             
           <binding property="date" converter="calenderStringTwoWayConverter"/>   
      </binder>  
 </view-state>  

This way of using named converters has been discontinued from Spring 3.0. In Spring3.0 the converters are handled in a different way.

4. Use in JSP

There is nothing special in jsp.

 <form:form name="myform" modelAttribute="reviewModel">  
      <form:input type="text" id="date" path="date" value="${date}"/>  
 </form:form>  

Single selection checkbox javascript


Javascript:

 function singleSelectCheckbox(checkbox) {  
       var cbs = document.getElementsByTagName('input');  
       for(var i=0; i < cbs.length; i++) {  
             if(cbs[i].type == 'checkbox') {  
                   if (cbs[i].id == checkbox.id) {    
             continue;  
                   }  
                   else{  
                        cbs[i].checked = false;   
                        }  
                   }  
            }  
        }  
In html page, give different ids to each checkboxes

Followers