Home > Net >  Could not create an action for button (Java, Spring, Thymeleaf)
Could not create an action for button (Java, Spring, Thymeleaf)

Time:07-18

I have a list with books (object). I try to add the button for each books, which redirect page "/bookList" to "/bookUpdate", that contain a form with editable fields, that were filled information about specific book from database. But I could not open this form because of the error and I could not find a mistake.

Error: [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "(id=${books.id})'"]

bookList.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
layout:decorate="~{fragments/main_layout}">
<head>
<title>Books</title>
</head>
<body>

<div layout:fragment="content" >
    <form action="/bookList" >
        <div  >
            <input type="text"  name="name"
                placeholder="Search book" /> <input type="submit" value="Search"
                 />
        </div>
    </form>
    <div >
        <div >
            <ul th:each="books:${book}" style="list-style: none; padding-left: 10px;">
                <li><b>Topic:</b> <span th:text="${books.topic}"></span></li>
                <li><b>Description:</b><p th:text="${books.description}"></p></li>
                <li><b>Link:</b> <span th:text="${books.link}"></span></li>
                <br>
                <form          
 th:action="@{/bookUpdate/(id=${books.id})'}" th:object="${books}" method="get">
                    <input  type="hidden" />
                    <button  type="submit"     
>Edit</button>
                </form>
                <hr>
            </ul>
        </div>
    </div>
</div>
</body>
</html>

BookController

@Controller
public class BookController {

@Autowired
private BookService service;

@GetMapping("/bookList")
public String bookList(Model model, @RequestParam(defaultValue = "") String topic) {
    model.addAttribute("book", service.findByTopicSearch(topic));
    return "views/bookList";
}

 @GetMapping("/bookUpdate/{id}")
 public String bookListUpdate(@PathVariable (value = "id") Integer id, Model model, @Valid 
  BookDto book) {
    model.addAttribute("book", service.findById(id));
    return "/bookUpdate";
 }

Book

@Entity
@Table(name = "book")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String topic;
private String description;
private String link;

public Book() {

}

public Integer getId() {
    return id;
}

public void setId(Integer book) {
    this.id = book;
}

public String getTopic() {
    return topic;
}

public void setTopic(String topic) {
    this.topic = topic;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

}

CodePudding user response:

try this in the form action

th:action="@{'/bookUpdate/' ${books.id}}"
  • Related