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

时间:2017-02-16 12:12:36

标签: java spring-boot bean-validation

我花了整整一天的时间试图找出为什么我的表单数据在验证失败时消失了。我还添加了一个redirectAttributes.addFlashAttribute,它假设指示发生了错误并且在一次重定向后仍然存在。到目前为止,这些都没有奏效。我已经完成了对堆栈和其他论坛的研究,我似乎正在做正确的事情,但它不适合我。 我没有收到错误,所以我甚至无法调试以找出错误。

<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>

控制器

@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"; }

模型

@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;

1 个答案:

答案 0 :(得分:1)

在你的addTask控制器方法中你有

redirectAttributes.addFlashAttribute("exams",exams)

属性名称为&#34;考试&#34;你正在检查&#34; newExam&#34;在examsList控制器方法中。这可能是问题所在。 试试这个,

redirectAttributes.addFlashAttribute("newExam",exams)