Java @ Desk: Spring | Java at your desk

Angular JS routing with Spring 4 MVC using ngRoute

Angular JS routing with Spring 4 MVC using ngRoute

This example demonstrates how routing works in angular js using ngRoute, $routeProvider.

Following components are used:
1) Spring 4.3.0.RELEASE
2) Angular JS 1.4.4
3) Angular JS Route 1.4.4

Configuring Routes
Routes in angular js are configured in the config function using the $routeProvider service. In the example, we will configure 2 routes, 1st to load the list of Persons and 2nd to load list of Customers.
1) templateUrl - This triggers the Spring's controller to decide which html page to navigate to
2) controller - This triggers the angular js controller
3) resolve - This triggers the angular js service

app.js
'use strict';

var indexApp = angular.module('indexApp', [ 'ngRoute' ]);

indexApp.config([ '$routeProvider', function($routeProvider) {
 $routeProvider.when('/loadPersons', {
  templateUrl : 'loadPersons',
  controller : "PersonController as personCtrl",
  resolve : {
   async : [ 'PersonService', function(PersonService) {
    return PersonService.fetchPersons();
   } ]
  }
 }).when('/loadCustomers', {
  templateUrl : 'loadCustomers',
  controller : "CustomerController as custCtrl",
  resolve : {
   async : [ 'CustomerService', function(CustomerService) {
    return CustomerService.fetchCustomers();
   } ]
  }
 });
} ]);


Service JS
Angular JS service file will communicate with the Spring controller where the services are configured. The call to spring is done using the $http.

appService.js
'use strict';

indexApp.factory('CustomerService', [ '$http', '$q', function($http, $q) {
 return {
  fetchCustomers : function() {
   return $http.get('customers').then(function(response) {
    return response.data;
   }, function(errResponse) {
    console.error('Error while fetching Items');
    return $q.reject(errResponse);
   });
  }
 };
} ]);

indexApp.factory('PersonService', [ '$http', '$q', function($http, $q) {
 return {
  fetchPersons : function() {
   return $http.get('persons').then(function(response) {
    return response.data;
   }, function(errResponse) {
    console.error('Error while fetching Items');
    return $q.reject(errResponse);
   });
  }
 };
} ]);


Controller Js
Angular JS controller file sets the response of service to the UI scope variables defined in JSP page.

appController.js
'use strict';

indexApp.controller('CustomerController', [ 'async', function(async) {
 var self = this;
 self.customers = async;
} ]);

indexApp.controller('PersonController', [ 'async', function(async) {
 var self = this;
 self.persons = async;
} ]);


Welcome Page
This page configures the indexApp ng app and displays the content using the ng-view directive.



index.jsp - In below JSP, when user clicks on "Get Persons List" hyperlink, it calls the config of app.js and check with the $routeProvider for the matching URL. It finds the "/loadPersons" and it hits the "templateUrl : 'loadPersons'" that goes to MainController.java to define the HTML page to navigate to. In MainController.java, it finds "persons" and so navigates to persons.jsp as shown below and displays the content on ng-view.

<!DOCTYPE html>
<html ng-app="indexApp">
<head>
<title>Person App</title>
<script src="resources/js/angular.js"></script>
<script src="resources/js/angular-route.js"></script>
<script src="resources/js/app.js"></script>
<script src="resources/js/appService.js"></script>
<script src="resources/js/jquery.min.js"></script>
</head>

<body>
 Home Screen
 <li><a href="#/loadPersons">Get Persons List</a></li>
 <li><a href="#/loadCustomers">Get Customers List</a></li>
 <div ng-view></div>
 <script src="resources/js/appController.js"></script>
</body>
</html>


persons.jsp
<!DOCTYPE html>
<html ng-app="personApp">
<head>
<title>Person App</title>
<script src="resources/js/angular.js"></script>
<script src="resources/js/angular-route.js"></script>
<script src="resources/js/app.js"></script>
<script src="resources/js/personService.js"></script>
<script src="resources/js/jquery.min.js"></script>
</head>

<body>
 Persons Data -
 <div ng-repeat="person in personCtrl.persons">
  Person Name - {{person.name}} <br> Person Id - {{person.id}}
 </div>
 <li><a href="#/back">Back</a></li>
 <script src="resources/js/personController.js"></script>
</body>
</html>


