Tuesday, 29 November 2011

add remove() function to array of javascript

Array.prototype.indexOf = function(e){
    for (var i = 0; i < this.length; i++) {
        if (e==this[i]) {
            return i;
        }
    }

  return -1;
}

Array.prototype.remove = function(elem) {
    var match = -1;
    while ((match = this.indexOf(elem)) > -1) {
        this.splice(match, 1);
    }
};

How to create an unique array using javascript

Array.prototype.indexOf = function(e){
    for (var i = 0; i < this.length; i++) {
        if (e==this[i]) {
            return i;
        }
    }

  return -1;
}

Array.prototype.unique = function () {
    var values = this;
    var uniqueValues = [];
    for (var i = values.length; i--;) {
        var val = values[i];
        if (uniqueValues.indexOf(val) === -1) {
            uniqueValues.unshift(val);
        }
    }
    return uniqueValues;
};

How to reset the start value of Oracle Sequence?

Alter Sequence SEQ_TX_ID Increment By 1000;
Select SEQ_TX_ID.NextVal From Dual;
Alter Sequence SEQ_TX_ID Increment By 1;

Spring MVC error page

It’s always recommended to display a custom friendly error page instead of the default long java plain exception code in tomcat.So what do you do in Spring MVC Web application? 1) Declare SimpleMappingExceptionResolver in Spring’s bean configuration file.
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>
2) Define "error.jsp"
<html>
<head>
    <title>Error</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/styles/smoothness/jquery-ui-1.8.12.custom.css" type="text/css"/>
    <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery-1.5.1.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/scripts/jquery-ui-1.8.12.custom.min.js"></script>
</head>
<body>
    <div>
        <div class="ui-widget">
            <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
                <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
                    <strong>Alert:</strong> An error has occurred. Please contact our administrator for details.</p>
                <p><strong>Cause:</strong></p> ${exception.message}
            </div>
        </div>
    </div>
</body>
</html>
3) Exmaple

How to validate Date format in Javascript?

Validate the input Date text is very common in web application. My solution is combine Regular Express and Date object. Using regular express to validate the text under the special pattern(yyyy-MM-dd etc.). Using Date object to validate the year,month, day is reasonable.

function dateValidator(text) {
    var pattern = /^([1-9]\d{3})-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])$/;
    if (pattern.test($.trim(text))) {
        var datePart = text.split('-');
        var year = parseInt(datePart[0], 10);
        var month = parseInt(datePart[1], 10) - 1;
        var day = parseInt(datePart[2], 10);
        var date = new Date(year, month, day, 0, 0, 0);

        var y = date.getFullYear();
        var m = date.getMonth();
        var d = date.getDate();

        if ( year == y && month== m && day == d) {
           return true;
        }
    }
    return false;
}

Friday, 25 November 2011

how to customize icon for JQuery-UI buttons

Step1: define css



Step2:





Sunday, 13 November 2011

How to avoid web page duplicated submit?

Solution 1) PRG (Post/Redirect/Get) Pattern
http://en.wikipedia.org/wiki/Post/Redirect/Get

Solution 2) Synchronizer Token Pattern (From book "java/j2ee interview")

The basic idea of this pattern is to set a use once only token in a “session”, when a form is requested and the
token is stored in the form as a hidden field. When you submit the form the token in the request (i.e. due to hidden field) is compared with the token in the session. If tokens match, then reset the token in the session to null or increment it to a different value and proceed with the model & database update. If you inadvertently resubmit the form by clicking the refresh button, the request processing servlet (i.e. PurchaseServlet) first tests for the presence of a valid token in the request parameter by comparing it with the one stored in the session. Since the token was reset in the first submit, the token in the request (i.e 123) would not match with the token in the session (i.e. null or 124). Since the tokens do not match, an alternate course of action is taken like forwarding to an error.jsp page.