How to mark checkbox in Freemarker template - java

I'm using Freemarker template (java) and I need to show checked checkboxes. I expect to see a checkbox marked with a regular checkmark (unicode "U+2714"), but instead of I see that my checkbox marked with question mark (U+003F). I need a regular checkmark, or even "X" is good, but not question mark "?". My code now is:
<#list labels as checklabel>
<div>
<input type="checkbox" id="checklabel" name="checklabel" checked="true">
<label for="checklabel">${checklabel}</label>
</div>
</#list>
Maybe somebody knows what I do not do right and can help me?

Write like this:-
<input type="radio" id="checklabel" name="checklabel" checked>
you have to write only checked and the type to radio.

Related

Spring, Java, HTML: How can I populate an HTML form with values?

I want to write a part of a website that lets the user alter the data of a pre existing book. For that, I am trying to use a form which works fine. I just can't figure out how to get the form to display data in the editing fields so the user doesn't have to enter everything again but can simply change some details. My HTML code looks like this:
<form method="post" role="form" class="ui form" id="bookForm" th:action="#{/editBook}" th:object="${bookForm}">
<div class="field">
<label for="name">Book</label>
<input id="name" name="name" th:field="*{name}" th:errorclass="fieldError" type="text" required="required"/><br/>
</div>
I include some more code about errors and other things but this is basically where I want the form not only to pass values to my java file but also to take values about the book and display them in the editing fields.
I think I need to pass the book that the user wants to edit into this form but I'm not sure how. I have tried:
<input type="hidden" id="currentBook" name="currentBook" th:value="${currentBook}"/>
right before the "div" statement and then passing the "currentBook" into HTML with
model.addAttribute("currentBook", currentBook);
in my #GetMapping method of that website. I then changed the "input" statement in my field as well to
<input id="name" name="name" th:field="*{name}" th:value="${currentBook.name}" th:errorclass="fieldError" type="text" required="required"/><br/>
currentBook.name will give me the name of that book just not within this context. Does anyone know what I'm doing wrong and how it will work?
Thank you in advance!

Thymeleaf checkbox can't default to checked when using th:field?

Does anyone know how to add a default setting of checked in a Thymeleaf, SpringBoot, Java project, to an input field for a Single checkbox?
I have to use th:field because I am later submitting this form via email and if I use th:name with the checked box default, the default works but email doesn't.
Any suggestions?
<label>
<input type="checkbox" id="serviceLevel" th:field="*{serviceLevel}" th:checked="${serviceLevel}"/>
Affiliate Serviced</label>
Email html code to pull value on email:
<tr class="emailRow">
<h3>Direct: </h3>
<td class="bodycopy" th:text="${directBind.directBox}">
</td>
</tr>
I have this variable set as private Boolean directBox in the model.
I've tried everything: th:checked=checked, value=true, value=checked, set the value and checked ="${directBox}" none of this works.
Is there a solution out there?
Figured out a way to use th:field and have it default to checked. Have to set it in my controller, like Gustavo mentioned. Code example below.
...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";

styles not applied on validation form using thymeleaf and bootstrap

