UserController.java:
@Controller
public class UserController {
@Autowired
private CensusRepository UR;
@GetMapping("/")
public String home(Model model){
CensusModel censusModel = new CensusModel();
FMModel fmModel = new FMModel();
model.addAttribute("fetchCensusModel", censusModel);
model.addAttribute("fetchFMModel", fmModel);
return "test";
}
@PostMapping("/saveUser")
public String saveUser(@ModelAttribute("fetchCensusModel") @Valid CensusModel userForm,
BindingResult userFormBindingResult, Model model) {
if (userFormBindingResult.hasErrors()) {
model.addAttribute("fetchCensusModel", userForm);
return "test";
}
UR.save(userForm);
return "test";
}
}
form in test.html
<form th:action="@{/saveUser}" id="saveUserID" th:object="${fetchCensusModel}" method="post">
<div id="page1" class="page page-1">
<h1>Census Details</h1>
<div>
<div class="form-group">
<label for="fullName">Full Name</label>
<input type="text" id="fullName" th:field="${fetchCensusModel.fullName}" placeholder="Enter your full name" />
<div th:if="${#fields.hasErrors('fullName')}" th:errors="${fetchCensusModel.fullName}">Full name error</div>
</div>
<div class="form-inline">
<div class="form-group">
<label for="dateOfBirth">Date of Birth</label>
<input type="date" id="dateOfBirth" th:field="${fetchCensusModel.dateOfBirth}" placeholder="Enter date of birth" />
<div th:if="${#fields.hasErrors('dateOfBirth')}" th:errors="${fetchCensusModel.dateOfBirth}">Full name error</div>
</div>
<div class="form-group">
<label for="gender">Gender</label>
<select id="gender" th:field="${fetchCensusModel.gender}">
<option value="">Select Gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
<div th:if="${#fields.hasErrors('gender')}" th:errors="${fetchCensusModel.gender}">Gender error</div>
</div>
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" th:field="${fetchCensusModel.email}" placeholder="Enter your email address" />
<div th:if="${#fields.hasErrors('email')}" th:errors="${fetchCensusModel.email}">Email address error</div>
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="tel" id="phone" th:field="${fetchCensusModel.phone}" maxlength="10" placeholder="Enter your phone number" />
<div th:if="${#fields.hasErrors('phone')}" th:errors="${fetchCensusModel.phone}">Phone number error</div>
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea type="tel" id="address" th:field="${fetchCensusModel.address}" placeholder="Enter your address" />
<div th:if="${#fields.hasErrors('address')}" th:errors="${fetchCensusModel.address}">Address error</div>
</div>
<div class="form-group">
<label for="occupation">Occupation</label>
<input type="text" id="occupation" th:field="${fetchCensusModel.occupation}" placeholder="Enter your occupation" />
<div th:if="${#fields.hasErrors('occupation')}" th:errors="${fetchCensusModel.occupation}">Occupation error</div>
</div>
<div class="form-group">
<label for="education">Education</label>
<input type="text" id="education" th:field="${fetchCensusModel.education}" placeholder="Enter your education" />
<div th:if="${#fields.hasErrors('education')}" th:errors="${fetchCensusModel.education}">Education error</div>
</div>
<fieldset th:object="${fetchFMModel}">
<div class="form-inline">
<div class="form-group">
<label for="fatherName">Father's Name</label>
<input type="text" id="fatherName" th:field="${fetchFMModel.fatherName}" placeholder="Enter your father's name" />
<div th:if="${#fields.hasErrors('fatherName')}" th:errors="${fetchFMModel.fatherName}">Father name error</div>
</div>
<div class="form-group">
<label for="motherName">Mother's Name</label>
<input type="text" id="motherName" th:field="${fetchFMModel.motherName}" placeholder="Enter your mother's name" />
<div th:if="${#fields.hasErrors('motherName')}" th:errors="${fetchFMModel.motherName}">Mother name error</div>
</div>
</div>
</fieldset>
<div class="form-group center">
<button id="submit1" type="submit">Submit</button>
</div>
</div>
</div>
</form>
CensusModel.java
@Entity
@Transactional
@Table(name = "test_user", uniqueConstraints = { @UniqueConstraint(columnNames = "id") })
public class CensusModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotBlank(message = "Full name is required")
private String fullName;
@NotNull(message = "Date of birth is required")
@Past(message = "Date of birth must be a past date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateOfBirth;
@NotBlank(message = "Gender is required")
private String gender;
@NotBlank(message = "Email is required")
@Email(message = "Email should be valid")
private String email;
@NotBlank(message = "Phone is required")
@Pattern(regexp = "^[0-9]{10}$", message = "Phone number should be 10 digits")
private String phone;
@NotBlank(message = "Address is required")
private String address;
@NotBlank(message = "Occupation is required")
private String occupation;
@NotBlank(message = "Education is required")
private String education;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<FMModel> fmModels;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public Set<FMModel> getFmModels() {
return fmModels;
}
public void setFmModels(Set<FMModel> fmModels) {
this.fmModels = fmModels;
}
}
FMModel.java
@Entity
@Table(name = "test_fm", uniqueConstraints = { @UniqueConstraint(columnNames = "id") })
public class FMModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@NotBlank(message = "Father's name is required")
private String fatherName;
@NotNull(message = "Father's date of birth is required")
@Past(message = "Father's date of birth must be a past date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date father_date_of_birth;
@NotBlank(message = "Father's email is required")
@Email(message = "Father's email should be valid")
private String father_email;
@NotBlank(message = "Father's phone is required")
private String father_phone;
@NotBlank(message = "Father's address is required")
private String father_address;
@NotBlank(message = "Father's occupation is required")
private String father_occupation;
@NotBlank(message = "Father's education is required")
private String father_education;
@NotBlank(message = "Mother's name is required")
private String motherName;
@NotNull(message = "Mother's date of birth is required")
@Past(message = "Mother's date of birth must be a past date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date mother_date_of_birth;
@NotBlank(message = "Mother's email is required")
@Email(message = "Mother's email should be valid")
private String mother_email;
@NotBlank(message = "Mother's phone is required")
private String mother_phone;
@NotBlank(message = "Mother's address is required")
private String mother_address;
@NotBlank(message = "Mother's occupation is required")
private String mother_occupation;
@NotBlank(message = "Mother's education is required")
private String mother_education;
@ManyToOne
@JoinColumn(name = "census_id", nullable = false)
private CensusModel censusModel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public Date getFather_date_of_birth() {
return father_date_of_birth;
}
public void setFather_date_of_birth(Date father_date_of_birth) {
this.father_date_of_birth = father_date_of_birth;
}
public String getFather_email() {
return father_email;
}
public void setFather_email(String father_email) {
this.father_email = father_email;
}
public String getFather_phone() {
return father_phone;
}
public void setFather_phone(String father_phone) {
this.father_phone = father_phone;
}
public String getFather_address() {
return father_address;
}
public void setFather_address(String father_address) {
this.father_address = father_address;
}
public String getFather_occupation() {
return father_occupation;
}
public void setFather_occupation(String father_occupation) {
this.father_occupation = father_occupation;
}
public String getFather_education() {
return father_education;
}
public void setFather_education(String father_education) {
this.father_education = father_education;
}
public String getMotherName() {
return motherName;
}
public void setMotherName(String motherName) {
this.motherName = motherName;
}
public Date getMother_date_of_birth() {
return mother_date_of_birth;
}
public void setMother_date_of_birth(Date mother_date_of_birth) {
this.mother_date_of_birth = mother_date_of_birth;
}
public String getMother_email() {
return mother_email;
}
public void setMother_email(String mother_email) {
this.mother_email = mother_email;
}
public String getMother_phone() {
return mother_phone;
}
public void setMother_phone(String mother_phone) {
this.mother_phone = mother_phone;
}
public String getMother_address() {
return mother_address;
}
public void setMother_address(String mother_address) {
this.mother_address = mother_address;
}
public String getMother_occupation() {
return mother_occupation;
}
public void setMother_occupation(String mother_occupation) {
this.mother_occupation = mother_occupation;
}
public String getMother_education() {
return mother_education;
}
public void setMother_education(String mother_education) {
this.mother_education = mother_education;
}
public CensusModel getCensusModel() {
return censusModel;
}
public void setCensusModel(CensusModel censusModel) {
this.censusModel = censusModel;
}
}
Yes, naming is not upto the mark!
I get an error saying: Neither BindingResult nor plain target object for bean name 'fetchFMModel' available as request attribute
I am still able to save the data that belongs to the first table, but second table stays empty.
Task is to not declare the second model (FMModel) in the save method, in other words the saving should be carried out by only and only UR.save(userForm);
sttipid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.