Tuesday, October 2, 2012

custom converter for BigDecimal struts2

if you are trying to map a BigDecimal field in jsp to the Action, you may get exception,
to solve this exception you need a converter to convert it.

1-you need to define file by the name Action_Name-conversion.properties


attrb1=com.a.b.BigDecimalConverter
attrb2=com.a.b.BigDecimalConverter

2-add the BigDecimalConverter in the package you want


public class BigDecimalConverter extends StrutsTypeConverter{

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
if(values!=null && values.length>0){
String s=values[0];
if(s!=null && s.trim().length()>0){
BigDecimal bd = new BigDecimal(s);
return bd.setScale(2, BigDecimal.ROUND_HALF_UP);
}
}
return null;
}

@Override
public String convertToString(Map context, Object o) {
return o.toString();

}





Wednesday, September 19, 2012

show/hide html component using java script


HTML Code: 

<TD>
<s:select id="dateTargetWriteOffComparitor" 
onchange="showHide('dateTargetWriteOffComparitor','toDateOfTarget')"
value="searchDateCriteriaOperators[0]"
list="searchDateCriteriaOperators"
name="dateTargetWriteOffComparitor" />
</TD>

<TD height="23" width="91"> 
    <sj:datepicker name="dateOfTarget" displayFormat="mm-dd-yy" style="width: 80px;" />

<div id="toDateOfTarget" style="display: none;">
   <sj:datepicker name="toDateOfTarget" displayFormat="mm-dd-yy" style="width: 80px; " />
</div>


Java Script Code: 


function showHide(htmlComp,target){
var listValue=document.getElementById(htmlComp).value;  
if(listValue=='Between'){
document.getElementById(target).style.display = 'block';
}else{
document.getElementById(target).style.display = 'none';
}
}

Friday, August 24, 2012

Clear Form with jQuery

<html>

<head>
<script language="JavaScript" type="text/JavaScript" src="Script/jquery-1.8.0.min.js"></script>
</head>

<body>

<form id="frm">
          <input type="button"  value="Clear" onclick="resetForm('frm')" />
</form>

<script>
/**
* This function clears all input fields value except button, submit, reset, hidden fields
* */
function resetForm(formid) {
                  $(':input','#'+formid) .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked').removeAttr('selected');
 
}

</script>

</body>
</html>

Thursday, August 16, 2012

scrollable div


To make the div scroll-able in vertical direction:  


<div  style="height: 300px; overflow-y: scroll">

</div>

How to view List inside list using

if you have a list inside another list, for example you want to iterate over students list and students courses 
and you want to view it in the jsp page, you can use the <s:iterator>  as follows: 

<s:iterator value="studentsList" >

        <s:property value="studentName"/>

       <s:iterator value="%{courses}" >

                      <s:property value="courseName"/>

       </s:iterator> 

</s:iterator> 


This example read the studentsList from struts action. 

Friday, August 10, 2012

How to get fields from different tables in hibernate ?



StringWriter queryS = new StringWriter(200);
Query query;

queryS.append("SELECT A.dateBilling,B.currencyCode " );
queryS.append(" from AImpl A,BImpl B ");
queryS.append("where B.id = A.id  " );


query = this.getSession().createQuery(queryS.toString());
//The values in this array are:
//dateBilling     = results[0]
//currencyCode = results[1]
Object[] results = (Object[]) query.list().get(0);

return results;

Friday, August 3, 2012

Toggles the visibility of a div by clicking on an expand/collapse image


/**
 * Toggles the visibility of a div by clicking on an expand/collapse image.
 * The obj parameter is the image link and the "id" is the id of the div to
 * toggle.
 * @param obj
 * @param id
 */
function toggle(obj,id) {
    var state = document.getElementById(id).style.display;
        if (state == 'block') {
            document.getElementById(id).style.display = 'none';
            obj.src = 'Images/Expand.png';
            obj.title = 'Click to Expand';
        } else {
            document.getElementById(id).style.display = 'block';
            obj.src = 'Images/Collapse.png';
            obj.title = 'Click to Collapse';
        }
}


// In JSP file add the following:


<img id="invoiceListImg" src="Images/Collapse.png" alt="Collapse"  title="Click to Collapse"
onclick="toggle(this,'divName');"/>

Friday, July 27, 2012

s:checkbox design make a problem in the table design

