Tuesday, 17 September 2013

Spring web.xml with Listener,Without LIstener

Using Two configuration files with Listener

  • Eventhough we provided the name of applicationContext.xml,It will search for spring-servlet.xml,Here servlet-name is spring

  • ContextLoaderListener,it will get the configuration file(using contextConfigLocation) and create Spring contatiner object when the project is deployed into the server

  • Dispatcher servlet creating container object with another configuration file(xxx-servlet.xml)

  • Here xxx means spring(<servlet-name>spring</servlet-name>)


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>



    Using only one configuration file with Listener

  • Here Listener and DispatcherServlet both using spring-servlet.xml


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>


    Using One Configuration file without Listener

  • We can use init parameter also for giving configuration file name,this init-param is under DispatcherServlet

  • we no need to follow particular name(xxx-servlet.xml),we no need to place that configuration file in WEB-INF folder also

  • but in this way Spring Container object is created when the DispatcherServlet object is created


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Config/Congig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    Saturday, 7 September 2013

    Working with Date class in java,Converting String to Date


    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    //This examples are used to take date in string format and stored in date object
    public class DateExamples {


    public static void main(String[] args) {

    System.out.println("+++++++++++++++++++++++++++++++++");
    //Just displaying today date in required format
    DateFormat df = new SimpleDateFormat("dd/yy/MM");
    String formattedDate = df.format(new Date());
    System.out.println("TO DAY DATE IS:"+formattedDate);
    System.out.println("+++++++++++++++++++++++++++++++++");
    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter = new SimpleDateFormat
    ("dd-MMM-yyyy");
    String dateInString = "7-Jun-2013";

    try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter1 = new SimpleDateFormat
    ("dd/MM/yyyy");
    String dateInString1 = "07/06/2013";

    try {

    Date date = formatter1.parse(dateInString1);
    System.out.println(date);
    System.out.println(formatter1.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter2 = new SimpleDateFormat
    ("MMM dd, yyyy");
    String dateInString2 = "Jun 7, 2013";

    try {

    Date date = formatter2.parse(dateInString2);
    System.out.println(date);
    System.out.println(formatter2.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter3 = new SimpleDateFormat
    ("E, MMM dd yyyy");
    String dateInString3 = "Fri, June 7 2013";

    try {

    Date date = formatter3.parse(dateInString3);
    System.out.println(date);
    System.out.println(formatter3.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");


    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter4 = new SimpleDateFormat
    ("EEEE, MMM dd, yyyy HH:mm:ss a");
    String dateInString4 = "Friday, Jun 7, 2013 12:10:56 PM";

    try {

    Date date = formatter4.parse(dateInString4);
    System.out.println(date);
    System.out.println(formatter4.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the time
    SimpleDateFormat formatter5 = new SimpleDateFormat
    ("HH:mm:ss a");
    String dateInString5 = "12:10:56 PM";

    try {

    Date date = formatter5.parse(dateInString5);
    //System.out.println(date);
    System.out.println(formatter5.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //It just displaying current date and time in required format
    SimpleDateFormat formatter6 = new SimpleDateFormat
    ("yyyy/MM/dd EEE,hh:mm::ss a");
    System.out.println(formatter6.format(new Date()));

    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //It just displaying current date and time in required format
    SimpleDateFormat formatter7 = new SimpleDateFormat
    ("yyyy/MMMM/dd EEE,hh:mm::ss a");
    System.out.println(formatter7.format(new Date()));

    System.out.println("+++++++++++++++++++++++++++++++++");


    }

    }
    OUTPUT:
    +++++++++++++++++++++++++++++++++
    TO DAY DATE IS:07/13/09
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    07-Jun-2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    07/06/2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    Jun 07, 2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    Fri, Jun 07 2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 12:10:56 IST 2013
    Friday, Jun 07, 2013 12:10:56 PM
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    12:10:56 PM
    ++++++++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++++++++
    2013/09/07 Sat,03:33::32 PM
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    2013/September/07 Sat,03:33::32 PM
    +++++++++++++++++++++++++++++++++

    Spring MVC working Flow

  • By using URL/From jsp we are sending request

  • This request will come to web.xml file and execute DispatcherServlet

  • It is the responsibility of DispatcherServlet to identify perticular HandlerMappings

  • By using HandlerMapping DispathcerServlet will search for requested url path matching in spring configuration file/Spring Configuration class

  • If it is available,DispatcherServlet will call methods in our Controller

  • Controller process the request,get the data form form,store into pojo class/javabean,if required perform validations

  • And then Controller returns ModelAndView object with data/errors and jsp name

  • DispatcherServlet take the ModleAndView object and stored in scoped variables

  • Based on the jsp name returned by Controller,DispatcherServlet will find out jsp by using ViewResolver(InternalResourceViewResolver)

  • Finally loginpage/successpage/failure page send to clietnt/jsp/other

  • By using scoped variables we can display the data/errors

  • Wednesday, 14 August 2013

    JQuery Dropdown List Example

    JQuery Dropdown List Example
    <html>
    <head>
    <script src="jquery.js"></script>
    <script>

    $(document).ready(function() {
    //To clear the selected item from the list
       $("#bt").click(function() {
         $("#selId option").attr("selected",false);
       });

    //To get the value of selected item
       $("#bt1").click(function() {
         var temp=$("#selId option:selected").val();
         alert(temp);
       });

    //To get the text of selected item
       $("#bt2").click(function() {
         var temp=$("#selId option:selected").text();
         alert(temp);
       });

    //To get the index of selected item
       $("#bt3").click(function() {
         var temp=$("#selId option:selected").index();
         alert(temp);
       });

    });

    </script>
    </head>
    <body>
    <select id="selId">
    <option value="">---select---</option>
    <option value="java">JAVA</option>
    <option value="c">C</option>
    <option value="c++">C++</option>
    <option value="oracle">ORACLE</option>
    </select>
    <button id="bt">clear</button>
    <button id="bt1">selectedValue</button>
    <button id="bt2">selectedText</button>
    <button id="bt3">selectedIndex</button>
    </body>
    </html>

    Tuesday, 13 August 2013

    JQuery Check box Example



    JQuery Checkbox Example
    <html>
    <head>
    <script src="jquery.js"></script>
    <script>
    $(document).ready(function() {

    $("#selectedValue").click(function() {
       var values=[];
       $("input:checkbox[name=lang]:checked").each(function() {
          values.push($(this).val());
       });
    for(i=0;i<values.length;i++) {
       alert(values[i]);
    }
    });
    $("#selectedText").click(function() {
       var values=[];
       $("input:checkbox[name=lang]:checked").each(function() {
          values.push($(this).prev().text());
       });
    for(i=0;i<values.length;i++) {
       alert(values[i]);
    }
    });
    $("#selectedIndex").click(function() {
       var values=[];
       $("input:checkbox[name=lang]:checked").each(function() {
          values.push($(this).index());
       });
    for(i=0;i<values.length;i++) {
        alert(values[i]);
    }
    });
    });
    </script>
    </head>
    <body>
    <div>
    <label for="telugu">Telugu</label>
    <input type="checkbox" name="lang" value="telugu"/>
    <label for="english">English</label>
    <input type="checkbox" name="lang" value="english"/>
    <label for="hindi">Hindi</label>
    <input type="checkbox" name="lang" value="hindi"/>
    </div>
    <button id="selectedValue">Selected Value</button>
    <button id="selectedText">Selected text</button>
    <button id="selectedIndex">Selected index</button>
    </body>
    </html>



    Tuesday, 30 July 2013

    JQuery Radio Button

    //To uncheck by default checked radio button

    $("input:radio[name='radioButtonName']").attr("checked",false);


    //To check particular radio button with starting index 0 or 1 or 2

    $("input:radio[name='radioButtonName']")[0].checked = true;


    //To get the value of selected radio button

    $("input:radio[name='radioButtonName']:checked").val();


    //To get the text(display text in radio button label) of selected radio button

    $("input:radio[name='radioButtonName']:checked").parent().text();


    //To get the index of selected radio buttons

    var temp = $("input:radio[name='radioButtonName']");


    var selectedIndex = temp.index(temp.filter(":checked"));





    EXAMPLE PROGRAM
    <html>
    <head>
    <style>

    .addSelColor {
    color: red;
    }

    .MyCssClass {
    background-color: green;
    cursor: pointer;
    border: 2px solid yellow;
    }

    </style>

    <script src="jquery.js"></script>
    <script>
    $(document).ready(function() {

      alert("Jquery is Loaded");

    $("input:radio[name=sex]").change(function() {
      alert($(this).val());
      $(".radioClass").parent().removeClass("addSelColor"); //for css
      $(".radioClass:checked").parent().addClass("addSelColor"); //for css
    });


    $("#ClearButton").click(function() {
      $("input:radio[name=sex]").attr("checked",false);
    });


    $("#selectMale").click(function() {
      $("input:radio[name=sex]")[0].checked = true;
      $(".radioClass").parent().removeClass('addSelColor'); //for css
      $(".radioClass:checked").parent().addClass('addSelColor'); //for css
    });


    $("#selectFemale").click(function() {
      $("input:radio[name=sex]")[1].checked = true;

      $(".radioClass").parent().removeClass("addSelColor"); //for css
      $(".radioClass:checked").parent().addClass("addSelColor"); //for css
    });


    $("#hideButton").click(function() {
      $("#radioDiv").hide();
    });


    $("#showButton").click(function() {
      $("#radioDiv").show();
    });


    $("#getVal").click(function() {
      var temp=$("input:radio[name=sex]:checked").val();
      alert(temp);
    });


    $("#getText").click(function() {
      var temp=$("input:radio[name=sex]:checked").parent().text();
      alert(temp);
    });


    $("#getIndex").click(function() {
      var temp = $("input:radio[name=sex]");
      var selectedIndex = temp.index(temp.filter(":checked"));
      alert(selectedIndex);
    });


    $("#acss").click(function() {
      $("label[for=male]").addClass("MyCssClass");
      $("label[for=female]").addClass("MyCssClass");
    });


    $("#rcss").click(function() {
      $("label[for=male]").removeClass("MyCssClass");
      $("label[for=female]").removeClass("MyCssClass");
    });


    });
    </script>
    </head>
    <body>
    <div id="radioDiv">

    <label for="male">
    <input type="radio" class="radioClass" name="sex" value="BOY" checked>MALE<input>
    </label>  
    <label for="female">
    <input type="radio" class="radioClass" name="sex" value="GIRL">FEMALE</input>
    </label>

    </div></br>

      <button id="ClearButton">Reset</button></br>
      <button id="selectMale">Male</button></br>
      <button id="selectFemale">FeMale</button> </br>
      <button id="hideButton">Hide Radio Buttons</button></br>
      <button id="showButton">Show Radio Buttons</button></br>
      <button id="getVal">Get Selected Value</button></br>
      <button id="getText">Get Selected Text</button></br>
      <button id="getIndex">Get Selected Index</button></br>
      <button id="acss">ApplyCss</button></br>
      <button id="rcss">RemoveCss</button></br>

    </body>
    </html>

    Saturday, 13 July 2013

    Different ways to work with jQuery...

    ->First we have to download/copy JQuery code(In this blog already link is available).
    ->Place that code in one text file and save with the extension .js(Ex->jquery.js)
    ->create one html file and give this jquery.js file path in script tag(<script src="jquery.js"></script>)
    ->write jquery code in the script tag
    ->We can use following ways to work with JQuery
       
    $(document).ready(function(){

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    jQuery(document).ready(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    window.jQuery(document).ready(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    window.$(document).ready(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    $(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    jQuery(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    //We can use any name instead of $
    var ram=$.noConflict();
    ram(document).ready(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    var ram=$.noConflict();
    ram(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    var ram=$.noConflict();
    window.ram(document).ready(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });
    var ram=$.noConflict();
    window.ram(function() {

             alert("WELCOME TO JQUERY");
             //write JQuery code here

    });

    EXAMPLE PROGRAM
    <html>
    <head>
    <script src="jquery.js">
    </script>
    <script>
        $(document).ready(function() {
             alert("AFTER LOAD THE PAGE THIS JQUERY CODE IS EXECUTING");
             //WRITE ANY JQUERY CODE
        });
    </script>
    </head>
    <body>
    HOW TO WORK WITH JQUERY
    </body>
    </html>