Spring Thymeleaf th:selected on EnumList with multiple selection - spring

I have this enum
enum Types{
A, B
}
I have a form class
public class MyForm {
private Types[] types;
//getter setters
}
here is my form with select
<form th:action="${#httpServletRequest.requestURI}" th:object="${myForm}" method="POST" id="form">
<select name="types" multiple="" id="testSelect"
th:each="type : ${T(com.test.Types).values()}"
th:value="${type}"
th:text="${type}"
th:selected="*{types != null AND #arrays.contains(types, type)}"
>
</select>
</form>
here is the error i am getting.
Property or field 'type' cannot be found on object of type 'com.test.MyForm' - maybe not public or not valid?

First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.
<select name="types" multiple="" id="testSelect">
<option th:each="type : ${T(com.test.Types).values()}"
th:value="${type}" th:text="${type}"
th:selected="${types != null AND #arrays.contains(types, type)}">
</option>
</select>
One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.

The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:
public class MyForm {
private List<Types> types = new ArrayList<Types>();
//getter setters
}
Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.
Then should be able to simply skip th:selected logic...
<select th:field="*{types}" multiple="multiple" id="testSelect">
<option th:each="type : ${T(com.test.Types).values()}"
th:value="${type}" th:text="${type}">
</option>
</select>
Thymeleaf will do the magic for you ;-)

Related

Spring Boot Get Value of object from select using thymeleaf

So I have this project, and I want to return an object, from the previous choice of 2 select items. How can I do it? for example:
<select class="form-control" id="cityPicker1">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
<select class="form-control" id="cityPicker2">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
I want to display an item from another item list which has a number1 field and a number2 something like ticket.number1 and ticket.number2 but they have to match the values from the previous selects. I can't seem to find anything. Any ideas?
This is how I set it up in the controller:
#GetMapping("/tickets")
public String viewTicketsPage(Model model) {
List<City> listCities = cityRepo.findAll();
List<Ticket> listTickets = ticketRepo.findAll();
model.addAttribute("listCities", listCities);
model.addAttribute("listTickets", listTickets);
return "tickets";
}
I need to clarify a few things about your question.
1 - did you manage to get the checkboxes to work?
2 - can you send the selected data to the controller?
you need a controller to receive data from the page, send this data to the repository to fetch other data..
I believe you want to select a table, where the data matches the selected ones. .
take a look at JPQL, maybe this will help you:
https://www.baeldung.com/spring-data-jpa-query

Form with a select (drop-down) doesn't show error code

