'How to bind input radio to thymeleaf + spring boot
I'm making a springboot + thymeleaf app with books and i'm trying to add a rating that displays with stars with a input radio but I can't get the input to take the passed value, even tho I can see the passed value if I do it with a span.
This is my java class
package org.factoriaf5.libritos.repositories;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Entity
@Table(name = "books")
public class Book implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private String category;
private String imageURL;
private int rating;
private String startingDate;
private String finishDate;
private String review;
private boolean finished;
public Book() {
}
public Book(String title, String author, String category, String imageURL, int rating, String startingDate, String finishDate, String review, boolean finished) {
this.title = title;
this.author = author;
this.category = category;
this.imageURL = imageURL;
this.rating = rating;
this.startingDate = startingDate;
this.finishDate = finishDate;
this.review = review;
this.finished = finished;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getStartingDate() {
return startingDate;
}
public void setStartingDate(String startingDate) {
this.startingDate = startingDate;
}
public String getFinishDate() {
return finishDate;
}
public void setFinishDate(String finishDate) {
this.finishDate = finishDate;
}
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
", category='" + category + '\'' +
", imageURL='" + imageURL + '\'' +
", rating=" + rating +
", startingDate='" + startingDate + '\'' +
", finishDate='" + finishDate + '\'' +
", finished=" + finished +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Objects.equals(id, book.id) && Objects.equals(title, book.title) && Objects.equals(author, book.author) && Objects.equals(category, book.category);
}
@Override
public int hashCode() {
return Objects.hash(id, title, author, category);
}
}
HTML for the rating
<div class="rating">
<form >
<fieldset>
<span class="star-cb-group">
<input type="radio" id="rating-5" name="rating" value="5"/><label for="rating-5">5</label>
<input type="radio" id="rating-4" name="rating" value="4" checked="checked"/><label for="rating-4">4</label>
<input type="radio" id="rating-3" name="rating" value="3"/><label for="rating-3">3</label>
<input type="radio" id="rating-2" name="rating" value="2"/><label for="rating-2">2</label>
<input type="radio" id="rating-1" name="rating" value="1"/><label for="rating-1">1</label>
<input type="radio" id="rating-0" name="rating" :value="0" class="star-cb-clear"/><label for="rating-0">0</label>
</span>
</fieldset>
</form>
</div>
I also have springboot security added
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/login", "books/rating/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().permitAll()
.and().logout().permitAll();
}
}
And tried adding a putMapping
@PutMapping("/books/{id}/rating")
public Book addRating(@RequestBody Rating rating, @PathVariable Long id) {
Book book = bookRepository.findById(id).orElse(null);
book.setRating(rating.getScore());
return bookRepository.save(book);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
