Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring MVC thymeleaf无法解析对象属性和字段_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring MVC thymeleaf无法解析对象属性和字段

Spring 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

我刚刚开始发现SpringMVC,并被这个简单的项目困住了。在模板index1.html中,无法解析播放器对象(th:object=“${player}”)和字段值(th:field=“{playerId}”),(th:field=“{playerName}”)。在带有(${player.playerId})和(${player.playerName})的模板index2.html中也存在相同的情况。你能说明原因吗

PlayerController.java

@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中。我已经附上了上面的项目结构截图。