Home>

The CRUD function was working before the persistence processing, but we are currently implementing the CRUD function involving the persistence processing from H2.
The cooperation itself was successful, but when the edit button/delete button is pressed

Edit button → The content of each item is displayed, but it is not updated after the registration process, and the content is newly added.
Delete button → The same pressed item is not deleted, and all null items are newly added to the column.

Before the persistence process, the CRUD function was working, but I would like to improve this.
We would appreciate your help. Thank you.

It's not an error, but when you press both the edit button and the delete button, the following console appears.

Hibernate: call next value for hibernate_sequence
Hibernate: insert into addresslist (address, area, name, tel, zip, id) values ​​(?,?,?,?,?,?)
Hibernate: select user0_.id as id1_0_, user0_.address as address2_0_, user0_.area as area3_0_, user0_.name as name4_0_, user0_.tel as tel5_0_, user0_.zip as zip6_0_ from addresslist user0_
Corresponding source code

DemoController.java

package com.example.samplelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

@ComponentScan
@Controller
public class DemoController {
    private final UserRepository repos;
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    SearchService service;
    @Autowired
    public DemoController (UserRepository repos, JdbcTemplate jdbcTemplate) {
        this.repos = repos;
        this.jdbcTemplate = jdbcTemplate;
    }
   /* Transition to list screen (initial screen) * /
     @SuppressWarnings ("unused")
    @RequestMapping (path = "/")
    String index (@RequestParam (required = false) String name, @RequestParam (required = false) String address,
            @RequestParam (required = false) String tel,
            @RequestParam (required = false) String zip,
            @RequestParam (required = false) String area, Model model) {
        List<User>lists = repos.findAll ();
        List<Map<String, Object >>list = jdbcTemplate.queryForList ("select * from addresslist");
         model.addAttribute ("lists", lists);
         return "users/list";
    }
    @RequestMapping (path = "/", method = RequestMethod.POST)
    String create (Model model, @ModelAttribute User users) {
        User user = new User ();
        user.setName (users.getName ());
        user.setAddress (users.getAddress ());
        user.setTel (users.getTel ());
        user.setZip (users.getZip ());
        user.setArea (users.getArea ());
       repos.save (user);
        List<User>lists = repos.findAll ();
        model.addAttribute ("lists", lists);
        return "users/list";
    }

   /* Transition to new screen * /
    @GetMapping ("/ add")ModelAndView add () {
        ModelAndView mav = new ModelAndView ();
        User data = new User ();
        mav.addObject ("formModel", data);
        mav.setViewName ("users/new");
        return mav;
    }
   /* Transition to edit screen * /
    @GetMapping ("/ edit")
    ModelAndView edit (@RequestParam int id) {
        ModelAndView mav = new ModelAndView ();
        User data = repos.findById (id);
        mav.addObject ("formModel", data);
        mav.setViewName ("users/new");
        return mav;
    }
   /* Update process * /
    @RequestMapping (path = "/ update", method = RequestMethod.POST)
    String update (Model model, @ModelAttribute User users) {
        User user = new User ();
        user.setName (users.getName ());
        user.setAddress (users.getAddress ());
        user.setTel (users.getTel ());
        user.setZip (users.getZip ());
        user.setArea (users.getArea ());
       repos.save (user);
       List<User>lists = repos.findAll ();
       model.addAttribute ("lists", lists);
        return "redirect: /";
    }

   /* Delete process * /
    @RequestMapping (path = "/ delete", method = RequestMethod.POST) // Modify here as well
    String delete (@ModelAttribute User users, Model model) {
        // COMMENT table: Delete record
        repos.deleteById (users.getId ());
        List<User>lists = repos.findAll ();
        model.addAttribute ("lists", lists);
        // Redirect to the root path ("/")
        return "redirect: /";
    }

    // Search function omitted
     }
}


User.java

package com.example.samplelist;
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 = "addresslist")
public class User {
    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column
    private int id;
    @Column (name = "name")
    private String name;
    @Column
    private String address;
    @Column
    private String tel;
    @Column
    private String zip;@Column
    private String area;
    // Getter.Setter part omitted
}


UserRepository.java

package com.example.samplelist;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long>{
    public User findById (int id);
    public void deleteById (int id);
}

index.html

<! DOCTYPE html><html xmlns: th = "http://www.thymeleaf.org"><head><meta charset = "UTF-8"><title>Users List</title><link rel = "stylesheet" href = "webjars/bootstrap/4.1.1/css/bootstrap.min.css" /><script src = "webjars/bootstrap/4.1.1/js/bootstrap.min.js"></script><style>//abridgement</style></head><body>Address book<form action = "/ search" method = "post"><input type = "text" name = "name" th: value = "${name}" size = "30"
           maxlength = "15" placeholder = "search ..." /><input type = "submit" value = "search" /></form><table border = "1"><thead><tr>    <th scope = "col">name</th>    <th scope = "col">Zip code</th>    <th scope = "col">address</th>    <th scope = "col">phone number</th>    <th scope = "col">Regional</th>    <th></th>    <th></th></tr></header><tr th: each = "obj: ${lists}" th: object = "${obj}">    <td th: text = "* {name}"></td>    <td th: text = "* {zip}"></td>    <td th: text = "* {address}"></td>    <td th: text = "* {tel}"></td>    <td th: text = "* {area}"></td>    <td>        <form action = "/ edit" method = "get">            <input type = "submit" value = "edit">            <input type = "hidden" name = "id" th: value = "${obj.id}">        </form>    </td>    <td>        <form action = "/" method = "post">            <input type = "submit" value = "delete" onclick = "return confirm ('Are you sure I want to delete?')">            <input type = "hidden" name = "id" th: value = "${obj.id}">        </form>    </td></tr></table><hr><form action = "/ add"><input type = "submit" value = "add new" /></form></body></html>


new.html

<! DOCTYPE html><html xmlns: th = "http://www.thymeleaf.org"><head><meta charset = "UTF-8"><title>Insert title here</title><link rel = "stylesheet" href = "webjars/bootstrap/4.1.1/css/bootstrap.min.css" /><script src = "webjars/bootstrap/4.1.1/js/bootstrap.min.js"></script><style>//abridgement</style></head><body>Address book registration<form method = "post" action = "/" th: object = "${formModel}"><input type = "hidden" name = "id" th: value = "* {id}"><table border = "1"><tr>  <th>name</th>  <td>    <input type = "text" name = "name" th: value = "* {name}" size = "20"
          maxlength = "20" />  </td></tr><tr>  <th>Zip code</th>  <td>    <input type = "text" name = "zip" th: value = "* {zip}" size = "20"
          maxlength = "20" />  </td></tr><tr>  <th>Address</th>  <td>    <input type = "text" name = "address" th: value = "* {address}" size = "40"
          maxlength = "40" />  </td></tr><tr>  <th>phone number</th>  <td>    <input type = "text" name = "tel" th: value = "* {tel}" size = "15"
           maxlength = "15" />  </td></tr><tr>  <th>Regional</th>  <td>    <input type = "text" name = "area" th: value = "* {area}" size = "15"
           maxlength = "15" />  </td></tr></table><hr><input type = "submit" value = "Registration" onclick = "return confirm ('Register. Are you sure?')" /></form><form action = "/"><input type = "submit" value = "return" /></form></body></html>
What I tried

Reference sitehttps://qiita.com/SSM3G/items/6b76291a4fcacb888c98

  • Answer # 1

    In new.html, which is commonly used on the registration/edit screen, it is fixed by

    , so it is a controller update process.

    @RequestMapping (path = "/ update", method = RequestMethod.POST)
    String update (Model model, @ModelAttribute User inputUser) {

    Is not executed,

    @RequestMapping (path = "/", method = RequestMethod.POST)
    String create (Model model, @ModelAttribute User inputUser)

    Is called, so I will create a new one.

    Dynamically change the URL of new.html to the update screen when transitioning by specifying the id as shown below.

    form method = "post" th: object = "${formModel}" th: action = "${formModel.id! = null?'/ update':'/'}">

    After that, the contents of the update method can be updated as it is with the value entered in the form as follows (* id is sent as hidden), so you can simply do as follows.

    @RequestMapping (path = "/ update", method = RequestMethod.POST)
    String update (Model model, @ModelAttribute User inputUser) {
      repos.save (inputUser);
      return "redirect: /";
    }