customers.jsp
<!DOCTYPE html>
<html ng-app="customerApp">
<head>
<title>Person App</title>
<script src="resources/js/angular.js"></script>
<script src="resources/js/angular-route.js"></script>
<script src="resources/js/appRoute.js"></script>
<script src="resources/js/customerService.js"></script>
<script src="resources/js/jquery.min.js"></script>
</head>

<body>
 Customers Data -
 <div ng-repeat="customer in custCtrl.customers">
  Customer Name - {{customer.name}} <br> Customer Id -
  {{customer.id}}
 </div>
 <li><a href="#/back">Back</a></li>
 <script src="resources/js/customerController.js"></script>
</body>
</html>

angular js $http rest controller status code 0

angular js $http rest controller status code 0

Angular JS $hhtp returns the status code 0 on calling the get request from the spring rest controller. The rest controller in the example below produces "application/json".

The response come fine if the URL is tested in the browser. It successfully prints the response on the chrome browser, but the same request returns the status code 0 if called from angular js $http. Below is the sample request and spring rest controller for the same.

angularController.js
var app = angular.module('customerApp', []);
app.controller("customerController", function($scope, $http) {
 $http.get('http://localhost:8060/customers').then(
   function successCallback(response) {
          $scope.customer = {name: response.data.name, id: response.data.id};
   }, function errorCallback(response) {
   });
});


CustomerController.java
package com.accenture.restviability.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.acc.auto.service.request.Customer;

@RestController()
public class CustomerController {

 @RequestMapping(method = RequestMethod.GET, path = "/customers", produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<Customer> getCustomers() {
  System.out.println("Entered in Get Customers");
  List<Customer> customers = new ArrayList<Customer>();
  Customer customer = new Customer();
  customer.setName("Kumar");
  customer.setId("1");
  return new ResponseEntity<Customer>(customer, HttpStatus.OK);
 }
}


The above angular method returns the status code 0 on calling the spring rest controller. The issue occure because of HTTP Access Control CORS issues.

Angular does not consume the response from the server that does not include Access-Control-Allow-Origin header in the response. The server responding back the angular JS calls must include the headers specified by CORS. In order to make it work, you need to include the below annotation of Spring.

@CrossOrigin(maxAge = 3600) from import org.springframework.web.bind.annotation.CrossOrigin;
package of Spring

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 allows annotation based configuration to initialize the web application without having the web.xml as well as spring configuration xml file. The spring controller and services are loaded using the annotation @ComponentScan.

With servlet 3.x there is no need of the spring configuration file defined in web.xml. Servlet 3.x allows annotation based loading of configuration classes for controllers and services.

Below example will load the controllers and services classes on server start up i.e. there will be a class to replace web.xml to map the Spring Dispatcher Servlet and load it on server start up. Rest controller example is implemented here that returns the JSON response to the caller.

pom.xml - Add Spring 4.x release and Servlet 3.x release to support annotation based loading of Dispatcher Servlet
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>Blogger</groupId>
 <artifactId>Blogger</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <build>
  <finalName>blogger</finalName>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.3.1.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.7.5</version>
  </dependency>
 </dependencies>
</project>


AppConfig.java - This class bootstrap Spring 4 MVC Rest in our application on server startup
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.rest.spring4.annotationconfiguration")
public class AppConfig {

}


AppInitializer.java - This class is loaded by Servlet 3.x. This class will replace web.xml and it will map the spring’s dispatcher servlet and bootstrap it.
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 @Override
 protected Class[] getRootConfigClasses() {
  return new Class[] { AppConfig.class };
 }

 @Override
 protected Class[] getServletConfigClasses() {
  return null;
 }

 @Override
 protected String[] getServletMappings() {
  return new String[] { "/" };
 }
}




Person.java - Domain object for JSON response
package com.rest.spring4.annotationconfiguration.pojo;

public class Person {

 private String id;

 private String name;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}


PersonRestController.java - This class contains the Rest mapping to load the list of person objects in JSON format
package com.rest.spring4.annotationconfiguration.restcontroller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.rest.spring4.annotationconfiguration.pojo.Person;

@RestController
public class PersonRestController {

 @GetMapping("/persons")
 public ResponseEntity getPersons() {
  List<Person> persons = new ArrayList<Person>();

  Person person = new Person();
  person.setId("Id 1");
  person.setName("Person Name 1");

  persons.add(person);

  Person person2 = new Person();
  person2.setId("Id 2");
  person2.setName("Person Name 2");

  persons.add(person2);

  return new ResponseEntity(persons, HttpStatus.OK);
 }
}


Output:
[{"id":"Id 1","name":"Person Name 1"},{"id":"Id 2","name":"Person Name 2"}]

