简体   繁体   English

使用Spring Boot和Thymeleaf-我无法将数据从模板传递到控制器再传递回模板

[英]Using Spring Boot and Thymeleaf - I am having trouble passing data from template to controller back to template

New to Spring/REST and getting this error after creating a new attorney, I have an attorney table with a firstName field mapped appropriately. Spring / REST的新手,在创建新律师后出现此错误,我有一个律师表,其firstName字段已正确映射。 The print statement outputs the current data. 打印语句输出当前数据。 I am not sure why the data is not getting to the template. 我不确定为什么数据没有到达模板。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'firstName' cannot be found on null

This is my controller: 这是我的控制器:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public Object create(@ModelAttribute Attorney attorney, @Valid Login login, @Valid Firm firm, BindingResult bindingResult, Model model) {
    try {
        if (bindingResult.hasErrors()) {
            model.addAttribute("createdAttorney", attorney);
            model.addAttribute("InitialLogin", login);
            model.addAttribute("firm", firm);
            return "views/register";
        } else {
           // login.setAttorneyEmailAddress(attorney.getEmailAddress());
           // login.setAttorneyId(attorney.getAttorneyId());
            this.attorneyService.save(attorney);
            System.out.println(attorney);
            model.addAttribute("att", attorney);
            return "redirect:/attorney";
        }
    } catch (Exception ex) {
        return "Error Registering " + ex.toString();
    }
}

This is my "redirect: /attorney" info: 这是我的“重定向:/ attorney”信息:

 Attorney <p th:text="${att.firstName}"></p> Summary Page

Step 1) 第1步)

In your html file add <form:form> tag so that you can add model attribute 在您的html文件中添加<form:form>标记,以便您可以添加模型属性

Step 2) 第2步)

You should return Model View instead of Object from your controller . 您应该从控制器返回模型视图而不是对象。

Create ModelView object in your controller and try returning the ModelView 在控制器中创建ModelView对象,然后尝试返回ModelView

ModelView model=new ModelView();
model.addAttribute("att", attorney);

Step 1 is not mandatory try using Step 2 alone ,if it doesn't work try both 步骤1不是强制性的,请尝试单独使用步骤2,如果它不起作用,请同时尝试

您应该添加

model.addAttribute("att", new Attorney());

您是否不必在方法中返回ModelAndView对象而不是Object?

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

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