简体   繁体   English

验证失败后,为什么我的redirectAttributes.addFlashAttribute和重新填充的数据不起作用?

[英]Why is my redirectAttributes.addFlashAttribute and data Re-populating not working after validation fails?

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. 我还添加了redirectAttributes.addFlashAttribute,它假定是指示发生了错误并且也可以在一次重定向中保留下来。 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. 我已经在stack over和其他论坛上进行了研究,而且似乎做对了,但对我却没有用。 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 在您的addTask控制器方法中

redirectAttributes.addFlashAttribute("exams",exams)

attribute name as "exams" and you are checking as "newExam" in examsList controller method. 属性名称为“ exams”,而您在examsList控制器方法中正在作为“ newExam”进行检查。 This might be the problem. 这可能是问题所在。 try this, 尝试这个,

redirectAttributes.addFlashAttribute("newExam",exams)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM