I’m very new to Thymeleaf and am trying to use a bootstrap radio button that will pass data down to java code to be used in a java application
my html code snippet:
<form action="#" method="post" th:action="@{processLogin}" th:object="${loginModel}">
<div class="btn-group" role="group" aria-label="Basic radio toggle button group">
<input type="radio" th:with="username=${username}" class="btn-check" name="btnradio1" id="a1" autocomplete="off" checked>
<label class="btn btn-outline-secondary" for="a1">GRAY</label>
<input type="radio" th:onclick="|window.location.href='/login/yellow1'|" class="btn-check" name="btnradio1" id="b1" autocomplete="off">
<label class="btn btn-outline-warning" for="b1">YELLOW</label>
<input type="radio" class="btn-check" name="btnradio1" id="c1" autocomplete="off">
<label class="btn btn-outline-success" for="c1">GREEN</label>
</div>
Controller:
@Controller
@RequestMapping("/login")
public class LoginController {
@GetMapping("/")
public String displayLoginForm(Model model) {
model.addAttribute("loginModel", new LoginModel());
return "loginForm.html";
}
@PostMapping("/processLogin")
public String processLogin(@Valid LoginModel loginModel, BindingResult bindingResult, Model model) {
return "loginResults.html";
}
}
Model:
package com.sam.models;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public class LoginModel {
private String username;
private String password;
public LoginModel() {
super();
// TODO Auto-generated constructor stub
}
public LoginModel(String username, String password) {
super();
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "LoginModel [username=" + username + ", password=" + password + "]";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I don’t really care what the username is as long as it interacts with a button press, I have tried using @GetMapping to a different url but that doesn’t really work for the webpage I’m trying to create as it would have multiple of these buttons that would have to have their data passed to my java program. Is it even possible what I’m trying to do? I have a working version of this program that passes data down upon entering it into a field and then clicking a submit button, but is it possible to just have the button press be passed and update a variable in my model onclick?
Sam Wobschall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.