Thursday, 21 July 2016

Spring CRUD Operations in Eclipse

Hi,

Before going to crude operations first set up the project with spring security as follows.

Spring Project setup in Eclipse

the flow is

  • generate request from JSP
  • the request passed to controller then it send to service then go to dao
  • processing the data at dao and then return back to JSP
      JSP -> Controller -> Service -> Dao

     After login the welcome page should like this

     Welcome page and structure of project given below check once.


Welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi This is welcome page...!</h1>

<a href="employee.html">Employee</a> &nbsp;

<a href="<%=request.getContextPath()%>/j_spring_security_logout"> Logout</a>
</body>
</html>

Employee.java:

package com.vamsi.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="employeeId")
private Long employeeId;

@Column(name="name")
private String name;

public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}


EmployeeController.java:

package com.vamsi.controller;

import java.util.HashMap;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vamsi.model.Employee;
import com.vamsi.service.EmployeeService;

@Controller
public class EmployeeController {

@Autowired
EmployeeService employeeService;

@RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView addEmployee(@ModelAttribute("command") Employee employee) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("employee", employee);
model.put("employeeList", employeeService.getEmployees());
return new ModelAndView("/employee/index", model);
}

@RequestMapping(value = "/employeeSave", method = RequestMethod.POST)
public ModelAndView employeeSave(@ModelAttribute("command") Employee employee) {
employeeService.addEmployee(employee);
return new ModelAndView("redirect:/employee.html");
}

@RequestMapping(value = "/employeeDelete", method = RequestMethod.GET)
public ModelAndView deleteEmployee(@ModelAttribute("command") Employee employee){
employeeService.deleteEmployee(employee);
return new ModelAndView("redirect:/employee.html");
}

@RequestMapping(value = "/employeeEdit", method = RequestMethod.GET)
public ModelAndView editEmployee(@ModelAttribute("command") Employee employee){
Map<String, Object> model = new HashMap<String, Object>();
model.put("employee", employeeService.getEmployee(employee.getEmployeeId()));
model.put("employeeList", employeeService.getEmployees());
return new ModelAndView("/employee/index", model);
}

}

EmployeeService.java:

package com.vamsi.service;

import java.util.List;

import com.vamsi.model.Employee;

public interface EmployeeService {

public void addEmployee(Employee employee);
public Employee getEmployee(Long employeeId);
public List<Employee> getEmployees();
public void deleteEmployee(Employee employee);
}

EmployeeServiceImp.java:

package com.vamsi.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vamsi.dao.EmployeeDao;
import com.vamsi.model.Employee;

@Service("employeeService")
public class EmployeeServiceImp implements EmployeeService{

@Autowired
EmployeeDao employeeDao;
public void addEmployee(Employee employee) {
// TODO Auto-generated method stub
employeeDao.addEmployee(employee);
}

public Employee getEmployee(Long employeeId) {
// TODO Auto-generated method stub
return employeeDao.getEmployee(employeeId);
}

public List<Employee> getEmployees() {
// TODO Auto-generated method stub
return employeeDao.getEmployees();
}

public void deleteEmployee(Employee employee) {
// TODO Auto-generated method stub
employeeDao.deleteEmployee(employee);
}

}

EmployeeDao.java:

package com.vamsi.dao;

import java.util.List;

import com.vamsi.model.Employee;

public interface EmployeeDao {

public void addEmployee(Employee employee);
public Employee getEmployee(Long employeeId);
public List<Employee> getEmployees();
public void deleteEmployee(Employee employee);
}

EmployeeDaoImp.java:

package com.vamsi.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.vamsi.model.Employee;

@Repository("employeeDao")
public class EmployeeDaoImp implements EmployeeDao {

@Autowired
HibernateTemplate hibernateTemplate;
public void addEmployee(Employee employee) {
// TODO Auto-generated method stub
hibernateTemplate.saveOrUpdate(employee);
}

public Employee getEmployee(Long employeeId) {
// TODO Auto-generated method stub
return hibernateTemplate.get(Employee.class, employeeId);
}

public List<Employee> getEmployees() {
// TODO Auto-generated method stub
return hibernateTemplate.loadAll(Employee.class);
}

public void deleteEmployee(Employee employee) {
// TODO Auto-generated method stub
hibernateTemplate.delete(employee);
}


}

Domain whould be added in sdnext-servlet.xml as follows:


index.jsp:

this is employee management page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi I am Employee Index page</h1>
<form:form method="post" action="employeeSave.html">
<input type="hidden" name="employeeId" value="${employee.employeeId }"/>
Name <form:input type="text" path="name" value="${employee.name }"/><button type="submit">Submit</button>
</form:form>
<br/><br/>
<c:if test="${!empty employeeList }">
<table>
<thead>
<tr>
<th>Sno</th>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach items="${employeeList }" var="e">
<tr>
<td><c:out value=" ${e.employeeId }"/></td>
<td><c:out value=" ${e.name }"/></td>
<td><a href="employeeDelete.html?employeeId=${e.employeeId}">Delete</a> <a href="employeeEdit.html?employeeId=${e.employeeId}">Edit</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</body>
</html>

after run the app the frames as follows:

Login:

After login, welcome page as follows:

Click on Employee, we will navigate to manage employees. here we will do crud operations.


                                Thank You

No comments:

Post a Comment