I have a form that contains a select to list all teachers by id in the system but it is not working properly.
Here is the code part of the form
and the corresponding path controller requests
I'm Using Thymeleaf and Spring Boot, so 'pr' corresponds a name for a variable of a repository of teachers.
<form th:action="#{/professor/updateProfessor/}" method="post" th:object="${professor}">
<div class= "form-group">
<label th:for = "id">Id</label>
<select th:field="*{id}">
<option
th:value = "${id}"
th:text = "${professor.id}">
</option>
</select>
</div>
<input type = "submit" value = "Add Professor">Save</button>
</form>
#GetMapping(value = {"/selecionaProfessor"})
#ResponseBody
public ModelAndView professorSelecao(){
ModelAndView atualizaProfessor = new ModelAndView("/atualizaProfessor");
atualizaProfessor.addObject("Add Professor");
return atualizaProfessor;
}
#PostMapping(value = {"/selecionaProfessor"})
#ResponseBody
public ModelAndView selecaoProfessor(){
ModelAndView pagSucesso = new ModelAndView("/pagSucesso");
pagSucesso.addObject(pr.findAll());
return pagSucesso;
}
From your controller, send a list of professors as per following to your view. Here you are associating the list of professors to the "professorList" :
model.addAttribute("professorList", pr.findAll());
And then to access above "professorList" in your thymeleaf do (similar to) this :
<option th:each="professor: ${professorList}" th:value="${professor}"> </option>
Not a full code but i hope you got the idea to get started.
For a full example, take a look here and here.
First of all what is not working? because I see a lot of things that may not work maybe because I don't see the all code or I am guessing some things, let's see
When you enter to your controller using
localhost:8080/professor/selecionaProfessor
are you expecting to use the form you put right? (the next code)
<form th:action="#{/professor/updateProfessor/}" method="post" th:object="${professor}">
<div class= "form-group">
<label th:for = "id">Id</label>
<select th:field="*{id}">
<option
th:value = "${id}"
th:text = "${professor.id}">
</option>
</select>
</div>
<input type = "submit" value = "Add Professor">Save</button>
</form>
because if that's correct you have a problem in your method:
#GetMapping(value = {"/selecionaProfessor"})
#ResponseBody
public ModelAndView professorSelecao(){
ModelAndView atualizaProfessor = new ModelAndView("/atualizaProfessor");
atualizaProfessor.addObject("Add Professor");
return atualizaProfessor;
}
you will get an error saying:
Neither BindingResult nor plain target object for bean name 'professor' available as request attribute
So you're missing to add the Key professor and a List so change:
atualizaProfessor.addObject("Add Professor");
with something like:
atualizaProfessor.addObject("professor", someListOfProfessorHereFromTheService (List<Professor>));
and it should work if your profesor object have the attributes you have on your form.
Now let's suppose that that worked before and the error wasn't that.
When you enter to your form if you see here:
form th:action="#{/professor/updateProfessor/}"
you're using updateProfessor I don't see that on your controller you have
#PostMapping(value = {"/selecionaProfessor"})
So I think that you should change the url mapping inside the html page or the controller and use the same as error 1, map the object using a key and value and iterate the list into the html as I showed in the 1st error
Hope it helps

Spring Boot error while submitting form

I'm trying to add a table to the database via a form. The entity being created is called Album and it has 2 fields, Artist and Genre. Each of these two are separate entities. These 2 fields are annotated with #ManyToOne
#ManyToOne
private Artist artist;
#ManyToOne
private Genre genre;
When I submit the form, this is the error im getting:
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor' (album/add:52)
The following code is part of my controller:
#RequestMapping({"/add", "/add/"})
public String adminAlbumAdd(Model model) {
model.addAttribute("album", new Album());
model.addAttribute("artists", artistService.list());
model.addAttribute("genres", genreService.list());
return "album/add";
}
#RequestMapping( value = "/save", method = RequestMethod.POST )
public String save(#Valid Album album, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("artists", artistService.list());
model.addAttribute("genres", genreService.list());
return "album/add";
} else {
Album savedAlbum = albumService.save(album);
return "redirect:/album/view/" + savedAlbum.getAlbumId();
}
}
And the following code is part of the thymeleaf template:
<div th:class="form-group" th:classappend="${#fields.hasErrors('artist')}? 'has-error'">
<label class="col-sm-2 control-label">Artist <span class="required">*</span></label>
<div class="col-md-10">
<select class="form-control" th:field="*{artist}">
<option value="">Select Artist</option>
<option th:each="artist : ${artists}" th:value="${artist.artistId}" th:text="${artist.artistFirstName + ' ' + artist.artistFirstName}">Artists</option>
</select>
<span th:if="${#fields.hasErrors('artist')}" th:errors="*{artist}" th:class="help-block">Artist Errors</span>
</div>
</div>
<div th:class="form-group" th:classappend="${#fields.hasErrors('genre')}? 'has-error'">
<label class="col-sm-2 control-label">Genre <span class="required">*</span></label>
<div class="col-md-10">
<select class="form-control" th:field="*{genre}">
<option value="">Select Genre</option>
<option th:each="genre : ${genres}" th:value="${genre.genreName}" th:text="${genre.genreName}">Genres</option>
</select>
<span th:if="${#fields.hasErrors('genre')}" th:errors="*{genre}" th:class="help-block">Genre Errors</span>
</div>
</div>
What is causing this error ?
The issue turned out to be related to the repository. I was extending CrudRepository, but the id was of type int. Once i changed that, it worked.
Firstly, you might consider using same mapping for GET/POST requests as a standard like:
#GetMapping("/new")
...
#PostMapping("/new")
Also #Valid Album album parameter should be annotated as #ModelAttribute.
You should not add model attributes if binding result has errors. (Actually, you should not add any model attribute for a POST method.)
You should not create that savedAlbum object with albumService.save().
That method should be void.
I will advise against posting directly to your database object. You should rather create a DTO class, say AlbumDto, that will map the classes like so:
public class AlbumDto {
...
private long genreId;
private long artistId;
// Getters & Setters
}
You can then convert it to your Album object, lookup the corresponding Genre and Artist in your controller, set them on the Album object and then save.

