I have spent the whole day and night trying to find why my form data disappears when validation fails. I also added a redirectAttributes.addFlashAttribute which is suppose to indicate that an error occurred and also survive one redirect. So far none of these are working. I have done my research on stack over and other forums and i seem to be doing the right thing but it is not working for me. I am not getting an error so i can not even debug to find what is wrong.
<div class="form-group" th:if="${exams.size() lt 6}">
<form method="post" th:object="${newExam}" th:action="@{/exams}" class="inline new-item">
<div th:classappend="${#fields.hasErrors('indexNumber')}? 'error' : ''">
<input type="text" th:field="*{indexNumber}" placeholder="Index Number" />
<div class="error-message" th:if="${#fields.hasErrors('indexNumber')}" th:errors="*{indexNumber}"></div>
</div>
<div th:classappend="${#fields.hasErrors('grade')}? 'error' : ''">
<select th:field="*{grade}" class="form-control input-lg">
<option value="">[Select Grade]</option>
<option th:each="grade : ${grades}" th:value="${grade.values}" th:text="${grade.name}">Grade
</option>
</select>
<div class="error-message" th:if="${#fields.hasErrors('grade')}" th:errors="*{grade}"></div>
</div>
<div th:classappend="${#fields.hasErrors('courseOffered')}? 'error' : ''">
<input type="text" th:field="*{courseOffered}" placeholder="CourseOffered" />
<div class="error-message" th:if="${#fields.hasErrors('courseOffered')}" th:errors="*{courseOffered}"></div>
</div>
<div th:classappend="${#fields.hasErrors('examType')}? 'error' : ''">
<input type="text" th:field="*{examType}" placeholder="ExamType" />
<div class="error-message" th:if="${#fields.hasErrors('examType')}" th:errors="*{examType}"></div>
</div>
<div th:classappend="${#fields.hasErrors('subject')}? 'error' : ''">
<input type="text" th:field="*{subject}" placeholder="Subject" />
<div class="error-message" th:if="${#fields.hasErrors('subject')}" th:errors="*{subject}"></div>
</div>
<div th:classappend="${#fields.hasErrors('gradeYear')}?'error' : ''">
<input type="text" th:field="*{gradeYear}" placeholder="ExamYear" />
<div class="error-message" th:if="${#fields.hasErrors('gradeYear')}" th:errors="*{gradeYear}"></div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
Controller
@RequestMapping(value = "/cert_prog", method = RequestMethod.GET)
public String examsList(Model model){
Iterable<Exams> exams = examService.findAll();
if(!model.containsAttribute("newExam")){
model.addAttribute("newExam", new Exams()); }
model.addAttribute("grades", Grade.values());
model.addAttribute("regions", Region.values()); model.addAttribute("schools",schools);
if(!model.containsAttribute("newSchool")){
model.addAttribute("newSchool",new School()); }
model.addAttribute("regions", Region.values());
return "cert_prog"; }
@RequestMapping(value = "/exams", method = RequestMethod.POST) public String addTask(@Valid
@ModelAttribute("newExam") Exams exams, BindingResult result, RedirectAttributes redirectAttributes, Principal principal){
User user = (User)((UsernamePasswordAuthenticationToken)principal).getPrincipal(); exams.setUser(user);
if(result.hasErrors()){
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.exams", result);
redirectAttributes.addFlashAttribute("exams",exams); return "redirect:/cert_prog"; }
examService.save(exams);
return "redirect:/cert_prog"; }
Model
@Entity
public class Exams {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "The above field must not be blank.")
private String courseOffered;
@NotNull(message = "The above field must not be blank.")
private String examType;
@NotNull(message = "The above field must not be blank.")
private String subject;
@NotNull(message = "The above field must not be blank.")
private String grade;
@NotNull(message = "The above field must not be blank.")
private Long indexNumber;
@NotNull(message = "The above field must not be blank.")
private Long gradeYear;
private boolean isComplete;
In your addTask controller method you have
redirectAttributes.addFlashAttribute("exams",exams)
attribute name as "exams" and you are checking as "newExam" in examsList controller method. This might be the problem. try this,
redirectAttributes.addFlashAttribute("newExam",exams)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.