Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 提交表单时,Spring Boot Thymeleaf SpelEvaluationException EL1007E_Java_Spring Boot_Thymeleaf - Fatal编程技术网

Java 提交表单时,Spring Boot Thymeleaf SpelEvaluationException EL1007E

Java 提交表单时,Spring Boot Thymeleaf SpelEvaluationException EL1007E,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,我正在尝试在我的spring boot应用程序中使用thymeleaf处理表单提交,我集成了 这在我的应用程序上运行良好。我正试图改变它一点,但这是我作为一个例外得到的: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'recruiter' cannot be found on null at org.springframework.expression.sp

我正在尝试在我的spring boot应用程序中使用
thymeleaf
处理表单提交,我集成了
这在我的应用程序上运行良好。我正试图改变它一点,但这是我作为一个例外得到的:

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'recruiter' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.2.3.RELEASE.jar:5.2.3.RELEASE]

...
这就是我试图处理的对象
thymeleaf


公共类FormRec{
私人字符串猎头;
公共字符串getRecruiter(){
返回招聘人员;
}
公共招聘人员(字符串招聘人员){
this.recruiter=招聘人员;
}
}
这是我的
控制器

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.service.minimicroservice.objects.FormRec;

@Controller
public class MyController {




      @GetMapping("/form")
      public String greetingForm(Model model) {
        model.addAttribute("recForm", new FormRec());
        return "form";
      }

      @PostMapping("/form")
      public String greetingSubmit(@ModelAttribute FormRec rec) {
        return "result";
      }


}

result.html


入门:处理表单提交
结果

form.html的一部分


招聘人员姓名
全名
...
为了引用
FormRec
对象,我在
form.html
中使用
recForm
作为
th:Object
,并在
result.html
中引用它


注意:我在提交表单时将值传递给
th:field=“*{recruiter}”
输入文本(不为null

您必须命名数据绑定到的ModelAttribute
rec
。因此,在控制器方法中执行此操作(注意
name=“rec”
):

它应该会起作用

附加说明:

我仔细研究了为什么会发生这种情况,这是因为Spring的
ModelAttribute
注释默认是从类型的名称而不是参数的名称推断出来的(您提供的示例链接说它是方法参数名称,但似乎是错误的)

因此,在本例中,Spring将
formRec
(注意camelCase,当类名被调用
formRec
)发送到
result.html
,而不是像您预期的那样发送到
rec

如果我的解释没有真正意义,那么这就是
ModelAttribute
上的Spring文档:

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'recruiter' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.2.3.RELEASE.jar:5.2.3.RELEASE]

...
默认模型属性名称是根据声明的属性类型(即方法参数类型或方法返回类型)根据非限定类名称推断出来的:例如,类“mypackage.orderAddress”的“orderAddress”,或“List”的“orderAddressList”


您正在
result.html
中调用
rec.recruiter
,但是您是否将
rec
对象传递到该html页面?我认为当您在@PostMapping方法中传递@modeltribute时,它会自行处理查看简介部分中的示例是的,对不起,必须在IDE中更好地查看它。它起作用了,谢谢!虽然spring boot示例中没有包括这一点,但您是对的。我补充了关于这一点的解释。Spring示例中关于问候语参数名称推断的一句话似乎已经过时或错误。