If you have a table and you notice that the <s:checkbox> change the expected design, the solution is simple, add the following property to the tag, and it will be in line with other fields in the table:

<s:checkbox name="persistentBeanKey" theme="simple"
fieldValue="%{#attr.row.sequenceNumberN15}" id="persistentBeanKey"/>

collapse div


This is the javascript part:

<script language="JavaScript" type="text/JavaScript">

function collapse(collapsId, expandId) {
document.getElementById(collapsId).style.display = "none";
document.getElementById(expandId).style.display = "block";
}
function expand(collapsId, expandId) {
document.getElementById(collapsId).style.display = "block";
document.getElementById(expandId).style.display = "none";
}


</script>


------------------------------


jsp/html page :



<div id="divE" style="display: none;">
 <s:a href="javaScript:expand(' divA',' divE')">
 <img id="iviFormCoImg" src="Images/Expand.png" alt="Expand" />Expand</s:a>
</div>

<div  class="pageContainer" id="divA"  style="position: relative;">


<div id="divC" style="display: block;">
<a href="javaScript:collapse(' divA ',' divE')">
        <img src="Images/Collapse.png" alt="Collapse" />Collapse</a>
</div>

</div>


Thursday, July 26, 2012

How to use the submit ajax tag with Struts2



first you have to include the ajax tag library 

<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>

second, define the submit ajax component
<sj:submit  targets="yourTargetDiv/Form"   title="Submit"
effect="pulsate" effectOptions="{ color : '#222222' }" effectDuration="500" />

Third, add the action to the form that contains this <sj:submit>

-----
after you click the <sj:submit> , This test.jsp will be viewed in the target defined in the <sj:submit>:

<action name="actionTest" method="test" class="com.QueryAction">
<result name="success">/jsp/test.jsp</result>
</action>

ajaxify the display:table in struts2


I was in a trouble for 2 days, How I can ajaxify the display table pagination,
I used Struts2 framework, after looking around and using other codes, and integrate it together, I do this, it works.

1: add this javascript code, and edit the ["insert your form name here"] where you find it with the form name you have

<script language="JavaScript" type="text/JavaScript">


    var formToUpdate;
function ajaxifyDisplayTag(targetId) {

stopEvent(event);
if(typeof(event.target.href)!= "undefined"){
formToUpdate=targetId;
var url = event.target.href;
showReport(url);
}

}

//This function will prevent the reloading
//it will kill the event fired by selecting the anchor in the pagination
function stopEvent(e) {

   if (e.stopPropagation) e.stopPropagation();
   else e.cancelBubble = true;

   if (e.preventDefault) e.preventDefault();
   else e.returnValue = false;
}


var xmlHttp;
function showReport(url)/*Passing two param, one is any user input you need, next one is page number.When you call the function in jsp page(calling page), just pass 0 for the second argument.This is how i designed this code.*/
{
xmlHttp = GetXmlHttpObject();
if (xmlHttp==null)
{
 //alert("Your browser does not support AJAX!");
 return;
}

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
var results = xmlHttp.responseText;
if(results!=0)
{
//Displaying results in a DIV tag...You may also use SPAN to display the results.
//alert('Displaying results in a DIV tag');
document.getElementById("insert your form name here").innerHTML=xmlHttp.responseText;

}
else
{
//alert('else Displaying results in a DIV tag');
document.getElementById("insert your form name here").innerHTML="No records in the database";
}
}
}

//Normal code for Browser detection
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
   {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   }
 catch (e)
   {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 }
return xmlHttp;
}

</script>

2:in the jsp page, define this div around display:table tag
<div id="ajaxifyDiv" onclick="ajaxifyDisplayTag(ajaxifyDiv)">


----------------------------


IE make some problems with this code, it works well with chrome, to solve the problem , replace the following code:

if (typeof event.srcElement === 'undefined') {
   target = event.target;
   } else {
   target =event.srcElement;
}
var nodeName=  target.nodeName;


function stopEvent(e) {
//alert("Yes Yes stopEvent");
   if (e.stopPropagation) e.stopPropagation();
   else e.cancelBubble = true;

   if (e.preventDefault) e.preventDefault();
   else e.returnValue = false;
}



Friday, June 29, 2012



This Blog is simple, it is not professional, I will try to document the bugs, and exceptions that may I find in some projects and how I solve it!!!
I believe that this blog may help me to remember some solutions I forget or share some solutions I have with others.

Let’s start bugs fighting...