无法使用thymeleaf获得提交的值

时间:2017-11-16 16:00:34

标签: java spring spring-boot thymeleaf

我是百老汇的新手,我对如何从html(使用Thymeleaf)到Java(Spring Boot)检索字段表示怀疑。请遵循我所拥有的代码和错误:

HTML(部分有问题)

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0" />
<title>Entity Migration</title>

<!-- CSS  -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
    rel="stylesheet">

<link href="css/style.css" type="text/css" rel="stylesheet"
    media="screen,projection" />
<link href="css/materialize.css" type="text/css" rel="stylesheet"
    media="screen,projection" />


</head>
<body>
    <nav class="light-blue lighten-1" role="navigation">
        <div class="nav-wrapper container">
            <a id="logo-container" href="#" class="brand-logo">Entity
                Migration</a>
            <ul class="right hide-on-med-and-down">
                <li><a href="#">Logout</a></li>
            </ul>

            <ul id="nav-mobile" class="side-nav">
                <li><a href="#">Entity Migration</a></li>
            </ul>
            <a href="#" data-activates="nav-mobile" class="button-collapse"><i
                class="material-icons">Logout</i></a>
        </div>
    </nav>
    <div class="section no-pad-bot" id="index-banner">
        <div class="container">
            <br> <br>
            <div class="row center">
                <h5 class="header col s12 light">Fill the form below to
                    generate your XML:</h5>
            </div>
            <br> <br>

        </div>
    </div>
    <form class="col s12" action="#" th:action="@{/prepareData}" th:object="${entity}" method="post">
        <div class="row">
            <div class="input-field col s4">
                <input type="number" id="release" name="release" placeholder="Release" 
                    class="validate" th:value="*{release}"/>

            </div>
            <div class="input-field col s4">
                <input placeholder="Version" id="version" name="version" type="number"
                    class="validate" th:value="*{version}"/>
            </div>
        </div>
        <input type="submit" value="Generate XML" id="generate"
                class="btn-large waves-effect waves-light orange" />
        </div>
    </form>
        <!--  Scripts-->
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="js/init.js"></script>
    <script src="js/materialize.js"></script>
    <script>
        $(document).ready(function() {
            $('select').material_select();
        });
    </script>

</body>
</html>

Java(Spring Boot Controller)

 @PostMapping(value = "/prepareData")
  public String prepareData(@ModelAttribute(value="entity") EntityMigration entity) {
    TemplatePrepare tP = new TemplatePrepare();
    tP.prepareMainTemplate(entity);
    return "results";

EntityMigration(Java模型)

public class EntityMigration {

    private String release;
    private String version;

    public String getRelease() {
        return release;
    }
    public void setRelease(String release) {
        this.release = release;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
}

错误

    2017-11-16 14:01:02.445 ERROR 26932 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "mainForm": An error happened during template parsing (template: "class path resource [templates/mainForm.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/mainForm.html]")

(...)

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "release" (template: "mainForm" - line 55, col 23)

(...)

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

(...)

我做错了什么? 谢谢。

2 个答案:

答案 0 :(得分:1)

解析html异常是由忘记关闭输入标记引起的。请替换:

<input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}">
<input placeholder="Version" id="version" type="number" class="validate">

with:

<input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}"/>
<input placeholder="Version" id="version" type="number" class="validate"/>

后期错误:

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

是因为尝试访问“发布”而造成的。 on&#39; entity&#39; - &GT;实体为空,因此Thymeleaf无法呈现它。

您必须添加属性&#39;实体&#39;进行建模以进行渲染。 为了避免SpelEvaluationException,您可以在控制器中检查null:

if (entityManager!= null) {
    model.addAttribute("entity", entityManager);
} else {
    model.addAttribute("entity", new EntityManager());
}

答案 1 :(得分:0)

您忘记在输入中使用name属性,并关闭您的输入,因此请替换:

<input placeholder="Version" id="version" type="number" class="validate">
        <input type="number" id="release" placeholder="Release" class="validate" th:value="*{release}">

with:

<input placeholder="Version" id="version" name="version" type="number" class="validate" />
        <input type="number" id="release" name="release" placeholder="Release" class="validate" th:value="*{release}" />

或者您可以使用