簡體   English   中英

無法從百里香葉觸及物體

[英]Can't reach the object from thymeleaf

我有一個帶有spring boot / thymeleaf的簡單項目,並且我遇到了有關從thymeleaf訪問對象的問題。 我有我的用戶和角色對象。 這是我的用戶實體:

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(unique = true)
    private String username;
    private String password;
    private int enabled;
    @ManyToOne
    private Role role;

    // getters and setters...

}

和角色實體:

@Entity
@Table(name = "roles")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String role;

    // getters and setters...
}

我提供對象的控制器:

@RequestMapping(value = "edit", method = RequestMethod.GET)
public ModelAndView editRoles(){
    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("users", userService.getAll());
    modelAndView.addObject("roles", roleService.getAll());
    modelAndView.setViewName("editRole");
    System.out.println(userService.findUser(2).getRole().getRole());
    return modelAndView;
}

在頁面上,我正在嘗試使用選擇框來編輯用戶角色。 我希望它顯示用戶角色為選定值。 但它不起作用。 這是該代碼:

<tr th:each="user : ${users}">
    <td >
        <select class="form-control" id="sel1" th:field="*{roles}" >
            <option th:each="role : ${roles}" th:value="${role.id}" th:text="${role.role}" th:selected="${user.role.role}">
            </option>
        </select>
    </td> 
</tr>

問題是關於該user.role.role部分。 它給

SpringEL表達式

錯誤。

當我使用user.role ,我可以訪問角色對象; 但我不能使用角色的屬性。 而有趣的部分是,當我使用完全相同的配置使用完全不同的實體時,我沒有遇到任何錯誤。

有人可以告訴我這是什么問題嗎?

調節器

@RequestMapping(value = "/create", method = RequestMethod.GET)
public String method1(Model model, Principal principal) {
    model.addAttribute("editRole", roles);
    return "HTML_NAME";
}

控制器2

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String campaignPost(@ModelAttribute("roles") Roles roles,  Principal principal){
    roles.getName();
    //roles object can be accessed
}

HTML

<form th:action="@{/edit}" method="post" id="formName">
    <select class="form-control" th:value="${objectparamater}" name="gender" id="gender">
        <option disabled="disabled" selected="selected" > -- select the location --</option>
        <option>male</option>
        <option>female</option>
        <option>choose not to tell</option>
    </select>
</form>

我想這可能對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM