thymeleaf 错误 - 通过 Thymleaf 和 Netbeans 将两个数字相加

问题描述 投票:0回答:1

我正在尝试使用 Thymeleaf 通过表单请求两个数字(第一个和第二个)作为输入,并添加数字并返回答案。代码运行自: http://localhost:8080/add

我的程序基于此链接: https://spring.io/guides/gs/validating-form-input/

我也在使用最新版本的Springboot,目前是1.5.8。

我发现这个简单的任务变得有些复杂(与Python相比),不断出现没有任何意义的错误。我当前的问题是我收到以下错误,即使在网上检查类似的帖子后,我也无法解决...

错误: “异常处理模板“add”:处理器'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'执行期间出错”

根据这些帖子,我已将 NumberForm 中的“long”更改为“Long”,并将 NumberForm.java 移至“sec.calculator”下,但没有喜悦...

  • 如有任何帮助,我们将不胜感激!

这是我的代码:

1)src/main/java

CalculatorController.java:

package sec.calculator;

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

@Controller
public class CalculatorController {

  @GetMapping("/add")
  public String addForm(Model model) {
     model.addAttribute("add", new NumberForm());
     return "add";
  }

  @PostMapping("/add")
  public String addSubmit(@ModelAttribute NumberForm add) {
     return "result";
  }
}

------------------------------------

CalculatorApplication.java

package sec.calculator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CalculatorApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(CalculatorApplication.class, args);
    }
}

------------------------------------

NumberForm.java

package sec.calculator;

/* * 要更改此许可证标头,请在项目属性中选择许可证标头。 * 要更改此模板文件,请选择“工具”|“模板 * 并在编辑器中打开模板。 */

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**import javax.validation.constraints.Size;
*/

public class NumberForm {
    @NotNull
    @Min(0)
    private Long first;
    private Long second;

    public Long add_out;

    public Long add_output() {
        return add_out ;
    }

    public void Setfirst(Long first) {
        this.first = first;
    }

    public void Setsecond(Long second) {
        this.second = second;
    }
}   

------------------------------------------------

2)src/main/resources/templates

添加.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project 
Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

    <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
            <body>
                <h1>Form</h1>
                    <form action="#" th:action="@{/add}" th:object="${NumberForm}" method="post">
                    <p>First: <input type="text" th:field="*{First}" /></p>
                    <p>Second: <input type="text" th:field="*{Second}" /></p>
                    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
                </form>
            </body>
    </html>

---------------------------------------------------------

java netbeans thymeleaf
1个回答
0
投票

欢迎来到SO。

这里有几个问题。特别是,Java 非常区分大小写。

  1. 遵循 setter 的约定,以便 Thymeleaf 可以 明白了:

    public void setFirst(Long first) {
        this.first = first;
    }
    
    public void setSecond(Long second) {
        this.second = second;
    }
    

    我假设您遗漏了类似的 getter 方法。

    更好的是,请参阅 Project Lombok - 完全停止疯狂并使用

    @Setter
    @Getter
    @Data

  2. 在表单中,小写字段名称:

    <p>First: <input type="text" th:field="*{first}" /></p>
    <p>Second: <input type="text" th:field="*{second}" /></p>
    

    我也会远离这样的变量名称,因为它们会变得非常混乱。

  3. 也在您的表单中,您正在使用

    th:object="${NumberForm}"
    但是 您将变量命名为
    add
    。 Thymeleaf 对这个名为
    NumberForm
    的变量一无所知。您的变量名为
    add
    ,类型为
    NumberForm
    ,但 Thymeleaf 需要此处的变量名称。

    所以你可以这样做:

    model.addAttribute("numberForm", new NumberForm());

    th:object="${numberForm}"

    使用小写字母表示

    th:object
    (modelAttribute) 是 大会。

  4. 您实际上需要包含一些代码来对值求和。患病的 把那部分留给你。

© www.soinside.com 2019 - 2024. All rights reserved.