java.lang.IllegalStateException:Bean名称“ CityInfos”的BindingResult和普通目标对象都不能用作请求属性

时间:2018-11-10 18:21:50

标签: java spring spring-mvc thymeleaf

我无法使我的表格正常工作,我在不同的论坛上尝试了很多东西,但找不到解决方法

我的控制器:

@RequestMapping(value = "/board", method = RequestMethod.GET)
public String getCityValues(Model model) {
    model.addAttribute("CityInfos", new CityInfos());
    return "board";
}

@PostMapping("/board")
public String CitySubmit(@ModelAttribute CityInfos cityInfos) {
    return "board";
}

我的课:

public class CityInfos {

    private String cityName;
    private String countryCode;

    public CityInfos() {}

    public CityInfos(String cityName, String countryCode) {
        this.cityName = cityName;
        this.countryCode = countryCode;
    }

    public String getCityName() {
        return cityName;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }
}

和board.html

<form th:action="@{/board}" th:object="${CityInfos}" method="post">
    <p>Name of the City : <input type="text" th:text="*{cityName}" /></p>
    <p>Country code : <input type="text" th:text="*{countryCode}" /></p>
    <input type="submit" value="Submit" />
</form>

我只想将输入文本的值存储在我的java类中

1 个答案:

答案 0 :(得分:0)

尝试一下:

@RequestMapping(value = "/board", method = RequestMethod.GET)
public ModelAndView getCityValues(ModelAndView mv) {
    mv.setViewName("board");
    mv.addObject("CityInfos", new CityInfos());

    return mv;
}