如何从下拉列表传递值

时间:2019-04-08 20:52:39

标签: spring-mvc model thymeleaf

我正在使用Spring MVC + Thymeleaf项目,将字段值传递给对象时遇到问题。 有maltcountry个实体。在malt格式中,有一个下拉列表,该列表从DB填充-仅是国家/地区名称-没花哨。我可以填充列表,但是当我单击“提交”按钮时,会出现一些错误。下面的代码(仅相关部分):

麦芽实体:

@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;

麦芽控制器:

@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    

回购链接:https://github.com/fangirsan/maruszka-new/tree/malt-form-problem

我尝试了很多不同的方法,但是没有结果。

1 个答案:

答案 0 :(得分:1)

正如例外所述,问题出在elevation: 1表单内的import React, {Component} from 'react' import { StyleSheet, View, Text, ScrollView, Image, TouchableHighlight } from 'react-native' import {BoxShadow} from 'react-native-shadow' export default class VideoCell extends Component { render = () => { const shadowOpt = { width:160, height:170, color:"#000", border:2, radius:3, opacity:0.2, x:3, y:3, style:{marginVertical:5} } return ( <View style={{flex:1}}> <BoxShadow setting={shadowOpt}> <TouchableHighlight style={{ marginTop:10, position:"relative", width: 160, height: 170, backgroundColor: "#fff", borderRadius:3, // marginVertical:5, overflow:"hidden"}}> <Text>Wnand</Text> </TouchableHighlight> </BoxShadow> </View> ) } } 内。在异常堆栈跟踪的最后一行中,我看到了${malt.country.countryName}。这意味着您正在尝试获取相关模型的属性为空malt-show表中的列Property or field 'countryName' cannot be found on null可能为空。换句话说,country_id不会与其他字段一起保存。基于这些假设,您将在malt表格中找到保存麦芽的问题。我检查了此表单,可能是问题country_id。我认为必须将其更改为malt-form

重要提示:

某些相关模型可以为null,例如,假设您的<select class="form-control" th:field="*{id}">模型中的<select class="form-control" th:field="*{country.id}">可以为null(取决于应用程序业务逻辑)。在可为空的关系的情况下,以country模式访问模型关系字段可能会产生上述错误。因此,在这种情况下,您应该在百里香模板中使用空检查