I want to show data with spring on td html - spring

<td >
<select class="form-control adj_type" th:field="*{type_id}" />
<option th:each="Adjustment : ${listAdjType}"
th:value="${Adjustment.id}"
th:text="${Adjustment.adj_name}"
th:if="${Adjustment.deleted_status}"
name="adj_type" id="adj_type">
</option>
</select>
</td>
this is my error message
Neither BindingResult nor plain target object for bean name 'type_id' available as request attribute

Related

th:field syntax inside th:each is incorrect

I am using spring boot and thymeleaf combination for my project.
Following is the code snippet for Get controller -
#GetMapping("/split")
public String VerticalSplit(Model model) {
MessageContent messageContent0 = new MessageContent();
MessageContent messageContent1 = new MessageContent();
List<MessageContent> messageContentList = new ArrayList<MessageContent>();
messageContentList.add(0, messageContent0);
messageContentList.add(1, messageContent1);
DisplayMessage displayMessage = new DisplayMessage();
model.addAttribute("displayList", displayService.getAllDisplays());
model.addAttribute("groupList", groupService.getAllGroups());
model.addAttribute("messageContentList", messageContentList);
model.addAttribute("displayMessage", displayMessage);
return "combo_vertical_split";
}
And respective html code is as follows -
<html>
<head></head>
<body>
<div class="col-12">
<label class="form-label"> Name of Message </label>
<input type="text" name="name" class="form-control" placeholder="Enter Name Of Message" required="required" th:value="${displayMessage.name}" />
</div>
<hr />
<div th:each="messageContent, iStat : ${messageContentList}">
<h6 align="center" th:text="'Section '+${iStat.count}"></h6>
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="${messageContent.type}"> <option class="form-select" th:value="Text">Text</option> <option class="form-select" th:value="Image">Image</option> <option class="form-select" th:value="Video">Video</option> </select>
<br />
</div>
</div>
</body>
</html>
When I am trying to load the page, i am getting error as follows -
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'messageContent' available as request attribute
The point which I am not getting is that even though I have declared the messageContent in controller, why am i getting the above mentioned error.
I tried multiple syntax of
th:field="${messageContent.type}"
as follows -
th:field="*{messageContent.type}" //its silly as i have not declared the form object but still i tried
th:field="${messageContentList[__${iStat.index}__].type}"
and few other combinations as well, but nothing worked and almost everytime i got the same error.
You're getting the Neither Bindingnor plain target... error because you have used the th:field in your HTML, but you haven't defined an object from which to bind to it.
You can add your input elements in a form element and add a th:object attribute bound to your model's messageContent object.
You can do something similar to this:
<form th:object="${messageContent}">
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="*{type}">
<option class="form-select" th:value="Text">Text</option>
<option class="form-select" th:value="Image">Image</option>
</select>
<br />
</div>
</form>

Select field validation

<div class="form-group">
<label for="category-id">Category</label>
<select id="category-id" class="form-control {{$errors->get('category_id') ? 'is-invalid' : ''}}" name="category_id">
<option value="177">Select a category</option>
#foreach ($categories as $category)
<option value="{{$category['id']}}">{{$category['title']}}</option>
#endforeach
</select>
#if ($errors->has('category_id'))
<div class="invalid-feedback"><strong>{{$errors->first('category_id')}}</strong></div>
#endif
</div>
The category_id is defined as the required field in my Request php file ('category_id' => 'required')
But when I submit the form without any input to any field, all fields generate error messages by highlighting the field, except this SELECT field.
It passes the validation because your are sending a value in that field.
Change this
<option value="177">Select a category</option>
to
<option selected disabled>Select a category</option>
then it should not pass the validation.

How can we add 'required' attribute to a dropdown box in jsp page

How can we add required attribute to a dropdown box in a JSP page? I added it in normal fields using Required = required keyword. But in dropdown, it is not working.
<div class="form-group row">
<div class="col-md-3">
<label class="form-control-label" for="customer_category">Category</label>
</div>
<div class="col-md-9">
<form:select class="select2" id="customer_category" path="category.id">
<form:option value="0">Select a Category</form:option>
<c:forEach items="${listCustomerCategory}" var="category">
<form:option value="${category.id}">${category.name}</form:option>
</c:forEach>
</form:select>
</div>
</div>
Finally i found the answer, I just Changed this line
<form:option value="0">Select a Category</form:option>
to
<option value="">Select a Category</option>
Now it is working..

Spring how to use select with Thymeleaf [duplicate]

I want to create a drop down menu that allows a client to search users by a field specified in the drop down menu. For example, search by state, search by city, etc.
This is what I have so far:
<p>Search options:</p>
<form action="#" th:action="#{/get/{value}" method="get">
<select>
<option th:value="AllUsers">Search all users</option>
<option th:value="ByUsername">Search by user name</option>
<option th:value="ByFirstname">Search by first name</option>
<option th:value="ByLastname">Search by last name</option>
<option th:value="ByAddress">Search by address</option>
<option th:value="ByCity">Search by city</option>
<option th:value="ByState">Search by state</option>
<option th:value="ByZipCode">Search by zip code</option>
<option th:value="ByPhoneNumber">Search by phone number</option>
<option th:value="ByEmail">Search by email</option>
</select>
<input type="text" th:field="value" name="searchField"/>
<input type="submit" value="Search" name="searchButton"/>
</form>
I'm just not sure how to connect the action and the value tag of the currently selected item in the drop down list to specify the URI. If a user selects, search by state, and he enters in "Maryland", how do I specify the corresponding URI tag?
This would be my method in Spring that executes the action:
#RequestMapping(value = "/get/ByState", method = RequestMethod.GET)
public String getByState() {
// ...
}
#RequestMapping(value = "/get/ByCity", method = RequestMethod.GET)
public String getByCity() {
// ...
}
Because the accepted answer is not using Thymeleaf and this question is top in Google I'm adding my solution here.
In my situation statues is a list of Enum values. Model attribute is populated as usually you do in Spring:
mav.addObject("statuses", EnumSet.allOf(Status.class));
Group has a filed of type Status (the enum).
<div class="form-group row">
<select class="form-control" id="status" name="status">
<option th:each="stat : ${statuses}"
th:value="${stat}"
th:text="${stat}"
th:selected="${stat.equals(group.status)}"/>
</select>
</div>
This automatically populates the list and selects value that is selected in my Group instance:
You have just the required answer in this link:
http://fruzenshtein.com/spring-mvc-form-select-tag/

the value of form:select didn't get selected from controller

i set the value of form:select but in the result it didn't get selected.
My controller:
contactBF.setNom(contact.getNom());
contactBF.setQualite("130");
model.put("contact", contactBF);
model.put("qualities", [...]);
My jsp page:
<form:form id="contactform" modelAttribute="contact" action="">
<form:input path="nom" type="text"/>
<form:select path="qualite">
<option value=""> ... </option>
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
</form:select>
</form>
the form:input get fill with the right value and form:select is loaded with all item but the right value is not selected !
i know this solution work:
<option value="${qualite.id}" ${(qualite.id == contact.qualite) ? 'selected' : ''}>${qualite.nom}</option>
But it wil be a lot of test.
i replace :
<c:forEach items="${qualities}" var="qualite" >
<option value="${qualite.id}" >${qualite.nom}</option>
</c:forEach>
By :
<form:options items="${qualiteList}" itemValue="id" itemLabel="nom" />
And it's work ;)

Resources