Thymeleaf-spring 启动时模型属性为 null

Model Attributes null with Thymeleaf-spring boot

我有一个奇怪的错误让我抓狂。 我有一个具有两种不同 Get 方法的控制器 - 用于两种不同的 url 映射。 URL1 工作得很好,使用 URL2,我得到

[THYMELEAF][http-nio-8080-exec-3] Exception processing template "questionnaireForm": Error during execution of processor 'org.thymeleaf.standard.processor.attr.StandardEachAttrProcessor' (questionnaireForm:10) caused by java.lang.NullPointerException: null at org.thymeleaf.context.WebSessionVariablesMap.hashCode(WebSessionVariablesMap.java:276) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]

在检查每一行数小时后,很明显我的控制器获取方法在调用视图之前向 Model.addAttribute 添加了一个非空对象。 我还从模板中删除了我的所有代码,并添加了以下内容以进行故障排除:

<tr th:each="var : ${#vars}">
<td th:text="${var.key}"></td>
<td th:text="${var.value}"></td>

这也返回相同的空错误! 我在网上唯一能找到的是 Thymeleaf 2.1.4 的错误,这是由于没有 HttpSession 对象引起的 https://github.com/thymeleaf/thymeleaf/issues/349

但话又说回来,我的 URL1 工作正常! 我没有在这里 post 编辑任何代码,因为我不确定 post 还有什么,请问!

编辑:添加了控制器代码。 /admin/questions 是 URL1 工作正常,'/admin/questionnaires' 是 URL2 不工作。

@Controller
public class AdminController {
private QuestionRepository questionRepository;
private EQuestionnaireRepository eQuestionnaireRepository;
private EQuestionnaire eQuestionnaire;


@Autowired
public AdminController(QuestionRepository questionRepository,
                       EQuestionnaireRepository
                       eQuestionnaireRepository){
    this.questionRepository = questionRepository;
    this.eQuestionnaireRepository = eQuestionnaireRepository;
    eQuestionnaire = eQuestionnaireRepository.findAll().get(0);

}

@RequestMapping(value = "/admin/questions", method=RequestMethod.GET)
        public String questionForm(Model model) {
        model.addAttribute("question", new Question());
        return "questionForm";

}//-->This Works fine

@RequestMapping(value = "/admin/questions", method=RequestMethod.POST)
public String saveQuestion(@ModelAttribute Question newQuestion, BindingResult bindingResult, Model model) {
    if( bindingResult.hasErrors())
    {
        System.out.println(bindingResult);
        System.out.println("BINDING RESULTS ERROR");
        return "questionForm";
    }
    questionRepository.save(newQuestion);
    return "questionForm";
}

@RequestMapping(value = "/admin/Questionnaires", method=RequestMethod.GET)
public String QuestionnaireForm(Model model){
model.addAttribute("ListofQ",eQuestionnaire.getQuestionList());
    return "questionnaireForm";

更新:我切换到 thymeleaf 2.1.5-SNAPSHOT,现在我没有收到前面提到的错误 - 而是一个不同的错误:

There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "ask.questionText"     
(questionnaireForm:13)

所以现在,模板中的对象引用为空。当我将它放入模型时它不是空的(我在 GET 方法中得到了 System.out 的 getQuestionText 方法,所以那是确认的) 这是我在模板中尝试的简单代码:

<table>
  <tr th:each= "ask : ${ListofQ}"></tr>
  <tr th:text= "${ask.questionText}"></tr>
</table>

这里是问题对象:

@Entity
@Table(name = "Questions")
public class Question {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long ID;
private int sequence;
private String questionText;
private int weightage;
private String option1 = "option1";
private String option2 = "option2";
private String option3 = "option3";

public Question() {

}
public Long getID() {
    return ID;
}
public void setID(Long iD) {
    ID = iD;
}

public int getSequence() {
    return sequence;
}
public void setSequence(int sequence) {
    if (sequence > 0) this.sequence = sequence;
}
public String getQuestionText() {
    return questionText;
}
public void setQuestionText(String questionText) {
    this.questionText = questionText;
}
public int getWeightage() {
    return weightage;
}
public void setWeightage(int weightage) {
    this.weightage = weightage;
}

public void setOption1(String option1) {
    this.option1 = option1;

}
public void setOption2(String option2) {
    this.option2 = option2;

}
public void setOption3(String option3) {
    this.option3 = option3;

}
public String getOption1() {
    return option1;
}
public String getOption2() {
    return option2;
}
public String getOption3() {
    return option3;
}

您没有将模型对象返回到 Thymeleaf。

尝试这样的事情:

@RequestMapping(value = "/admin/Questionnaires", method=RequestMethod.GET)
public ModelAndView QuestionnaireForm(Model model){
    return return new ModelAndView("questionnaireForm", "ListofQ",eQuestionnaire.getQuestionList());
}

"each" 中的输出需要在同一个语句中,我认为这对你有用

<table>
  <tr th:each= "ask : ${ListofQ}" th:text= "${ask.questionText}"></tr>
</table>