[英]Can't reach the object from thymeleaf
I have a simple project with spring boot/thymeleaf and i have this problem about accessing an object from thymeleaf. 我有一个带有spring boot / thymeleaf的简单项目,并且我遇到了有关从thymeleaf访问对象的问题。 I have my user and role objects.
我有我的用户和角色对象。 Here is my User entity:
这是我的用户实体:
@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...
}
and Role entity: 和角色实体:
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String role;
// getters and setters...
}
controller that i'm providing objects: 我提供对象的控制器:
@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;
}
on the page, im trying to edit users roles with a select box. 在页面上,我正在尝试使用选择框来编辑用户角色。 and i want it to show the users role as selected value.
我希望它显示用户角色为选定值。 but it doesnt work.
但它不起作用。 Here is that code:
这是该代码:
<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>
the problem is about that user.role.role part. 问题是关于该user.role.role部分。 It gives
它给
SpringEL expression
SpringEL表达式
error. 错误。
When i use user.role
, i can access the role object; 当我使用
user.role
,我可以访问角色对象; but i can't use role's attributes. 但我不能使用角色的属性。 And the funny part is when i use completely different entities with exactly same configurations, i'm not getting any errors.
而有趣的部分是,当我使用完全相同的配置使用完全不同的实体时,我没有遇到任何错误。
Can anybody tell me what's the problem here? 有人可以告诉我这是什么问题吗?
Controller 调节器
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String method1(Model model, Principal principal) {
model.addAttribute("editRole", roles);
return "HTML_NAME";
}
Controller 2 控制器2
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String campaignPost(@ModelAttribute("roles") Roles roles, Principal principal){
roles.getName();
//roles object can be accessed
}
HTML 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>
I guess this might help You 我想这可能对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.