Spring mvc 如何从下拉列表中传递值
我正在使用SpringMVC+Thymeleaf项目,在将字段值传递给object时遇到了一个问题。 有Spring mvc 如何从下拉列表中传递值,spring-mvc,model,thymeleaf,Spring Mvc,Model,Thymeleaf,我正在使用SpringMVC+Thymeleaf项目,在将字段值传递给object时遇到了一个问题。 有malt和country实体。在malt表单中,有一个下拉列表,该列表由DB-仅国家名称-nothig fancy填充。我可以填充列表,但当我单击“提交”按钮时,会出现一些错误。以下代码(仅适用于相关部分): Malt实体: @Setter @Getter @NoArgsConstructor @Entity @ToString @Table(name="malt") public class
malt
和country
实体。在malt
表单中,有一个下拉列表,该列表由DB-仅国家名称-nothig fancy填充。我可以填充列表,但当我单击“提交”按钮时,会出现一些错误。以下代码(仅适用于相关部分):
Malt实体:
@Setter
@Getter
@NoArgsConstructor
@Entity
@ToString
@Table(name="malt")
public class Malt extends BaseEntity {
@Column(name="malt_name")
private String maltName;
@ManyToOne(fetch=FetchType.EAGER,
cascade= {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name="producer_id")
private Producer producer;
@Column(name="malt_filling")
private int maltFilling;
@Column(name="malt_ebc")
private int maltEbc;
@Column(name="malt_usage")
private String maltUsage;
@ManyToOne(fetch=FetchType.EAGER,
cascade= {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name="country_id")
private Country country;
@ManyToMany(mappedBy="malts")
private Set<Batch> batches;
链接至回购:
我尝试了很多不同的方法,但没有结果。正如例外情况所说,问题是
${malt.country.countryName}
在您的malt show
表单中。在exception stacktrace的最后一行中,我看到在null上找不到属性或字段'countryName'。这意味着您试图获取的相关模型的属性为null。您的malt
表中的country\u id
列可能为空。换句话说,country\u id
未与其他字段一起保存。根据这些假设,您将在保存malt的malt表单中找到问题所在。我检查了此表单,可能问题是
。我想这必须改成
重要提示:
一些相关模型可以为空,例如,假设您的malt
模型中的country
可以为空(取决于应用程序业务逻辑)。对于可为空的关系,访问${model.relation.field}
模式中的模型关系字段可能会产生上述错误。因此,在这些情况下,您应该在您的thymeleaf模板中使用空检查。谢谢,我已经更改了您的建议+我已经删除了Malt模型中国家的级联选项-它正在工作。我需要考虑一些验证,正如你之前提到的。
@Controller
@RequestMapping("/malt")
public class MaltController {
@ModelAttribute("countries")
public Collection<Country> populateCountries() {
return countryService.findAll();
}
@RequestMapping("{id}/update")
public String updateMalt(@PathVariable String id, Model model) {
model.addAttribute("malt", maltService.findById(Long.valueOf(id)));
return "malt-form";
}
@PostMapping
public String saveOrUpdate(@ModelAttribute Malt malt) {
Malt savedMalt = maltService.save(malt);
return "redirect:/malt/" + savedMalt.getId() + "/malt-show";
}
<div class="form-field-input">
<select class="form-control" th:field="*{id}">
<option value="0">Select country</option>
<option
th:each="country : ${countries}"
th:value="${country.id}"
th:text="${country?.countryName}">
</option>
</select>
</div>
<div class="form-field-submit">
<button class="submit-button" type="submit">Submit</button>
</div>
<div class="wrapper">
<div class="main">
<div class="page-title">
<p th:text="${malt.maltName}">Malt name</p>
</div>
<div class="show">
<div class="form-row">
<div class="form-field-name">
<label>Producer:</label>
</div>
<div class="form-field-input">
<p th:text="${malt.producer.producerName}">Producer name</p>
</div>
</div>
<div class="form-row">
<div class="form-field-name">
<label>Country:</label>
</div>
<div class="form-field-input">
<p th:text="${malt.country.countryName}">Country</p>
</div>
</div>
<div class="form-row">
<div class="form-field-name">
<label>Malt filling:</label>
</div>
<div class="form-field-input">
<p th:text="${malt.maltFilling}">Malt filling</p>
</div>
</div>
<div class="form-row">
<div class="form-field-name">
<label>Malt usage:</label>
</div>
<div class="form-field-input">
<p th:text="${malt.maltUsage}">Malt usage</p>
</div>
</div>
<div class="form-row">
<div class="form-field-name">
<label>Malt EBC:</label>
</div>
<div class="form-field-input">
<p th:text="${malt.maltEbc}">Malt EBC</p>
</div>
</div>
</div>
</div>
</div>
An error happened during template parsing (template: "class path resource [templates/malt-show.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/malt-show.html]")
.
.
.
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "malt.country.countryName" (template: "malt-show" - line 44, col 11)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 52 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "malt.country.countryName" (template: "malt-show" - line 44, col 11)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
.
.
.
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'countryName' cannot be found on null