繁体   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