Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"></meta>
- <title>CRUD</title>
- <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" />
- </head>
- <body>
- <h1>Edit or Delete User</h1>
- <table class="table table-hover">
- <thead>
- <tr>
- <th>Id</th>
- <th>User</th>
- </tr>
- </thead>
- <td><span th:text=${user.id}></span></td>
- <td><span th:text=${user.name}></span></td>
- </table>
- <hr>
- <form action="../update" method="post">
- <div id="userEdit"></div>
- <div>
- <label for="userpassword">Password:</label> <input type="text"
- id="userpassword" name="user_password" /> <input type="hidden"
- th:value="${user.id}" name="id" />
- </div>
- <div class="button">
- <button type="submit">Update</button>
- </div>
- </form>
- <hr>
- <form action="../delete" method="delete">
- <div class="button">
- <input type="hidden" th:value="${user.id}" name="id" />
- <button type="submit">Delete</button>
- </div>
- </form>
- </body>
- </html>
- package com.br.crud.controllers;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.View;
- import org.springframework.web.servlet.mvc.support.RedirectAttributes;
- import com.br.crud.dao.UserDAO;
- import com.br.crud.model.User;
- import com.fasterxml.jackson.annotation.JsonCreator.Mode;
- @ComponentScan
- @Controller
- public class UserController {
- @Autowired
- private UserDAO dao;
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public String index() {
- return "index";
- }
- @RequestMapping(value = "/", method = RequestMethod.POST)
- public ModelAndView login(@RequestParam("user_name") String name, @RequestParam("user_password") String password) {
- for (int i = 0; i < dao.listUsers().size(); i++) {
- if (dao.listUsers().get(i).getName().equals(name)
- && dao.listUsers().get(i).getPassword().equals(password)) {
- return new ModelAndView("redirect:crud");
- }
- }
- return new ModelAndView("index");
- }
- @RequestMapping(value = "crud", method = RequestMethod.GET)
- public String userList(Model model) {
- List<User> users = dao.listUsers();
- model.addAttribute("users", users);
- return "userList";
- }
- @RequestMapping(value = "crud", method = RequestMethod.POST)
- public ModelAndView saveUser(@RequestParam("user_name") String name, @RequestParam("user_password") String password,
- RedirectAttributes redirectAttributes) {
- User user = new User(name, password);
- dao.save(user);
- return new ModelAndView("redirect:crud");
- }
- @RequestMapping(value = "edit/{id}")
- public ModelAndView edit(@PathVariable("id") Long id) {
- ModelAndView modelAndView = new ModelAndView("editUser");
- User user = new User();
- user = dao.find(id);
- modelAndView.addObject("user", user);
- return modelAndView;
- }
- @RequestMapping(value = "update", method = RequestMethod.POST)
- public ModelAndView updateUser(@RequestParam("id") Long id, @RequestParam("user_password") String newPassword) {
- User user = dao.find(id);
- User newUser = dao.find(user.getId());
- newUser.setPassword(newPassword);
- dao.save(newUser);
- return new ModelAndView("redirect:crud");
- }
- @RequestMapping(value = "delete", method = RequestMethod.DELETE)
- public ModelAndView deleteUser(@RequestParam("id") Long id) {
- User deletedUser = dao.find(id);
- dao.delete(deletedUser);
- return new ModelAndView("redirect:crud");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement