Wednesday, September 16, 2015

Spring MVC



Spring MVC has below major components :

DispacherServlet : it is a common entry point of all http request . It forwards the request to the controller .Spring MVC follows front controller design pattern in which there is one controller which responds to all the requests.

HandlerMapping : it determines the controller to be called from request url .

Controller : it services the request and sets up data to the model and pass it to View.

ViewResolver : it determines the view to which request is to be transferred.

Lets see how can make use for the above Spring components in a web application.

In web.xml file  make an entry for dispatcher servlet.
<servlet-name>spring-web</servlet-name>
<servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Then map your application URL to the disptcher servlet.

<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/urlOfApp</url-pattern>
</servlet-mapping>



Create a file spring-web-servlet.xml and place it in WEB-INF with the following content.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

</beans>


The naming convention of this file is [servlet name]-servlet.xml  , if we need to put a custom name or place it in custom path you need to configure the  listener class .We can also specify multiple application context xml  using the attribute. This is usually done to separate the web application context with the spring root application context. The web application context can be placed in WEB-INF folder which will be created when dispatcher servlet is loaded . The root application context can be created when the Context Loader Listener is loaded .



<listener>
<listener-class>org.spring.framework.web.context.ContextLoaderListener</listener-class>

</listener>
context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/common-config.xml</param-value>
</context-param>

If we need to have multiple web application configuration in different XML files we can also configure that.



<servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/main-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>spring-web2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/sub.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


To use annotations to configure beans instead of xml configuration we need to add the following line of code in Spring configuration xml.

<context:component-scan base-package="org.myProject.question" />

To create a controller class use annotation @Controller:
Controller :

@Controller
public class SampleController
{
@RequestMapping(value = "/controllerUrl")
public String performAction(ModelMap requestResponse)
{
String viewName =" home";
requestResponse.put("message","Welcome to Home page");
return viewName;
}
}


Controller returns the view name , View page is a jsp. Which has to be placed in the path specified for the ViewResolver bean , if you look into the spring-web-servlet.xml we have specified the path as WEB-INF/views/jsp using prefix and suffix attributes .


<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Home Page</title>
   </head>

   <body>
      <h2>${message}</h2>
   </body>
</html>


So let's try to build a web application with a home page .




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>My App </display-name>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">



<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

</beans>
@Controller
public class SampleController
{
@RequestMapping(value = "/home")
public String performAction(ModelMap requestResponse)
{
String viewName =" home";
requestResponse.put("message","Welcome to Home page");
return viewName;
}
}
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
 
   <body>
      <h2>${message}</h2>
   </body>
</html>

Let's try to build a restful web service using Spring MVC framework .
The following example will create a web service and list all the products .

Rest Controller :


@Controller
public class ProductController
{
@RequestMapping(value = "/products")
public @ResponseBody ProductListVO getAllProducts()
{
ProductListVO products = new ProductListVO();
//service call to get the products
return products;
}


End




Explain Normalization of database


The database normalization is a measure of stability .
There are 4 forms of Normalization

First Normal Form or 1NF

To be first normalized a database table should not have a duplicate data . Reference for a data should be unique . Lets assume there is a table Customer and another table Customer_Details . Both having columns customer_name and customer_id . In this situation,if we want to change customer_name , it is required to be changed in both the columns of the table , therefore is not first normalized.

Second Normal Form or 2NF

If the table is in 1NF then we can check for 2NF.

Tuesday, September 8, 2015

Java pass by value vs pass by reference

Hi guys,

This blog is about pass by value and pass by reference concept and understanding. Primitives are always pass by value, objects are always pass by reference.

If only the fields of the parameter are modified in the called method , the changes will be reflected in the calling method . If the reference of the parameter is assigned to a new object , the change will not be reflected in calling method .

Monday, September 7, 2015

Declarative Exception handling in struts


Declarative programming is a way to separate Control from computation logic . Exception handling in java is usually done using try catch and throws . In jsp we can handle exceptions using errorPage attribute of page declarative . Any exception occurred will be routed to the value of errorPage attribute . In addition to this struts offer similar way of handling exception . Use exception tags in StrutsConfig.xml to declare excpetion handling . All exception tags should be enclosed under global-Exception tag. Type of exception is mentioned in type attribute , exception message is mentioned in key attribute and the page which is called in case of exception is mentioned in path attribute of exception tag . The action exception message is picked from MessageResource.properties file for the key in key attribute .

Wednesday, August 26, 2015

Advice in spring


Advice is the action taken up by an Aspect at a join point . You can use a specific advice based when you want it to be executed . Before Advice : If you want your advice to be executed before the join point use Before Advice .

Monday, August 24, 2015

Hashcode in java

What is the use of hashcode in java.
Hashcode is very important in java . in object class , you will have a method hashcode , which will give the hashcode of an object.

hashcode is used to check whether two objects are equal or not . If two objects have a different hashcode they are definately not equal , but if their hashcode is same ,it does not guarantee both objects to be same .

hibernate pooling


what is hibernate pooling ?

aspect in spring


Aspect is modulerization of a concern that cuts accross multiple classes . Take an example of exception handling ,consider exception handling as an Aspect . In a program if an exception occurs it is an example join point as per spring nomenclature . At a join point action taken by Aspect is called an Advice , so if an ExceptionHandlingAspect calls logException(Exception e) ,logException is an example of advice . All advices has a pointcut expression , if for a join point , point cut expression is true , then the advice is executed at the join point .

Sorting list contents

List content is sorted using Collections.sort(listObject,comparatorObject);
For example if have list of Student object and you want to sort it, then you need to create a new object of type Comparator. The logic of comparision is written in

int compare(Object object1,Object object2)

While overriding compare method you need to return the following :
Positive integer : if object 1 > object 2
Negative integer : if object 2> object 1
Zero : if object 1 = object 2

This logic will allow your list to be sorted in ascending order.

Tuesday, May 5, 2015

Public key private key certificate

public key is the one which is shared , l can share my public key to you . Suppose you want to send a document to me .So you can encrypt it using a public key . Once it reaches me I can decrypt it using a private key . If the document is delivered to an unknown person he cannot decrypt it since he does not have the private key .

Sunday, April 26, 2015

Javax.mail.MessagingException

IOException while sending message;
nested exception is:
com.sun.mail.SMTP.SMTPTransport.sendMessage.

You seem to have call javax.mail.Transport. send from a j2ee application.

occurs if attachment is not present.
Remember extension is must , in case the extension is missing you will get this exception .

Monday, April 6, 2015

Shell

ps- process status

Reports

Report can be scheduled based on events or time . this can be sent to bank user or bank customer.

Customer cheques

Stale Customer cheque can be revalidated . in case of correspondent bank cheque stale and revalidation status need not be shared to corr. Bank through a report.

Demand draft

Demand draft cannot be stopped. There is no stop status for demand draft in CMS .

ec2-user@ec2 Permission denied