Trying to add text with a style on error validation in a bootstrap form
This is part of the form:
<label th:text="#{name}"
class="col-form-label col-form-label-sm"></label>
<input
type="text" class="form-control form-control-sm"
th:field="*{name}" />
<span
th:if="${#fields.hasErrors('name')}" th:errors="*{name}"
th:class="invalid-feedback">Here there is an error</span>
I get the message on validation error, but without styles.
If I debug I see the class with the style:
<span class="invalid-feedback">Here there is an error</span>
I have tried with severals styles like help-block but no way.
I'm using bootstrap4.0.0-alpha.6
Any idea?
Thanks
In case you are still interested.
Bootstrap's current validation docs give you three approaches: client-side custom validation, browser defaults and server-side validation.
From your post I will assume you're using server side, meaning that you are sending the data to be validated in your server code for then showing the error fields when the form is re-rendered.
In that case, remember that for bootstrap's styles to kick in, a certain html structure is expected from your code, example:
<input th:field ="*{email}" type="email" class="form-control"
th:classappend="${not #lists.isEmpty(#fields.errors('email'))} ? is-invalid"
required>
<div class="invalid-feedback">
<p th:each="error: ${#fields.errors('email')}" th:text="${error}">Invalid data</p>
</div>
(I believe a span tag will work too for <div class="invalid-feedback">)
That is a minimal necessary structure for the error styles to work. What happens:
Initially, invalid-feedback is assigned display: none
The form is submitted, if it has an error it'll be re-rendered and it's up to the developer to provide a mechanism which will include is-invalid in the class attribute of every <input class="form-control"/> with an error. In the previous code, th:classappend="${not #lists.isEmpty(#fields.errors('email'))} ? is-invalid" is responsible for that to happen. (I'm new to Spring and Thymeleaf, so there could be a neater code for this)
Declaration .form-control.is-invalid~.invalid-feedback in _forms.scss will kick in and assign display: block to <div class="invalid-feedback">, thus showing each field's error message (s).
That should be enough to show the error (s) under each field and highlight them in red.
HIH

How do I fetch input from select box using Thymeleaf

I have been doing school's project recently and started learning Java Web/Spring/Bootstrap and stuff only a week ago, so please do forgive and correct me if I got any idea wrong.
So I was working on some webpage following an online tutorial and it's really great that I can fetch user's input by using Thymeleaf tags like codes below
<div class="ip input-group" align="center">
<input id="username" type="text" class="form-control" name="username" placeholder="Username" th:value="*{username}"/>
</div>
Though I'm having a hard time trying to fetch input from select box or radio button like codes below (am I doing anything wrong here?)
<div class="input-group">
<select class="btn btn-default" th:value="*{sex}">
<option>Male</option>
<option>Fmale</option>
<option>Other</option>
</select>
</div>
Since it's able to fetch input from simple input area, I'm thinking that there should be a way to acquire input or data from select box or radio button by using Thymeleaf?
th:field tag should be in <select> tag, but it does not exist.
th:value tag should be in <option> tag, not in <select> tag.
As described in thymeleaf docs:
Select fields have two parts: the tag and its nested
tags. When creating this kind of field, only the tag has to
include a th:field attribute, but the th:value attributes in the
nested tags will be very important because they will provide
the means of knowing which is the currently selected option (in a
similar way to non-boolean checkboxes and radio buttons).
Code snipped in source

Setting jsp checkbox with a value from database

Ok. I'm making a java web app with a database backend to do some CRUD on some data. When the edit button is clicked next to an item, it navigates to a form with the current data for editing. One of the fields is boolean and I would like to display it as a checkbox so that True makes it checked and False leaves it unchecked.
I have tried many different variations none seem to work. Here are some examples where <%= action.get("stable")%> returns a string with either True or False
<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>
<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>
<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>
So how do you set a check box to checked/unchecked depending on the string returned with action.get("stable")
Thank you for any help sorry if the question is a bit trivial.
I used this, and it worked perfectly.
<input type="checkbox" <c:if test="${item.estado==2}">checked=checked</c:if> class="switch-input" >
The correct markup for a checked checkbox is checked="checked". If it's not checked, the checked attribute must not be present at all.
You should generate it using the JSTL and the JSP EL, because scriptlets are something from the past which should not be used in JSPs for years. See How to avoid Java code in JSP files?.
This would of course need some refactoring so that the action bean has a regular isStable() method returning a boolean, which would be much cleaner. But anyway, here's how it would work using your existing code :
<input type="checkbox" name="stable" <%
if ("True".equals(action.get("stable"))) {
out.print("checked=\"checked\"");
} %>/>
Note that all attributes should also be surrounded by quotes.
You need to set checked attribute of <input type="checkbox"/>
Edit:
<input type="checkbox" <%=action.get("stable") ? "checked='checked'" : "" %> />

Categories