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

Wednesday, May 16, 2012

Spring Webflow Popup example in JSP

Spring version: Spring webflow 2.0.3 , Spring MVC 3.0.5

1. In the parent jsp page add the following java script

 <script type="text/javascript" src="<c:url value="/resources/dojo/dojo.js" />"> </script>  
 <script type="text/javascript" src="<c:url value="/resources/spring/Spring.js" />"> </script>  
 <script type="text/javascript" src="<c:url value="/resources/spring/Spring-Dojo.js" />"> </script>  
 <link type="text/css" rel="stylesheet" href="<c:url value="/resources/dijit/themes/tundra/tundra.css" />" />  
 <script type="text/javascript">  
      Spring.addDecoration(new Spring.AjaxEventDecoration({                           
                                    elementId: 'popupbutton',  
                                    formId: 'myform',  
                                    event: 'onclick',  
                                    popup: true  
            }));            
 </script>  
                                                           Listing: 1

Replace the following attributes with your values :-

elementId : is the Id of the button /link which opens the pop up
formId : is the Id of the form where the pop up button belongs to

Sample Code from page:

 <form:form id="myform">  
 <input type="button" name="_eventId_review" id="popupbutton" value="Open Popup"  
  onclick="openPopupPage()" />  
 </form:form>  
 function openPopupPage() {  
    window.location.href = '${flowExecutionUrl}&_eventId=review';  
 }  
                                                             Listing: 2

Make sure to have org.springframework.js-2.0.3.RELEASE jar. This jar contains the Spring.js, Dojo.js ,Spring-Dojo.js  and the css referenced in the script tag

2. Modify Web.xml

In the above jsp code (Listing:1)you can see that we are referring to javascripts (dojo.js , Spring.js etc ) Those requests to the java scripts are handled by Resource Servlet in spring. So add the following servlet entry in web.xml to map the requests to java scripts to the resource servlet.

  <servlet>  
   <servlet-name>Resource Servlet</servlet-name>  
   <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>  
 </servlet>  
 <servlet-mapping>  
   <servlet-name>Resource Servlet</servlet-name>  
   <url-pattern>/resources/*</url-pattern>  
 </servlet-mapping>  
                                                               Listing: 3

3. Modify flowregistry.xml

Add popup="true" in the view-state of the popup page
 <view-state id="review" view="review" model="reviewModel" popup="true">  
 </view-state>  
Listing: 4

The pop up page is a normal jsp page. There is no special code to be put in the pop up page to open it as a pop up. The above steps I took from my working code.

Note: In IE8 and IE9 , it will not open as a pop-up , if we try to access the page using http://localhost:8080. You have to replace localhost with IP address or computer-name. This is because IE8 and IE9 handles localhost separately.

Hope this helps.

Followers