![](/img/trans.png)
[英]Keep object in the view after refreshing when using RedirectAttributes.addFlashAttribute
[英]Why is my redirectAttributes.addFlashAttribute and data Re-populating not working after validation fails?
我花了一整夜的時間來尋找為什么驗證失敗時表單數據會消失的原因。 我還添加了redirectAttributes.addFlashAttribute,它假定是指示發生了錯誤並且也可以在一次重定向中保留下來。 到目前為止,這些都不起作用。 我已經在stack over和其他論壇上進行了研究,而且似乎做對了,但對我卻沒有用。 我沒有收到錯誤,所以我什至無法調試以查找問題所在。
<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;
在您的addTask控制器方法中
redirectAttributes.addFlashAttribute("exams",exams)
屬性名稱為“ exams”,而您在examsList控制器方法中正在作為“ newExam”進行檢查。 這可能是問題所在。 嘗試這個,
redirectAttributes.addFlashAttribute("newExam",exams)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.