Search code examples
javaspringspring-bootthymeleaf

Can't get values submitted using thymeleaf


I am new with thymeleaf and I am having a doubt about how to retrieve a field from html (using Thymeleaf) to Java (Spring Boot). Follow the code and the error that I am having:

HTML (part with issue)

    <!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 Model)

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

Error

    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

(...)

What am I doing wrong? Thank you.


Solution

  • Parsing html exception was caused by forgetting to close input tags. Please replace:

    <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"/>
    

    Latter error:

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

    is caused by trying to access 'release' on 'entity' -> entity is null so Thymeleaf can't render it.

    You must addAttribute 'entity' to model in order to render it. In order to avoid SpelEvaluationException you can check for null in controller:

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