Angular JS Spring MVC Rest CRUD JSON Example using $http

Angular JS Spring MVC Rest CRUD JSON Example using $http

In this example, we will learn how to get the json data from rest service controller from Spring and display using the ng-model attribute of angular JS.

Spring RestController returns the Customer java object in JSON format and data is displayed on UI using angular JS. Call to the rest api is done using the $http of angular JS.

Following components will be used
1) Spring RestController
2) Angular JS ng-app
3) Angular JS ng-controller
4) Angular JS ng-model
5) Angular JS $http

Here are the files that are used.

Customer.java
package com.accenture.restviability.pojo;

public class Customer {

 private String name;
 
 private String id;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }
}


SpringRestController
package com.accenture.restviability.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.accenture.restviability.pojo.Customer;
import com.google.gson.Gson;

@RestController()
@CrossOrigin(maxAge = 3600)
public class SpringRestController {

 @RequestMapping(method = RequestMethod.GET, path = "/customers", produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<Customer> getCustomers() {
  System.out.println("Entered in Get Customers");
  List<Customer> customers = new ArrayList<Customer>();
  Customer customer = new Customer();
  customer.setName("Kumar");
  customer.setId("1");
  Gson gson = new Gson();
  return new ResponseEntity<Customer>(customer, HttpStatus.OK);
 }
}




customer.jsp
<!DOCTYPE html>
<html ng-app="customerApp">
<head>
<title>Customer App</title>
<script src="js/angular.js"></script>
<script src="js/customerController.js"></script>
<script src="js/jquery.min.js"></script>
</head>

<body>
 <div ng-controller="customerController">
  <p>
   Customer Id - <input type="text" ng-model="customer.id" />
  </p>
  <p>
   Customer Name - <input type="text" ng-model="customer.name" />
  </p>
 </div>
</body>
</html>


customerController.js
var app = angular.module('customerApp', []);
app.controller("customerController", function($scope, $http) {
 $http.get('http://localhost:8070/customers').then(
   function successCallback(response) {
          $scope.customer = {name: response.data.name, id: response.data.id};
   }, function errorCallback(response) {
   });
});


Spring MVC JdbcTemplate with JNDI Datasource in Tomcat Server

Spring MVC JdbcTemplate with JNDI Datasource in Tomcat Server

Configuring a datasource through JNDI mapping in Tomcat is beneficial since the database configuration remains independent of application. Configuring a JNDI in Tomcat and using the reference within Spring application to create a JdbcTemplate object achieves the object of loose coupling.

Any database configuration changes would be done at the Tomcat end without affecting the application deployed.

Below are the steps to define a JNDI in Tomcat and use in Spring configuration XML file to map with JdbcTemplate:

1) Create a JNDI configuration in Tomcat:
Open context.xml file in /conf folder and add a Resource entry

<Resource name="jdbc/MyDataSource"
              auth="Container"
              type="javax.sql.DataSource"
              username="${db.user}"
              password="${db.password}"
              driverClassName="${db.driver}"
              url="${db.url}"              
              maxActive="15"
              maxIdle="5"/>


2) Create properties in catalina.properties in /conf folder
db.driver=oracle.jdbc.driver.OracleDriver
db.user=DEVUser
db.password=XPL7TR360
db.url=URL_OF_DB


3) Update Spring configuration xml file to create JNDI mapping
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
 
<jee:jndi-lookup id="dataSource" expected-type="javax.sql.DataSource" jndi-name="jdbc/MyDataSource" resource-ref="true"/>


4) Inject or Autowire the JdbcTemplate object in classes to perform DB operation

Spring init-method and destroy-method example

Spring init-method and destroy-method example

A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.

Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.

To achieve this, a bean need to be configured in a spring configuration file and following attributes are used for the bean:
1) init-method - Provide the method name of the bean declared which needs to get called once the bean is initialized. For example, loading of properties file.
2) destroy-method - Provide the method name of the bean declared which needs to get called once the bean is destroyed. For example, clearing properties file.

In case, a method name used with init-method or destory-method is not found in a class, it throws the following exception:
Error creating bean with name 'springInitDestroy'
defined in class path resource [com//spring//appContext.xml]:
Invalid destruction signature; nested exception is org.springframework.beans.factory.support.BeanDefinitionValidationException:
Couldn't find a destroy method named 'cleanUp1' on bean with name 'springInitDestroy'
Where cleanUp1 -> method name not found in class.

Below is the sample implementation:
1) Spring Configuration File:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />

 <bean id="springInitDestroy" class="com.spring.SpringInitDestroy"
  init-method="initIt" destroy-method="cleanUp">
 </bean>

