我遇到了麻烦:如果在春天

时间:2018-04-11 15:26:21

标签: java spring thymeleaf

所以我开始学习java + Spring,我遇到了问题:如果"。当我想在localhost上打印:8080 / car YearOfProduction = 2000并且我使用 th:info =" $ {info} ==' 2000'"在我的模板中没有任何反应,但当我删除时:info =" $ {info} ==' 2000'"一切正常,这意味着我在1999年在YearOfProduction中编写我的应用程序打印我写的localhost年份。

我的控制器类:

@Controller 公共类CarCotnroller {

@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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

您可以使用Strings utils:

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

答案 1 :(得分:0)

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

<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 :(得分:0)

我仍然不知道为什么它不起作用,但我把它作为我在CarController中写的另一种方式:

model.addAttribute("info", form.getYearOfProduction() == 2000 ? form.getYearOfProduction(): "Error"); 

它的工作很棒。谢谢你的一切:)