Issue
I have two class:
import java.io.Serializable;
import java.util.Objects;
public class MediaTypeDto implements Serializable {
private String fileExtension;
private MediaTypeEnum mediaType;
public MediaTypeDto() {
}
public MediaTypeDto(String fileExtension, MediaTypeEnum mediaType) {
this.fileExtension = fileExtension;
this.mediaType = mediaType;
}
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public MediaTypeEnum getMediaType() {
return mediaType;
}
public void setMediaType(MediaTypeEnum mediaType) {
this.mediaType = mediaType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MediaTypeDto that = (MediaTypeDto) o;
return fileExtension.equals(that.fileExtension) && mediaType.equals(that.mediaType);
}
@Override
public int hashCode() {
return Objects.hash(fileExtension, mediaType);
}
@Override
public String toString() {
return "MediaTypeDto{" +
"fileExtension='" + fileExtension + '\'' +
", mediaType='" + mediaType + '\'' +
'}';
}
}
And
import java.io.Serializable;
public enum MediaTypeEnum implements Serializable
{
WALLPAPER("Fond d'écran"),
PERSONNAL_PHOTO("Photo personnel"),
OTHER_IMAGE("Autre photo"),
MUSIC("Music"),
AUDIO_BOOK("Livre audio"),
OTHER_SOUND("Autre son"),
MOVIE("Film"),
PERSONNAL_VIDEO("Vidéo personnel"),
OTHER_VIDEO("Autre vidéo"),
BOOK("Livre"),
OTHER_DOCUMENT("Autre document"),
VIDEO_GAME("Jeux vidéo"),
FILE_3D("Fichier 3D");
/*public static final Set<MediaTypeEnum> IMAGE = EnumSet.of(WALLPAPER, PERSONNAL_PHOTO, OTHER_IMAGE);
public static final Set<MediaTypeEnum> SOUND = EnumSet.of(MUSIC, AUDIO_BOOK, OTHER_SOUND);
public static final Set<MediaTypeEnum> VIDEO = EnumSet.of(MOVIE, PERSONNAL_VIDEO, OTHER_VIDEO);
public static final Set<MediaTypeEnum> DOCUMENT = EnumSet.of(BOOK, OTHER_DOCUMENT);
public static final Set<MediaTypeEnum> PERSONNAL = EnumSet.of(PERSONNAL_PHOTO, PERSONNAL_VIDEO);
*/
private final String displayValue;
MediaTypeEnum(String displayValue) {
this.displayValue = displayValue;
}
public String getDisplayValue() {
return displayValue;
}
}
In my controller, i have this request:
@GetMapping("/create")
public String create( Model model) {
model.addAttribute("mediaTypeDto", new MediaTypeDto());
model.addAttribute("mediaTypeEnum", MediaTypeEnum.class);
return "createMediaType";
}
In my html:
<!DOCTYPE html>
<html lang="fr" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" th:action="@{/mediaType/create}" th:object="${mediaTypeDto}" method="post">
<p>Extention: <input type="text" th:field="${mediaTypeDto.fileExtension}"/></p>
<select name="color" th:field="*{mediaTypeDto.mediaType}">
<option th:each="mediaTypeEnum : ${T(com.korp.multimedia.entity.MediaTypeEnum).values()}"
th:value="${mediaTypeEnum}" th:text="${mediaTypeEnum}"></option>
</select>
<p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
</body>
</html>
When I try to access the page, I get the following error message:
org.springframework.beans.NotReadablePropertyException: Invalid property 'mediaTypeDto' of bean class [com.korp.multimedia.entity.MediaTypeDto]: Bean property 'mediaTypeDto' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
I don't understand why it claims that the getter and setter for a property doesn't exist
Solution
Try removing mediaTypeDto.
since you reference it in the form tag th:object="${mediaTypeDto}"
:
<p>Extention: <input type="text" th:field="${mediaTypeDto.fileExtension}"/></p>
<select name="color" th:field="*{mediaTypeDto.mediaType}">
to:
<p>Extention: <input type="text" th:field="${fileExtension}"/></p>
<select name="color" th:field="*{mediaType}">
And probably you also need to change th:field="${fileExtension}"
to th:field="*{fileExtension}"
Answered By - pleft