</beans>

2) Spring Bean Class:
package com.spring;

public class SpringInitDestroy {

 public void initIt() throws Exception {
  System.out.println("Init method called on bean initialization");
 }

 public void cleanUp() throws Exception {
  System.out.println("Destroy method called on bean destroy");
 }
}

3) Test Client Class:


package com.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SprintInitDestroyTest {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com//spring//appContext.xml" });

  SpringInitDestroy initDestroy = (SpringInitDestroy) context
    .getBean("springInitDestroy");

  System.out.println("Bean Created : " + initDestroy);

  context.close();
 }
}

Spring InitializingBean interface init bean annotation

Spring InitializingBean interface init bean annotation

A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.

Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.

In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) Override afterPropertiesSet - Belongs to org.springframework.beans.factory.InitializingBean package. Override the function to provide a callback function (afterPropertiesSet()) which the ApplicationContext will invoke when the bean is constructed
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package

In case of annotation, Spring container will not be aware of @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
 1) Specify the <context:annotation-config />  
 2) Specify the <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  

Below is the sample implementation for this:
1) Spring Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />

 <bean id="springInitDestroy" class="com.spring.SpringInitDestroy">
 </bean>

</beans>
2) SpringInitDestroy Bean
package com.spring;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.InitializingBean;

public class SpringInitDestroy implements InitializingBean {
 @PreDestroy
 public void cleanUp() throws Exception {
  System.out.println("Init method called on bean destroy");
 }

 @Override
 public void afterPropertiesSet() throws Exception {
  System.out
    .println("Init method called on bean initialization - afterPropertiesSet");
 }
}
3) SprintInitDestroyTest Client File
package com.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SprintInitDestroyTest {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com//spring//appContext.xml" });

  SpringInitDestroy initDestroy = (SpringInitDestroy) context
    .getBean("springInitDestroy");

  System.out.println("Bean Created : " + initDestroy);

  context.close(); // @PreDestory method gets called here
 }
}

Output :
Init method called on bean initialization - afterPropertiesSet
Bean Created : com.spring.SpringInitDestroy@197bb7
Init method called on bean destroy

Spring @PostConstruct & @PreDestroy init bean method annotation

Spring @PostConstruct & @PreDestroy init bean method annotation

A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.

Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.

In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) @PostConstruct - Belongs to javax.annotation.PostConstruct package
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package

In case of annotation, Spring container will not be aware of @PostConstruct and @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
 1) Specify the <context:annotation-config />  
 2) Specify the <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  

Below is the sample implementation for this:
1) Spring Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />

 <bean id="springInitDestroy" class="com.spring.SpringInitDestroy">
 </bean>

</beans>
2) SpringInitDestroy Bean
package com.spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class SpringInitDestroy {

 @PostConstruct
 public void initIt() throws Exception {
  System.out.println("Init method called on bean initialization");
 }

 @PreDestroy
 public void cleanUp() throws Exception {
  System.out.println("Init method called on bean destroy");
 }
}
3) SprintInitDestroyTest Client File
package com.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SprintInitDestroyTest {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com//spring//appContext.xml" });

  SpringInitDestroy initDestroy = (SpringInitDestroy) context
    .getBean("springInitDestroy");

  System.out.println("Bean Created : " + initDestroy);

  context.close(); // @PreDestory method gets called here
 }
}

Output :
Init method called on bean initialization
Bean Created : com.spring.SpringInitDestroy@1a897a9
Init method called on bean destroy

Hibernate annotation example with Spring

Hibernate annotation example with Spring

The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Configure the datasource - This is configured in the spring application context xml file. Configure the bean for datasource using class org.springframework.jdbc.datasource.DriverManagerDataSource. The class can be found in jar spring-jdbc-2.0.7.jar
Load Database Configuration from Properties file

Integrate Struts Spring Hibernate web application

Integrate Struts Spring Hibernate web application

The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Load the Spring application context xml file in struts-config - This can be achieved using the Spring pugin for struts org.springframework.web.struts.ContextLoaderPlugIn class. The jar in which you can find this class is org.springframework.web.struts-3.0.1.RELEASE.jar

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  
 <set-property property="contextConfigLocation"  
 value="/WEB-INF/springconfig/applicationContext.xml"/>  
 </plug-in>