thymeleaf Field Attr处理器无法正常工作

时间:2018-02-05 11:17:49

标签: spring thymeleaf

我在以下架构中遇到以下错误。什么是错误?

  

Whitelabel错误页面

     

此应用程序没有明确的映射   /错误,所以你看到这是一个后备。出乎意料的是   错误(类型=内部服务器错误,状态= 500)。执行期间出错   处理器   ' org.thymeleaf.spring4.processor.attr.SpringInputRadioFieldAttrProcessor'   (酒店:24)

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Knight list</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

    <script th:src="@{/webjars/jquery/3.2.1/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
</head>
<body>
<form th:object="${hotel1}" th:action="@{/kniths}" th:method="${post}" >
<table class="table">
    <thead>
    <tr>
        <td>Hotel</td>
        <td></td>
    </tr>
    </thead>
    <tbody>
    <ul><label  >Hotels</label>
        <li th:each="hotel : ${hotels}">
            <input type="radio" th:field="*{hotels}" th:value="${hotel.id}" />
            <label  th:text="${hotel.name}"></label>
        </li>
    </ul>
    </tbody>

</table>
</form>
</body>
</html>

@Controller
public class HotelControler {

    @Autowired
    HotelService hotelService;

    @RequestMapping("/newhotel")
    public String addHotel(Model model){
        Hotel hotel1 = new Hotel();
        List<Hotel> hotels = hotelService.getAllHotels();
        model.addAttribute("hotel", hotel1);
        model.addAttribute("hotels",hotels);
        System.out.println(hotel1.getName());
        return "hotel";
    }
}

public class Hotel {

    @NotNull
    @Size(min=2, max=20, message="min 2 znaki maximum 20 znakow")
    private String name;
    private int cena;
    private int id;

    public Hotel (){}

    public Hotel(int id, String name, int cena){
        this.id=id;this.name=name;this.cena=222;
    }

    public Hotel(String name, int cena){
        this.cena=cena;
        this.name=name;
    }

    public String getName(){
        return name;
    }

    public void setName(int id, String name){
        this.id=id;
        this.name=name;
    }

    public void setCena(int cena){
        this.cena=cena;
    }

    public int getCena(){
        return this.cena;
    }

    public int getId(){
        return id;
    }

    public String toString(){
        return "Hotel " + name + "oferuje wpokoje w cenie " + cena;
    }
}

1 个答案:

答案 0 :(得分:0)

控制器酒店模型属性值不正确,必须像这样更改。

model.addAttribute("hotel1", hotel1);

在视图中th:field需要更改为酒店的“名称”属性。

 <input type="radio" th:field="*{name}" th:value="${hotel.id}" />