如何在Spring中解决重定向或模板解析器400错误

时间:2017-02-15 21:37:50

标签: java validation redirect spring-boot

我正在尝试将数据提交到数据库后将表单页面重定向到同一页面。在我将验证注释添加到模型和控制器之前,它工作正常。添加验证后,它会产生400错误,无法解析或找到模板。我添加了v重定向归因但它仍然没有解决错误

错误

2017-02-15 16:36:10.096 ERROR 28316 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "error": Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
2017-02-15 16:36:10.109 ERROR 28316 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet dispatcherServlet threw exception

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]

Cert_prog.html

<form method="post" th:object="${newExam}" th:action="@{/exams}" class="inline new-item">
     <input type="text" th:field="*{indexNumber}" placeholder="Index Number"/>
            <select th:field="*{grade}" class="form-control input-lg">
                            <option value="">Grade</option>
                            <option th:each="grade : ${grades}" th:value="${grade.values}" th:text="${grade.name}">Grade
                            </option>
             </select>
      <input type="text" th:field="*{courseOffered}" placeholder="CourseOffered"/>
      <input type="text" th:field="*{examType}" placeholder="ExamType"/>
      <input type="text" th:field="*{subject}" placeholder="Subject"/>
      <input type="text" th:field="*{gradeYear}" placeholder="ExamYear"/>
      <button type="submit" class="btn btn-primary">Add</button>
         </form>

模型

@Entity
public class Exams {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String courseOffered;

    @NotNull
    private String examType;

    @NotNull
    private String subject;

    @NotNull
    private String grade;

    @NotNull
    private Long indexNumber;

    @NotNull
    private Long gradeYear;

    private boolean isComplete;

/*    private int counter;

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }*/

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCourseOffered() {
        return courseOffered;
    }

    public void setCourseOffered(String courseOffered) {
        this.courseOffered = courseOffered;
    }

    public String getExamType() {
        return examType;
    }

    public void setExamType(String examType) {
        this.examType = examType;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public Long getIndexNumber() {
        return indexNumber;
    }

    public void setIndexNumber(Long indexNumber) {
        this.indexNumber = indexNumber;
    }

    public Long getGradeYear() {
        return gradeYear;
    }

    public void setGradeYear(Long gradeYear) {
        this.gradeYear = gradeYear;
    }

    public boolean isComplete() {
        return isComplete;
    }

    public void setComplete(boolean complete) {
        isComplete = complete;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

控制器

@RequestMapping(value = "/cert_prog", method = RequestMethod.GET)
    public String examsList(Model model){
        Iterable<Exams> exams = examService.findAll();
        model.addAttribute("exams", exams);
        if(!model.containsAttribute("newExams")){
            model.addAttribute("newExam", new Exams());
        }
    }  

@RequestMapping(value = "/exams", method = RequestMethod.POST)
            public String addTask(@Valid @ModelAttribute Exams exams, Principal principal, BindingResult result, RedirectAttributes redirectAttributes){
                //User user = userService.findByUsername(principal.getName());
                User user = (User)((UsernamePasswordAuthenticationToken)principal).getPrincipal();
                exams.setUser(user);
                //examService.increment(exams);
                if(result.hasErrors()){
                    redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.exams", result);
                    //
                    redirectAttributes.addFlashAttribute("exams",exams);
                    //redirect back to form
                    return "redirect:/cert_prog";
                }
                examService.save(exams);
                return "redirect:/cert_prog";
            }

0 个答案:

没有答案