Thymeleaf用数据填充对象?

时间:2017-04-18 12:23:15

标签: java html spring maven thymeleaf

我试图将用户的对象发送到我的html页面并填写他的属性,但是我这样做会给我带来很多错误" invaild setter and getter"或"接口类"
这是对象的声明

User user = new Student() ;

请注意,用户是界面和 学生是班级实施用户
这是我的用户界面

public interface User{


public Account getAccount();
    public void setAccount(Account account);
    public double getScore();
    public void setScore(double score);
    double getGameScore(int gameID);
    public void updateScore(double score);
    public void addGame(int gameID, double score);
    public Map<Integer, Double> getPlayedGames();
    public void setPlayedGames(Map<Integer, Double>();  
}

这是班级学生

public class Student implements User {
    private int id ; 
    Account account = new Account();
    private Map<Integer, Double> playedGames = new HashMap<Integer, Double>();    
    /**setter & getter*/
}

和我的html表单

<form action="#" th:action="@{/view/Register}" th:object="${user}"
    method="post">
    <p> Name: <input type="text" th:field="*${user.getAccount().userName}" required="required" /></p>
    <p>Email : <input type="text" th:field="${user.getAccount().email}" required="required" /></p>
    <p>Password : <input type="text" th:field="${user.getAccount().password}" required="required" /></p>
    <p> Profile :<input type="file" name="picture" /></p>
    <p>Male : <input type="radio" th:field="${user.getAccount().gender}" value="Male"
            checked="true" /> Female : <input type="radio" th:field="${user.getAccount().gender}"
            value="Female" /></p>
    <input type="submit" />
</form>

从控制器传递

@RequestMapping(value = "view/Register", method = RequestMethod.GET)
public String register(Model model) {

    User user = new Student () ; 
    model.addAttribute("user", user);

    return "view/Register";
}

我怎么能让它起作用?

0 个答案:

没有答案