Saturday, February 23, 2013

Spring Introduction

Introduction
Spring is a light-weight open source framework. Its initially written by Rod Johnson. Spring enables you to develop applications using POJOs. This got a laryered structure, so that you can choose to use any part of it as an isolation.
Spring Includes
  1. An IOC Light weight Container
  2. AOP Functionality
  3. Abstraction Layer for JDBC/Transaction management
  4. Web Integration Layer
  5. MVC Web Application Framework
Spring Modules
The following are the modules in Spring Architecture
spring-modules

Each of the Components can stand on its own or can be implemented jointly one or more jointly. The functionality of each component is as follows:
  1. Core: This is the basic component in the framework. It has the features of IoC and Dependency Injection. It provides a basic Container called BeanFactory. 
  2. AOP: It introdues aspect oriented programming feature in the Spring framework. Spring-AOP addresses the cross-cutting functionality. 
  3. DAO: This package supports both programmatic and declarative approaches to manage transactions. These can be used to add transaction capability not only for classes implementing special interfaces, but for all ordinary POJOs (plain old Java objects).
  4. ORM: The ORM package provides integration layers for popular object-relational mapping APIs such as JPA, JDO, Hibernate, and iBatis. Using the ORM package you can use these frameworks in combination with all the other features Spring offers, such as the simple declarative transaction management feature.
  5. Enterprise Integration: This package provides an integration layer for popular enterprise services including several remoting technologies, EJB, JMS, JMX, e-mail, and scheduling.
  6. Web Framework Integration: Spring’s Web package provides integration layer for popular web frameworks such as WebWork, Struts, JSF etc. It also provides features such as multipart file-upload functionality, initialization of the IoC container using servlet listeners and a web-oriented application context.



Templating with CI

Let’s see how to templating with CI.
  • Create Template : Create a file template.php in application/views with the below content.
<?php
$this->load->view(‘header’);
$this->load->view($content);
$this->load->view(‘footer’);
?>
  • Changing default Controller : By default, the controller will be welcome. If you want, you can change the configuration in application/config/routes.php file. Change the default_controller to your custom one (Note: Dont include extension).
  • Create Controller : Create the controller(If you have changed the default controller or exisitng one). Change the index function with the following .
$data['content']=’login’;
$this->load->view(‘template’,$data);
  • Create View : Create header.php, login.php and footer.php in application/views.
By default, the Login page will be opened in place of content. index is the default function called in the controller. As we mentioned the above lines of code in default contoller’s default function. So, when we open the page. we will login page with header and footer.
  • Example : the contents of the three files are as follows
header.php
<html>
<body>
This is header part
Footer.php
This is footer part
</body>
</html>
login.php
<?=form_open(‘welcome/login’); ?>
Login Name<br />
<?=form_input(‘username’,”,’class=text’)?><br />
Password<br />
<?=form_password(‘password’,”,’class=text’)?><br />
<input type=”submit” name=”login” value=”Login” class=”button” />
<?=form_close(); ?>
The first line in login.php says, when the form is submitted, the action goes to login function in welcome controller. Notation is controller/function_name. If we dont specify the function, by default index function will be called.
In the function login, do the login validation and re-direct based on the validation. For example
welcome.php
public function login() {
$user_name = $this->input->post(‘username’);
 $user_pass = $this->input->post(‘password’);
if($user_name == $user_pass ) {
$data['content'] = ‘home’;
}
else  {
$data['content']=’login’;
}
$this->load->view(‘template’,$data);
}
For login authentication, we can use models as well. Create the file in application/model and call in the controller.
User.php in application/model
$user = new User();
$user->username = $user_name;
$user->password = $user_pass;
$status = $user->login();
Instead of if condition in login function, use the status variable to redirect to the respective page. So, based on the login, it will redirect to login or home page.