How to develop cascading dropdowns in spring 4 using thymeleaf

i have a difficulty in developing cascading dropdowns in spring 4 with thymeleaf.
Here is my scenario:
i have 2 dropdowns like state and city. based on state selection, i need to populate city dropdown with corresponding cities for that state.
I am using Spring boot, spring 4 and thymeleaf template for view.
Thanks in advance.
Do you want to Dropdown / List selectors ?
Could I help you now. Please read this article.
Select fields have two parts: the <select> tag and its nested <option> tags. When creating this kind of field, only the <select> tag has to include a th:field attribute, but the th:value attributes in the nested <option> 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).
Let’s re-build the type field as a dropdown select:
<select th:field="*{type}">
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>
also you can check this Thymeleaf Website
You can use the appelsiini chained select to implement the cascading dropdown.
template.html
<select th:field="*{state}">
<option value="0">--</option>
<option th:each="state : ${states}" th:value="${state.stateId}" th:text="${state.name}">Florida</option>
</select>
<select th:field="*{city}">
<option value="0">--</option>
</select>
<script type="text/javascript">
$("#city").remoteChained({
parents: "#state",
url: /*[[#{/getStateCityValues}]]*/ ""
});
</script>
Controller.java
#PostMapping("/getStateCityValues")
public #ResponseBody
Map<String, String> getStateCityValues(#RequestParam("state") Integer state) {
Map<String, String> cityValues = new HashMap<>();
List<City> cities = cityService.getStateCities(state);
for(City city : cities){
cityValues.put(String.valueOf(city.getCityId()), city.getName());
}
return cityValues;
}

Spring 3: Select value to enum value mapping

I have a very simple scenario to handle. An enum is created to represent a set of options for select control. The select control needs to have a prompt mapped to '-' as the prompt value. The corresponding enum does not have this dash. When page is submitted with select control still sitting at the prompt, exception is thrown. How do you handle such cases?
Page:
<select id="filterUserAccessLevel" name="filterUserAccessLevel">
<option value="-">Select Value</option>
<option value="DEPOSITOR">Depositor</option>
<option value="READER">Reader</option>
<option value="AUTHOR">Author</option>
<option value="EDITOR">Editor</option>
<option value="ADMINISTRATOR">Administrator</option>
</select>
<input type="submit" name="resetFilter" value="<spring:message code="common.filterResetButtonLabel" />" />
UserAccessLevel enum:
public enum UserAccessLevel {
DEPOSITOR("DEPOSITOR"),
READER("READER"),
AUTHOR("AUTHOR"),
EDITOR("EDITOR"),
ADMINISTRATOR("ADMINISTRATOR");
private String code;
private UserAccessLevel(String code) {
this.code=code;
}
public String getCode() {
return this.code;
}
}
Controller:
#RequestMapping(value="/userIndex/", method=RequestMethod.POST, params="resetFilter")
public void resetFilter(#ModelAttribute("userIndexBean") UserIndexBean bean, Model model) {
System.out.println("resetFilter()");
bean.resetFilterSection();
loadBean(1, bean, model);
}
Exception:
Field error in object 'userIndexBean' on field 'filterUserAccessLevel': rejected value [-];
Why is necessary an option mapped to "-"? Can't it be just an empty String?
In this case, I think that the simplest solution is:
<option value="">Select Value</option>
.
#RequestMapping("userIndex")
public void resetFilter(#RequestParam(required = false) UserAccessLevel filterUserAccessLevel) {
...
}

Resources