Sunday, October 28, 2018

Spring Exception Handling:
--------------------------------------------------------------------------------------------------------------------------

Using HTTP Status Codes

Normally any unhandled exception thrown when processing a web-request causes the server to return an HTTP 500 response. However, any exception that you write yourself can be annotated with the @ResponseStatus annotation (which supports all the HTTP status codes defined by the HTTP specification). When an annotated exception is thrown from a controller method, and not handled elsewhere, it will automatically cause the appropriate HTTP response to be returned with the specified status-code.
For example, here is an exception for a missing order.
 @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Order")  // 404
 public class OrderNotFoundException extends RuntimeException {
     // ...
 }
And here is a controller method using it:
 @RequestMapping(value="/orders/{id}", method=GET)
 public String showOrder(@PathVariable("id") long id, Model model) {
     Order order = orderRepository.findOrderById(id);

     if (order == null) throw new OrderNotFoundException(id);

     model.addAttribute(order);
     return "orderDetail";
 }
A familiar HTTP 404 response will be returned if the URL handled by this method includes an unknown order id.

Controller Based Exception Handling

Using @ExceptionHandler

You can add extra (@ExceptionHandler) methods to any controller to specifically handle exceptions thrown by request handling (@RequestMapping) methods in the same controller. Such methods can:
  1. Handle exceptions without the @ResponseStatus annotation (typically predefined exceptions that you didn’t write)
  2. Redirect the user to a dedicated error view
  3. Build a totally custom error response

Saturday, October 27, 2018

Hibernate: storeProcedure

You can use createSQLQuery() to call a store procedure directly.
Query query = session.createSQLQuery(
 "CALL GetStocks(:stockCode)")
 .addEntity(Stock.class)
 .setParameter("stockCode", "7277");
   
List result = query.list();
for(int i=0; i<result.size(); i++){
 Stock stock = (Stock)result.get(i);
 System.out.println(stock.getStockCode());
}

Thursday, October 25, 2018

Fix for JQuery Dialog:

+$.noConflict(); +jQuery(function () { + $( "#divDidNotWorkMsg" ).dialog({ + Open: false + }); +});

Sunday, October 21, 2018

MAPPED BY MEANING ONE TO ONE MAPPING

Friday, October 19, 2018

DEADLOCK:

private static class Resource {
public int value;
}
private Resource resourceA = new Resource();
private Resource resourceB = new Resource();
public int read() {
synchronized(resourceA) { // May deadlock here
synchronized(resourceB) {
return resourceB.value + resourceA.value;
}
}
}

public void write(int a, int b) {
synchronized(resourceB) { // May deadlock here
synchronized(resourceA) {
resourceA.value = a;
resourceB.value = b;
}
}
}
}

Stack and Heap:
Instance variables and objects live on the heap.
 Local variables live on the stack.

Thursday, October 11, 2018


Iterator         Enumeration
Throw ConcurrentModification ExceptionYes                No
Remove() methodYes, you can remove the element
while traversing it
                No
Addition to JDK1.2               1.0
LegacyNo               Yes

Wednesday, October 10, 2018

Enabling JSTL:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %>

Wednesday, October 3, 2018

div id="dialog">
$(function() {

  $("#dialog").dialog({
     autoOpen: false,
     width: 630,
     modal: true,
     buttons : {
          "Cancel" : function() {
            $(this).dialog("close");
          },
          "Confirm" : function() {
          document.forms[0].submit();         
          }
        }
      });
  $(".ui-button-icon-only").hide();
  $(".ui-dialog-titlebar").hide();

});

function confirmDNW(){
if(document.forms[0].didNotWork && document.forms[0].didNotWork.checked == true){
var startDateVar=document.getElementById("startDate").value ;
if(startDateVar != null && startDateVar.trim() != ''){
    $("#dialog").html("
You are creating 'Did Not Work' timesheet for the selected period" + ".

" + "Once created this timesheet cannot be modified." + "


" +"Please Confirm.
" )

}else{
    $("#dialog").html("
You are creating 'Did Not Work' timesheet for period of "+ document.getElementById("dateRangeString").value + ".

" + "Once created this timesheet cannot be modified." + "


" +"Please Confirm.
" )
}
    $("#dialog").dialog("open");
    $("#footer").css("z-index", "0");
   
}else{
document.forms[0].submit();
}

}

Monday, October 1, 2018

DOM  and SAX Parser for XML:
 The DOM Parser loads the complete XML content into a Tree structure. And we iterate through the Node and NodeList to get the content of the XML. 
DocumentBuilder builder = factory.newDocumentBuilder();
 
    //Load and Parse the XML document
    //document contains the complete XML as a Tree.
    Document document =
      builder.parse(
        ClassLoader.getSystemResourceAsStream("xml/employee.xml"));
 
 
    List empList = new ArrayList<>();
 
    //Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();
 
    for (int i = 0; i < nodeList.getLength(); i++) {
 We will get the NodeList and parse.

SAX Parser for XML:
SAX Parser is different from the DOM Parser where SAX parser doesn’t load the complete XML into the memory, instead it parses the XML line by line triggering different events as and when it encounters different elements like: opening tag, closing tag, character data, comments and so on. This is the reason why SAX Parser is called an event based parser.