有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我对th有问题:如果在春天

所以我开始学习java+Spring,我对“th:if”有一个问题。当我想在本地主机上打印时:8080/car yearof production=2000,我使用 th:info=“${info}=='2000'”在我的模板中,什么都不会发生,但当我删除th:info=“${info}=='2000'”时,一切都正常,这意味着当我在1999年的生产年中写下我的应用程序在我写的本地主机年打印时

我的控制器类:

@Controller 公共车厢{

@RequestMapping(value = "/car", method = RequestMethod.POST)

public String carPost(@ModelAttribute("carForm") CarForm form, Model model) {

    model.addAttribute("info", "Rok produkcji samochodu to: " + form.getYearOfProduction());

    return "cars";
}


@RequestMapping(value = "/car", method = RequestMethod.GET)
public String carPost(Model model) {
    model.addAttribute("carForm", new CarForm());
    return "car";
}

}

我的班级:

public class CarForm {

private String carname;
private String yearOfProduction;

public CarForm(String carname, String yearOfProduction) {
    this.carname = carname;
    this.yearOfProduction = yearOfProduction;
}

public CarForm() {

}

public String getCarname() {
    return carname;
}

public void setCarname(String carname) {
    this.carname = carname;
}

public String getYearOfProduction() {
    return yearOfProduction;
}

public void setYearOfProduction(String yearOfProduction) {
    this.yearOfProduction = yearOfProduction;
}

}

还有我的模板:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org/"> <head> <title>Title</title> </head> <body> <center><span th:if="${info} == '2000'" th:text="${info}"></span></center> </br></br> <form th:action="@{/car}" method="post" th:object="${carForm}"> Car name: <input type="text" th:field="*{carname}"> <p></p> year of production: <input type="text" th:field="*{yearOfProduction}"> <p></p> <input type="submit" value="send"> </form> </body> </html>

共 (2) 个答案

  1. # 1 楼答案

    如果您只需要根据任何条件打印任何信息,为什么不尝试以下方法:

    <span th:text="${info} == '2000' ? 'something' : 'something else'"></span>
    

    或者,如果你真的需要if标签,那么:

    <span th:if="${info} == '2000'>Something</span><span th:if="not ${info} == '2000'">Something else</span>
    

    你可能想快速阅读一下Thymeleaf comparators and equality

  2. # 2 楼答案

    可以使用Stringsutils:

    <span th:if="${#strings.equals(info, '2000')}" th:text="${info}"></span>