I would like to improve the error such as No value present that appears when I update with Spring.

Asked 2 years ago, Updated 2 years ago, 464 views

"When I change the value to what I want to update on the edit screen and press the execution button, an error such as ""No value present"" appears, so I would like to make this improvement."

I am thinking of updating on Springboot by referring to the site below.

SpringBoot Getting Started vol.14: Add Edit and Remove Features

This site has one table update, but what I want to do is update two tables at the same time.There may be an accident due to that difference, but I can't find out the cause.
Could you teach me?

control class

@PostMapping("search")
  public String index(
      @RequestParam String id,
      @RequestParam String name,
      @RequestParam String kana,
      @ModelAttribute CreateForm createForm,
      @Validated @ModelAttribute SearchForm searchForm,
      BindingResult result,
      Model model,
      HttpServletRequest) {
    List<Userdetail>search=equipmentRepository.find(id,name,kana);
    BeanUtils.copyProperties (createForm, search);
    HttpSession session=request.getSession();
    session.setAttribute("search", search);
    model.addAttribute("search", search);
    if(result.hasErrors()){
      return "search";
    }
    return "searchout";
  }

@ PostMapping (path="update", params="update")
  String update(@RequestParamStringid,@ModelAttributeCreateForm){
    Optional<User>opt=sevi.selectById(id);
    User = opt.get();
    BeanUtils.copyProperties(u, createForm);
    return "update";
  }

  @ PostMapping (path="update", params="back")
  String back() {
    return "redirect: /";
  }

  @ PostMapping (path="update", params="register")
  // Point 2
  String register(@RequestParam String id, @Validated @ModelAttributeCreateForm createForm, BindingResult result, Model model) {
      if(result.hasErrors()){
          return update (id, createForm);
      }
      User user = new User();
      // Userdetail userdetail=new Userdetail();
      BeanUtils.copyProperties (createForm, user);
     // BeanUtils.copyProperties (createForm, userdetail);
      sevi.update(user);
      // sevi.updatee(userdetail);
    model.addAttribute("searchForm", newSearchForm());
      return "search";
  }

I started using two tables from around the method index.
Below is the method index listed in the repository class.
The contents of equipmentRepository.find.

@Query("SELECT DISTINCTE FROM USERDETAILE INNER JOIN e.user WHERE e.user.id LIKE CONCAT(:id, '%') and e.user.name LIKE CONCAT(:name, '%) and e.user.kana LIKE CONCAT(:kana, '%)ORDER BY e.id")
    List<Userdetail>find(@Param("id") String id,
        @Param("name") String name,
        @Param("kana") String kana);

table

@Entity
@Table(name="user")
@Getter
@Setter
@ AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

  @Id
  @Column(name="id")

  // @GeneratedValue (stringy=GenerationType.IDENTITY)
  private String id;

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

  @OneToMany (mappedBy="user", cascade=CascadeType.ALL)
  private List <Userdetail>userdetail;

}
@Entity
@Table(name="userdetail")
@Getter
@Setter
@ AllArgsConstructor
@NoArgsConstructor
public class Userdetail implements Serializable {
  @Id
  @GeneratedValue (strategy=GenerationType.AUTO)
  private int no;
  private String id;
  private String birth;
  private String club;
  @ManyToOne
  @JoinColumn(name="ID", insertable=false, updateable=false)
  private user;
}

Edit Screen (update.html)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<!--scripts import-->
<scriptth:src="@{/webjars/jquery/3.5.1/jquery.min.js}"></script>
<script
    th:src="@{/webjars/bootstrap/4.4.1-1/js/bootstrap.bundle.min.js}"></script>
<!--style import-->
<link th:href="@{/webjars/bootstrap/4.4.1-1/css/bootstrap.css}"
    rel="stylesheet"/>
