import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@SpringBootApplication
public class SpringBootMVCApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootMVCApplication.class, args);
}
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver r = new InternalResourceViewResolver();
r.setPrefix("/WEB-INF/jsp/");
r.setSuffix(".jsp");
return r;
}
}
import javax.servlet.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
@Component
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
System.out.println("CORSFilter");
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET");
response.setHeader("Access-Control-Max-Age", "3700");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ramsis.main.dto.EmployeeDTO;
import com.ramsis.main.service.EmployeeService;
@Controller
@CrossOrigin(origins = "*")
public class RegistrationController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/registration")
public String reg(Map<String, Object> model) {
model.put("employee", new EmployeeDTO());
return "Registration";
}
@PostMapping("/home")
public String createEmployee(@ModelAttribute("employee") EmployeeDTO empDto) {
employeeService.createOrUpdateEmployee(empDto);
return "redirect:/list";
}
@GetMapping("/list")
public String listOfEmployee(Model model) {
List<EmployeeDTO> employeeList = employeeService.getAllEmployee();
model.addAttribute("empList", employeeList);
return "employeeList";
}
@PostMapping("/delete")
public String deleteEmployee(@RequestParam("id") String id) {
employeeService.deleteEmployee(Long.parseLong(id));
return "redirect:/list";
}
@GetMapping("/edit")
public String editEmployee(@RequestParam("id") String id, Map<String, Object> model) {
EmployeeDTO empDTO = employeeService.editEmployee(Long.parseLong(id));
model.put("employee", empDTO);
return "Registration";
}
}
import com.ramsis.main.model.Employee;
public class EmployeeDTO {
private Long id;
private String firstName;
private String lastName;
private String userName;
private String emailId;
private String empId;
private String bloodGp;
private int age;
private String personalEmail;
private String mobileNo;
public EmployeeDTO() {
}
public EmployeeDTO(Employee employee) {
this.firstName = employee.getFirstName();
this.lastName = employee.getLastName();
this.userName = employee.getUserName();
this.emailId = employee.getEmailId();
this.empId = employee.getEmpId();
this.bloodGp = employee.getBloodGp();
this.age = employee.getAge();
this.personalEmail = employee.getPersonalEmail();
this.mobileNo = employee.getMobileNo();
this.id = employee.getId();
}
public EmployeeDTO(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getBloodGp() {
return bloodGp;
}
public void setBloodGp(String bloodGp) {
this.bloodGp = bloodGp;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPersonalEmail() {
return personalEmail;
}
public void setPersonalEmail(String personalEmail) {
this.personalEmail = personalEmail;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
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
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="user_name")
private String userName;
@Column(name="email_id")
private String emailId;
@Column(name="emp_id")
private String empId;
@Column(name="blood_gp")
private String bloodGp;
@Column(name="age")
private int age;
@Column(name="personal_email")
private String personalEmail;
@Column(name="mobile_no")
private String mobileNo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getBloodGp() {
return bloodGp;
}
public void setBloodGp(String bloodGp) {
this.bloodGp = bloodGp;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPersonalEmail() {
return personalEmail;
}
public void setPersonalEmail(String personalEmail) {
this.personalEmail = personalEmail;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ramsis.main.dto.EmployeeDTO;
import com.ramsis.main.model.Employee;
import com.ramsis.main.repository.EmployeeRepository;
import com.ramsis.main.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeRepository employeeRepository;
public void createOrUpdateEmployee(EmployeeDTO empDto) {
Employee emp = convertDtoToModel(empDto);
employeeRepository.save(emp);
}
public List<EmployeeDTO> getAllEmployee() {
List<Employee> list = employeeRepository.findAll();
List<EmployeeDTO> employeeList = list.stream()
.map(EmployeeDTO::new)
.collect(Collectors.toCollection(ArrayList::new));
return employeeList;
}
public void deleteEmployee(Long id) {
employeeRepository.deleteById(id);
}
public EmployeeDTO editEmployee(Long id) {
Employee emp = employeeRepository.getOne(id);
return convertModelToDTO(emp);
}
private Employee convertDtoToModel(EmployeeDTO empDto) {
Employee emp = new Employee();
if (empDto.getId() != null) {
emp.setId(empDto.getId());
}
emp.setAge(empDto.getAge());
emp.setBloodGp(empDto.getBloodGp());
emp.setEmailId(empDto.getEmailId());
emp.setEmpId(empDto.getEmpId());
emp.setFirstName(empDto.getFirstName());
emp.setLastName(empDto.getLastName());
emp.setMobileNo(empDto.getMobileNo());
emp.setPersonalEmail(empDto.getPersonalEmail());
emp.setUserName(empDto.getUserName());
return emp;
}
private EmployeeDTO convertModelToDTO(Employee emp) {
EmployeeDTO empDTO = new EmployeeDTO();
empDTO.setId(emp.getId());
empDTO.setAge(emp.getAge());
empDTO.setBloodGp(emp.getBloodGp());
empDTO.setEmailId(emp.getEmailId());
empDTO.setEmpId(emp.getEmpId());
empDTO.setFirstName(emp.getFirstName());
empDTO.setLastName(emp.getLastName());
empDTO.setMobileNo(emp.getMobileNo());
empDTO.setPersonalEmail(emp.getPersonalEmail());
empDTO.setUserName(emp.getUserName());
return empDTO;
}
}
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=cctns@123
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<groupId>com.ramsis.main</groupId>
<artifactId>EMSApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- jstl for jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>