Spring MVC thymeleaf无法解析对象属性和字段
我刚刚开始发现SpringMVC,并被这个简单的项目困住了。在模板index1.html中,无法解析播放器对象(th:object=“${player}”)和字段值(th:field=“{playerId}”),(th:field=“{playerName}”)。在带有(${player.playerId})和(${player.playerName})的模板index2.html中也存在相同的情况。你能说明原因吗 PlayerController.javaSpring MVC thymeleaf无法解析对象属性和字段,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,我刚刚开始发现SpringMVC,并被这个简单的项目困住了。在模板index1.html中,无法解析播放器对象(th:object=“${player}”)和字段值(th:field=“{playerId}”),(th:field=“{playerName}”)。在带有(${player.playerId})和(${player.playerName})的模板index2.html中也存在相同的情况。你能说明原因吗 PlayerController.java @Controller public
@Controller
public class PlayerController {
Logger log = LoggerFactory.getLogger(this.getClass());
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String showForm(Model model) {
model.addAttribute("player", new Player());
return "create";
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processForm(@ModelAttribute Player player, Model model) {
model.addAttribute("player", player);
String info = String.format("Player Submission: playerId = %d, playerName = %s",
player.getPlayerId(), player.getPlayerName());
log.info(info);
return "view";
}
}
模型
public class Player{
private int playerId;
private String playerName;
.... getters and setters
}
视图
提交表格
玩家
身份证:
姓名:
尝试返回“重定向:
将http请求传输到您的查看页面
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processForm(@ModelAttribute Player player, Model model) {
model.addAttribute("player", player);
String info = String.format("Player Submission: playerId = %d, playerName = %s",
player.getPlayerId(), player.getPlayerName());
log.info(info);
return "redirect:view";
// OR return "redirect:/view";
}
我建议的另一个方法是:
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processForm(@ModelAttribute("player")) {
log.info(info);
return "redirect:/view";
}
尝试return“重定向:
将http请求传输到您的查看页面
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processForm(@ModelAttribute Player player, Model model) {
model.addAttribute("player", player);
String info = String.format("Player Submission: playerId = %d, playerName = %s",
player.getPlayerId(), player.getPlayerName());
log.info(info);
return "redirect:view";
// OR return "redirect:/view";
}
我建议的另一个方法是:
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String processForm(@ModelAttribute("player")) {
log.info(info);
return "redirect:/view";
}
您的html文件位于何处?应该是ito/资源/templates@cralfaro是的,html文件位于/resources/templates中。我已经附上了上面的项目结构截图。你在哪里找到了你的html文件?应该是ito/资源/templates@cralfaro是的,html文件位于/resources/templates中。我已经附上了上面的项目结构截图。