<link th:href="@{/css/login.css}"rel="stylesheet"/>
<metacharset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div class="col-sm-5">
        <div class="page-header">
            <h1>Edit Screen</h1>
        </div>
        <formth:action="@{/update}"th:object="${createForm}"method="post">
            <table class="table table-border table-hover">
                <!--User ID-->
                <tr>
                    <th class="active col-sm-3">User ID</th>
                            <td><input type="text" class="form-control" name="id"
                                th —field="*{id}"
                                th:classappend="${#fields.hasErrors('id')}?'is-invalid'">
                            <span class="text-danger" th:if="${#fields.hasErrors('id')}"
                                th —errors="*{id}">/span></td>
                </tr>
                <!--Name-->
                <tr>
                    <th class="active">Name</th>
                    <td>
                            <input type="text" class="form-control" name="name"
                                th —field="*{name}"
                                th:classappend="${#fields.hasErrors('name')}?'is-invalid'">
                            <span class="text-danger" th:if="${#fields.hasErrors('name')}"
                                th —errors="*{name}"></span>
                    </td>
                </tr>
                <!--- Kana -->
                <tr>
                    <th class="active">Kana</th>
                    <td>
                            <input type="text" class="form-control" name="kana"
                                th —field="*{kana}"
                                th:classappend="${#fields.hasErrors('kana')}?'is-invalid'">
                            <span class="text-danger" th:if="${#fields.hasErrors('kana')}"
                                th —errors="*{kana}"></span>
                    </td>
                </tr>
                <!--Date of birth-->
                <tr>
                    <th class="active"> Date of birth (yyyy/mm/dd)</th>
                    <td>
                            <input type="text" class="form-control" name="birth"
                                th —field="*{birth}"
                                th:classappend="${#fields.hasErrors('birth')}?'is-invalid'">
                            <span class="text-danger" th:if="${#fields.hasErrors('birth')}"
                                th —errors="*{birth}"></span>
                    </td>
                </tr>
                <!--- Committee -->
                <tr>
                    <th class="active"> Committee</th>
                    <td>
                            <input type="text" class="form-control" name="club"
                                th —field="*{club}"
                                th:classappend="${#fields.hasErrors('club')}?'is-invalid'">
                            <span class="text-danger" th:if="${#fields.hasErrors('club')}"
                                th: errors="*{club}"></span>
                    </td>
                </tr>
            </table>
            <input type="submit" class="btn btn-outline-dark mt-3" name="back" value="back">
            <input type="submit" class="btn btn-primary mt-3" name="register" value="run">
            <input type="hidden" name="id" th:value="${param.id[0]}">
        </form>
    </div>

</body>
</html>

searchout.html

before transitioning to the edit screen (update.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="utf-8"/>
<title> Search Screen </title>
</head>
<body>
    <h2>Find Employee Information</h2>
    <h3> 前方Search forward match</h3>
    <form action="#"th:action="@{/search}"th:object="${searchForm}"
        method="post" >
        <table>
            <tr>
                <td>id:<input type="text" name="id" th:field="*{id}"/>br>
                    <spanth:if="${#fields.hasErrors('id')}"th:errors="*{id}">/span>
                </td>
            </tr>
            <tr>
                <td>Name:<input type="text" name="name" th:field="*{name}"/>br>
                    <spanth:if="${#fields.hasErrors('name')}"th:errors="*{name}">/span>
                </td>
            </tr>
            <tr>
            <tr>
                <td>Kana:<input type="text" name="kana" th:field="*{kana}"/>br>
                    <spanth:if="${#fields.hasErrors('kana')}"th:errors="*{kana}"></span>
                </td>
            </tr>
            <tr>
                <td><button type="submit">Search</button></td>
            </tr>
        </table>
    </form>
    <button type="submit" class="btn btn --blue"
        onClick="location.href='http://localhost:8080/create'">New Registration </button>

    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Kana</th>
            <th>Date of birth</th>
            <th> Committee </th>
            <th>Operation </th>
        </tr>
        <trth:each="search:${search}">
            <tdth:text="${search.user.id}">
            <tdth:text="${search.user.name}">
            <tdth:text="${search.user.kana}">
            <tdth:text="${search.birth}">
            <tdth:text="${search.club}">
            <td>
                <formth:action="@{/update}"method="post">
                    <input type="submit" name="update" value="edit">
                    <input type="hidden" name="birth" th:value="${search.birth}"><!--← Then, when you transition to update.html -->
                    <input type="hidden" name="club" th:value="${search.club}"><!--- text box contains characters -->
                    <input type="hidden" name="id" th:value="${search.user.id}">
                </form>
            </td>
            <td>
                <formth:action="@{/delete}"method="post">
                    <input type="submit" name="delete" value="delete">
                    <input type="hidden" name="id" th:value="${search.user.id}">
                </form>
            </td>
    </table>
</body>
</html>

Class of Service

public void update(User user){
    userRepository.save(user);
  }
  public void update (Userdetail userdetail) {
    userdetailRepository.save(userdetail);
  }

update.html screen when transitioning from searchout.html to update.html

This specification contains a value from the beginning

Enter a description of the image here
[!][Enter a description of the image here][2][2]

java mysql spring spring-boot

2022-09-30 21:59

1 Answers

Error such as "No value present"

I think this is a NoSuchElementException message that runs the Optionsal#get() method and is sent if there is no value.

This method is used only in one part of the question code, so I think the exception is here (if not, please add it to the questionnaire):

@PostMapping(path="update", params="update")
  String update(@RequestParamStringid,@ModelAttributeCreateForm){
    Optional<User>opt=sevi.selectById(id);
    User = opt.get();
    BeanUtils.copyProperties(u, createForm);
    return "update";
  }

sevi.selectById(id) is probably empty when the User search with the id key is missing.

Implementation similar to the following to process only if there is an applicable User:

Optional<User>opt=sevi.selectById(id);
    opt.ifPresent(u-> BeanUtils.copyProperties(u, createForm));
    return "update";

Note: Java Optional


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.