Skip to content
Advertisement

Retrieving a boolean value from webpage using Thymeleaf + Spring boot with Java

public class SuperHuman {
    private int id;
    
    @NotBlank(message = "Name must not be empty")
    @Size(max = 25, message = "Name must be less than 25 characters")
    private String name;
    
    @NotBlank(message = "Description must not be empty")
    @Size(max = 100, message = "Name must be less than 100 characters")
    private String description;
    
    // The hero boolean will have a default value
    private boolean hero;

    private List<Power> powers;

Above is my Super Human model. Please take note of the boolean variable named hero.

    @PostMapping("addSuperHuman")
    public String addSuper(HttpServletRequest request){
        String[] powerIds = request.getParameterValues("powers");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        
        // Request for boolean retrieval code here
        
        List<Power> powers = new ArrayList<>();
        for(String powerId : powerIds){
            powers.add(powerDao.getPowerById(Integer.parseInt(powerId)));
        }
        SuperHuman superHuman = new SuperHuman();
        superHuman.setName(name);
        superHuman.setDescription(description);
        // superHuman.setHero();
        superHuman.setPowers(powers);
        
        superDao.addSuperHuman(superHuman);
        
        return "redirect:/superHumans";
    }

Above is the controller method I am using to retrieve form data via servlet request. The commented portions are to be replaced with actual working code, once discovered.

```
                    <div class="form-group row pb-3">
                    <label for="affiliation" class="col-3 col-form-label">
                        <Strong>Affiliation:</Strong></label>
                    <div class="col-2">
                            <select id="heroOrVillian" name="heroOrVillian" class="form-control">
                                <option value="1">Hero</option>
                                <option value="2">Villian</option>
                            </select>
                    </div>
                </div>
```

Above is the final piece. This is a excerpt from HTML form submission page for adding the super human object into a SQL database. The issue I am currently faced with is this; I am presenting two options:

1.) You’re either a Hero (The boolean value for this is TRUE)

2.) You’re either a Villian (The boolean value for this is FALSE)

I’m unsure how to return boolean data through html input. I’ve googled this question and have read through the thymeleaf docs and have not been able to find the answer for myself.

                       <div class="col-2">
                                <select id="heroOrVillian" name="heroOrVillian" class="form-control">
                                    <option value="1">Hero</option>
                                    <option value="2">Villian</option>
                                </select>
                        </div>

This is the portion I am working on, I’ve tried using th:field ${SuperHuman.hero} and setting the option values to th:value=”true” and false, respectively, but I’ve only received errors in return.

I just want to know if there is a way to return a boolean true or false through an input field where the user can select if they are either a hero or villian and receive said data in java where i can submit it into an SQL database.

Advertisement

Answer

you must change your code like below

 <select id="heroOrVillian" name="heroOrVillian" class="form-control">
                            <option value="0">Hero</option>
                            <option value="1">Villian</option>
                        </select>
Advertisement