Guest User

Untitled

a guest
Apr 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Insert title here</title>
  9. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  10. </head>
  11. <body>
  12. <h3>Add / Edit Employee!!!</h3>
  13.  
  14. <form method="post" action="/SpringMVCTutorial/employee.html" commandName="employee">
  15. <div class="table-responsive">
  16. <table class="table table-bordered" style="width: 300px">
  17.  
  18. <input type="text" name="id" />
  19.  
  20.  
  21. <input type="text" name="name" />
  22.  
  23.  
  24. <input type="text" name="age" />
  25.  
  26.  
  27. <input type="text" name="dept" />
  28.  
  29. <input class="btn btn-primary btn-sm" type="submit" value="Submit" />
  30. </tr>
  31. </table>
  32. </div>
  33. </form>
  34. <br>
  35. <br>
  36. <h3>List of Employees</h3>
  37. <table class="table table-bordered" style="width: 300px">
  38. <tr>
  39. <th>Id</th>
  40. <th>Name</th>
  41. <th>Age</th>
  42. <th>Department</th>
  43. <th>Edit/Delete</th>
  44. </tr>
  45. <c:forEach items="$ {employeeList}" var="employee">
  46.  
  47. <tr>
  48. <td width="60" align="center">${employee.id}</td>
  49. <td width="60" align="center">${employee.name}</td>
  50. <td width="60" align="center">${employee.age}</td>
  51. <td width="60" align="center">${employee.dept}</td>
  52. <td width="60" align="center"><a href="edit/${employee.id}">Edit</a>/<a href="delete/${employee.id}">Delete</a></td>
  53. </tr>
  54. </c:forEach>
  55. </table>
  56. </body>
  57. </html>
  58.  
  59. package com.javainterviewpoint;
  60.  
  61. import java.io.Serializable;
  62.  
  63. public class Employee implements Serializable
  64. {
  65. private static final long serialVersionUID = -1280037900360314186L;
  66.  
  67. private Integer id;
  68. private String name;
  69. private Integer age;
  70. private String dept;
  71. public Employee()
  72. {
  73. super();
  74. }
  75. public Employee(Integer id, String name, Integer age, String dept)
  76. {
  77. super();
  78. this.id = id;
  79. this.name = name;
  80. this.age = age;
  81. this.dept = dept;
  82. }
  83. public Integer getId()
  84. {
  85. return id;
  86. }
  87. public void setId(Integer id)
  88. {
  89. this.id = id;
  90. }
  91. public String getName()
  92. {
  93. return name;
  94. }
  95. public void setName(String name)
  96. {
  97. this.name = name;
  98. }
  99. public Integer getAge()
  100. {
  101. return age;
  102. }
  103. public void setAge(Integer age)
  104. {
  105. this.age = age;
  106. }
  107. public String getDept()
  108. {
  109. return dept;
  110. }
  111. public void setDept(String dept)
  112. {
  113. this.dept = dept;
  114. }
  115.  
  116. @Override
  117. public int hashCode()
  118. {
  119. final int prime = 31;
  120. int result = 1;
  121. result = prime * result + ((age == null) ? 0 : age.hashCode());
  122. result = prime * result + ((dept == null) ? 0 : dept.hashCode());
  123. result = prime * result + ((id == null) ? 0 : id.hashCode());
  124. result = prime * result + ((name == null) ? 0 : name.hashCode());
  125. return result;
  126. }
  127. @Override
  128. public boolean equals(Object obj)
  129. {
  130. if (this == obj)
  131. return true;
  132. if (obj == null)
  133. return false;
  134. if (getClass() != obj.getClass())
  135. return false;
  136. Employee other = (Employee) obj;
  137. if (age == null)
  138. {
  139. if (other.age != null)
  140. return false;
  141. } else if (!age.equals(other.age))
  142. return false;
  143. if (dept == null)
  144. {
  145. if (other.dept != null)
  146. return false;
  147. } else if (!dept.equals(other.dept))
  148. return false;
  149. if (id == null)
  150. {
  151. if (other.id != null)
  152. return false;
  153. } else if (!id.equals(other.id))
  154. return false;
  155. if (name == null)
  156. {
  157. if (other.name != null)
  158. return false;
  159. } else if (!name.equals(other.name))
  160. return false;
  161. return true;
  162. }
  163. @Override
  164. public String toString()
  165. {
  166. return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + "]";
  167. }
  168. }
  169.  
  170. package com.javainterviewpoint;
  171.  
  172. import java.util.List;
  173.  
  174. import org.springframework.beans.factory.annotation.Autowired;
  175. import org.springframework.dao.EmptyResultDataAccessException;
  176. import org.springframework.stereotype.Controller;
  177. import org.springframework.web.bind.annotation.ModelAttribute;
  178. import org.springframework.web.bind.annotation.PathVariable;
  179. import org.springframework.web.bind.annotation.RequestMapping;
  180. import org.springframework.web.bind.annotation.RequestMethod;
  181. import org.springframework.web.servlet.ModelAndView;
  182.  
  183. @Controller
  184. public class EmployeeController
  185. {
  186. @Autowired
  187. private EmployeeDAO employeeDAO;
  188.  
  189. @RequestMapping(value = "/employee",method=RequestMethod.POST)
  190. public ModelAndView saveEmployee(@ModelAttribute("employee") Employee employee)
  191. {
  192. try
  193. {
  194. if(employeeDAO.getEmployeeById(employee.getId()) != null);
  195. employeeDAO.updateEmployee(employee);
  196. }
  197. catch(EmptyResultDataAccessException e)
  198. {
  199. System.out.println("inside catch");
  200. employeeDAO.saveEmployee(employee);
  201. }
  202. return new ModelAndView("redirect:/employees");
  203. }
  204.  
  205. @RequestMapping(value = "/edit/{id}")
  206. public ModelAndView editEmployee(@ModelAttribute("employee") Employee employee,@PathVariable("id") int id)
  207. {
  208. ModelAndView model = new ModelAndView("employees");
  209.  
  210. employee = employeeDAO.getEmployeeById(id);
  211. List<Employee> employeeList = employeeDAO.getAllEmployees();
  212.  
  213. model.addObject("employee",employee);
  214. model.addObject("employeeList",employeeList);
  215.  
  216. return model;
  217.  
  218. }
  219.  
  220. @RequestMapping(value = "/delete/{id}")
  221. public ModelAndView deleteEmployee(@ModelAttribute("employee") Employee employee,@PathVariable("id") int id)
  222. {
  223. employeeDAO.deleteEmployee(id);
  224.  
  225. return new ModelAndView("redirect:/employees");
  226. }
  227.  
  228. @RequestMapping(value = "/employees")
  229. public ModelAndView listEmployees(@ModelAttribute("employee") Employee employee)
  230. {
  231. ModelAndView model = new ModelAndView("employees");
  232.  
  233. List<Employee> employeeList = employeeDAO.getAllEmployees();
  234. System.out.println(employeeList);
  235. model.addObject("employeeList", employeeList);
  236.  
  237. return model;
  238. }
  239. }
  240.  
  241. package com.javainterviewpoint;
  242.  
  243. import java.util.List;
  244. public interface EmployeeDAO
  245. {
  246. public void saveEmployee(Employee employee);
  247. public Employee getEmployeeById(int id);
  248. public void updateEmployee(Employee employee);
  249. public void deleteEmployee(int id);
  250. public List<Employee> getAllEmployees();
  251. }
  252.  
  253. package com.javainterviewpoint;
  254.  
  255. import java.sql.ResultSet;
  256. import java.sql.SQLException;
  257. import java.util.ArrayList;
  258. import java.util.List;
  259.  
  260. import org.springframework.dao.DataAccessException;
  261. import org.springframework.jdbc.core.JdbcTemplate;
  262. import org.springframework.jdbc.core.ResultSetExtractor;
  263. import org.springframework.jdbc.core.RowMapper;
  264. import org.springframework.stereotype.Repository;
  265.  
  266. @Repository
  267. public class EmployeeDAOImpl implements EmployeeDAO
  268. {
  269.  
  270. private JdbcTemplate jdbcTemplate;
  271. // JdbcTemplate setter
  272. public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
  273. {
  274. this.jdbcTemplate = jdbcTemplate;
  275. }
  276.  
  277. // Saving a new Employee
  278. public void saveEmployee(Employee employee)
  279. {
  280. String sql = "insert into employee1 values(?,?,?,?)";
  281. System.out.println("dao called");
  282. jdbcTemplate.update(sql, new Object[]
  283. { employee.getId(), employee.getAge(), employee.getDept(), employee.getName() });
  284. }
  285.  
  286. // Getting a particular Employee
  287. public Employee getEmployeeById(int id)
  288. {
  289. String sql = "select * from employee1 where id=?";
  290. Employee employee = (Employee) jdbcTemplate.queryForObject(sql, new Object[]
  291. { id }, new RowMapper<Employee>()
  292. {
  293. @Override
  294. public Employee mapRow(ResultSet rs, int rowNum) throws SQLException
  295. {
  296. Employee employee = new Employee();
  297. employee.setId(rs.getInt(1));
  298. employee.setAge(rs.getInt(2));
  299. employee.setDept(rs.getString(3));
  300. employee.setName(rs.getString(4));
  301. return employee;
  302. }
  303. });
  304. return employee;
  305. }
  306.  
  307. // Getting all the Employees
  308. public List<Employee> getAllEmployees()
  309. {
  310. String sql = "select * from employee1";
  311.  
  312. List<Employee> employeeList = jdbcTemplate.query(sql, new ResultSetExtractor<List<Employee>>()
  313. {
  314. @Override
  315. public List<Employee> extractData(ResultSet rs) throws SQLException, DataAccessException
  316. {
  317. List<Employee> list = new ArrayList<Employee>();
  318. while (rs.next())
  319. {
  320. Employee employee = new Employee();
  321. employee.setId(rs.getInt(1));
  322. employee.setAge(rs.getInt(2));
  323. employee.setDept(rs.getString(3));
  324. employee.setName(rs.getString(4));
  325. list.add(employee);
  326. }
  327. return list;
  328. }
  329.  
  330. });
  331. return employeeList;
  332. }
  333.  
  334. // Updating a particular Employee
  335. public void updateEmployee(Employee employee)
  336. {
  337. String sql = "update employee1 set age =?, dept=?,name=? where id=?";
  338. jdbcTemplate.update(sql, new Object[]
  339. { employee.getAge(), employee.getDept(), employee.getName(), employee.getId() });
  340. }
  341.  
  342. // Deletion of a particular Employee
  343. public void deleteEmployee(int id)
  344. {
  345. String sql = "delete employee1 where id=?";
  346. jdbcTemplate.update(sql, new Object[]
  347. { id });
  348. }
  349. }
  350.  
  351. <?xml version="1.0" encoding="UTF-8"?>
  352. <beans xmlns="http://www.springframework.org/schema/beans"
  353. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  354. xmlns:p="http://www.springframework.org/schema/p"
  355. xmlns:context="http://www.springframework.org/schema/context"
  356. xsi:schemaLocation="http://www.springframework.org/schema/beans
  357. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  358. http://www.springframework.org/schema/context
  359. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  360.  
  361. <context:component-scan base-package="com.javainterviewpoint" />
  362.  
  363.  
  364. <bean id="employeeDAOImpl" class="com.javainterviewpoint.EmployeeDAOImpl">
  365. <property name="jdbcTemplate" ref="jdbcTemplate" />
  366. </bean>
  367.  
  368. <!-- Database Configurations -->
  369. <bean id="dataSource"
  370. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  371. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  372. <property name="url" value="jdbc:mysql://localhost:3306/headway" />
  373. <property name="username" value="root" />
  374. <property name="password" value="toor" />
  375. </bean>
  376.  
  377. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  378. <property name="dataSource" ref="dataSource" />
  379. </bean>
  380.  
  381. <bean
  382. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  383. <property name="prefix" value="/WEB-INF/JSP/" />
  384. <property name="suffix" value=".jsp" />
  385. </bean>
  386. </beans>
  387.  
  388. <form method="post" action="/SpringMVCTutorial/employee.html"
  389.  
  390. @RequestMapping(value = "/employee",method=RequestMethod.POST)
  391. public ModelAndView saveEmployee(@ModelAttribute("employee") Employee employee)
  392.  
  393. <form method="post" action="employee"
Add Comment
Please, Sign In to add comment