School Admission System

School Admission System


Posted in : Java Posted on : May 17, 2012 at 4:16 PM Comments : [ 0 ]

In this sample project you will learns about the simple web project of school and you will also learn about the integration Spring MVC, JPA with Hibernate

School Admission System

This a simple web project build using the following technologies.

  • Spring - As a web technology.
  • Hibernate - for handling database connections.
  • JPA - for maintaining persistence data in the application.
  • Maven - for compilation and building the application.
  • Jetty - for running application

This project will help you a lot in configuring and understanding the flow of the application.

At first look at the structure of the project

Now lets start with the web.xml, this file is also known as the deployment descriptor. So in order to use Spring MVC at first you need to map it here as.

Mapping the DispatcherServlet servlet that handles the client request

<servlet>
	<servlet-name>handler</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>handler</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

The above mapping will search the handler-servlet.xml in the /WEB-INF direcoty.

Now you require to map the ContextLoaderListener as.

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

This will search the file applicationContext.xml in the /WEB-INF directory. You can also save this file with different name in different location as.

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/myApplicationContext*.xml</param-value>
</context-param>

The above configuration is some basic configuration that is required for running this application. Now lets move to the handler-servlet.xml configuration. This basically contains the mapping for ViewResolver and CommonsMultipartResolver (if you are uploading some file). You can also map transaction manager, datasource, bean ect. But in this application we are using xml file called hibernate-context.xml. The mapping for the view resolver is as

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

Let us now look at the applicationContext.xmlWe have declared following.

<context:annotation-config />
This actives all the annoration classes. It allows to use @Required, @Autowire @Qualifier etc.. annotations.

<context:component-scan base-package="roseindia.tutorial.application" />
This declaration allow @Service, @Component, @Controller etc.. annotations.

The one important configuration is hibernate configuration. This imports the hibernate-context.xml as
<import resource="hibernate-context.xml" />

Now let us look into the hibernate-context.xml

In this xml datasouce, entityManagerFactory and transactionManager is mapped as

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
	destroy-method="close"
	p:driverClass="${database.driverClassName}"
	p:jdbcUrl="${database.connection.url}"
	p:user="${database.user.name}"
	p:password="${database.password}"
	p:acquireIncrement="5"
	p:idleConnectionTestPeriod="60"
	p:maxPoolSize="50"
	p:maxStatements="40"
	p:minPoolSize="5" />

<!-- Mapping JPA with Entity Manager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
	<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
	<property name="persistenceUnitName" value="schoolAdmissionPersistenceUnit" />
	<property name="dataSource" ref="dataSource"/>
	<property name="jpaVendorAdapter">
	<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
		<property name="showSql" value="true"/>
	</bean>
	</property>
</bean>

<!-- Mapping TransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> 

Here the value for persistenceUnitName is comming from persistence.xml stored in /resources/META-INF/ directory

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
	<persistence-unit name="schoolAdmissionPersistenceUnit" transaction-type="RESOURCE_LOCAL">
		<properties>
			<property name="hibernate.hbm2ddl.auto" value="none" />
		</properties>
	</persistence-unit>
</persistence>

In the above application we have writtent two controller one is for administrator and another for user

UserController.java

package roseindia.tutorial.application.controller;

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

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import roseindia.tutorial.application.form.StudentForm;
import roseindia.tutorial.application.form.StudentSearchForm;
import roseindia.tutorial.application.service.SchoolService;
import roseindia.tutorial.application.domain.Student;

@SuppressWarnings("unchecked")
@Controller
public class UserController{
	
	@Resource(name="addmissionService")
	private SchoolService service;


	@RequestMapping(value = "/load-home-page")
	public String loadHomePage(Map model){
		return "user/index";
	}

	@RequestMapping(value = "/view-cources")
	public String loadCources(Map model, HttpServletRequest request){
		int id = 0;
		if(request.getParameter("id") != null){
			id = Integer.parseInt(request.getParameter("id"));
			model.put("courseList", service.findCourse(id));
			return "user/view-courses";
		}
		model.put("courseList", service.loadCourse());
		return "user/view-courses";
	}
}

AdminCotroller.java

package roseindia.tutorial.application.controller;

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

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import roseindia.tutorial.application.form.StudentForm;
import roseindia.tutorial.application.form.StudentSearchForm;
import roseindia.tutorial.application.service.SchoolService;
import roseindia.tutorial.application.domain.Student;

@SuppressWarnings("unchecked")
@Controller
public class AdminCotroller {

	@Resource(name="addmissionService")
	private SchoolService service;


	@RequestMapping(value = "/admin/load-home-page")
	public String loadHomePage(Map model){
		return "admin/index";
	}

	@RequestMapping(value = "/admin/load-student-form")
	public String loadStudentForm(Map model, StudentForm studentForm, HttpServletRequest request){				
		if(request.getParameter("id") != null){
			int rollNo = Integer.parseInt(request.getParameter("id"));
			studentForm = service.findStudent(rollNo);
		}
		if(request.getParameter("courseId") != null){
			int courseId = Integer.parseInt(request.getParameter("courseId"));
			studentForm.setCourse(courseId);
		}
		model.put("studentForm", studentForm);
		model.put("courseList", service.loadCourse());		
		return "admin/student-form";
	}
	
	@RequestMapping(value = "/admin/add-update-student")
	public String addUpdateStudent(Map model, StudentForm studentForm){
		model.put("studentForm", studentForm);
		model.put("courseList", service.loadCourse());
		if(studentForm.getRollNo() != null){
			System.out.println("Updating Records to the Database");
			service.updateStudent(studentForm);
			return "forward:/admin/search";
		}else{		
			System.out.println("Saving Records to the Database");
			service.addSudent(studentForm);
			return "forward:/admin/load-home-page";
		}
	}

	@RequestMapping(value = "/admin/search-student")
	public String searchStudent(Map model, StudentSearchForm studentSearchForm){
		model.put("courseList", service.loadCourse());
		model.put("studentSearchForm", studentSearchForm);
		return "admin/student-search-form";
	}

	@RequestMapping(value = "/admin/search")
	public String search(Map model, StudentSearchForm studentSearchForm){
		model.put("courseList", service.loadCourse());
		model.put("studentSearchForm", studentSearchForm);
		model.put("studentList", service.findStudent(studentSearchForm));
		return "admin/student-search-form";
	}
	
	@RequestMapping(value = "/admin/delete-student")
	public String delete(Map model, StudentSearchForm studentSearchForm, HttpServletRequest request){
		model.put("studentSearchForm", studentSearchForm);
		int rollNo = 0;
		if(request.getParameter("id") != null){
			rollNo = Integer.parseInt(request.getParameter("id"));			
			service.deleteStudent(rollNo);
		}
		return "forward:/admin/search-student";
	}

}

You can download source code from here and open command prompt and type mvn jetty:run and open browser type url http://localhost:9090/addmission.system/admin/load-home-page you will see output as given below.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics