【发布时间】:2016-06-12 15:08:43
【问题描述】:
我有一个在 HTML 上使用 Thymeleaf 的 Spring Boot 1.3 应用程序。我有用户页面,当我编辑它们时,我希望从列表中选择用户机构。我有正确显示所有内容的窗口,但是当我选择机构时,控制器没有显示所选值。
这是我的 HTML:
<div class="form-group">
<label class="col-md-3 control-label">Institution</label>
<div class="col-md-5">
<div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}">
<select>
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.name}"
th:text="${dropDownItem.name}" />
</select>
</div>
<div th:if="${institutionList == null or lists.isEmpty(institutionList)}">
<div>"No Institutions were found, please create some first"</div>
</div>
</div>
</div>
这是我的控制器
@Controller
@PreAuthorize("hasRole('Admin')")
@RequestMapping("/admin")
public class AdminController {
@Transactional
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String checkPersonInfo(@ModelAttribute User user, Model model, Authentication authentication) {
// user does not have the selected institution set
customUserDetailsService.createNewUser(user);
updateModelWithAllUsers(model);
return "admin";
}
private void updateModelWithAllUsers(Model model) {
model.addAttribute(USERLIST, customUserDetailsService.findAllUsers());
model.addAttribute(INSTITUTION_LIST, institutionService.findAll());
model.addAttribute("user", new User());
}
...
}
这是我的用户:
@Entity
@Table(name = "Users")
public class User implements UserDetails {
@Id
private String username;
@Column(nullable = false)
private String password;
private boolean enabled;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private String companyName;
private String email;
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name="Authorities", joinColumns=@JoinColumn(name="username"))
@Column(name="authority")
private Set<String> roles = new HashSet<String>();
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "institutionId", nullable = false)
private Institution institution;
... getters & setters
}
还有我的机构:
@Entity
@Table(name = "Institution")
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long institutionId;
private String name;
@OneToMany(mappedBy = "institution", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
List<User> users = new ArrayList<User>();
... getters & setters
}
“/addUser”从UI获取User对象时,机构为空。
编辑
我使用 Sergios 建议使用 select th:field="${user.institution}" 以及 select th:field="*{institution}"
但这给了我一个错误:
org.springframework.validation.BeanPropertyBindingResult: 1 个错误 字段“机构”上的对象“用户”中的字段错误:拒绝值 [com.security.Institution@3e3945d2];代码 [typeMismatch.user.institution,typeMismatch.institution,typeMismatch.com。 .security.Institution,typeMismatch];论据 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [user.institution,institution];论据 [];默认消息 [机构]];默认消息 [无法转换的属性值 键入“java.lang.String”到所需类型“com.security.Institution” 对于财产“机构”;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 失败 从 java.lang.String 类型转换为 java.lang.Long 类型以获得值 'com.security.Institution@3e3945d2';嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“com. .security.Institution@3e3945d2"]
不确定我是否正确阅读,但这是否意味着 Thymeleaf 正在尝试将机构名称传递给 user.institution 字段?
任何人都可以就如何做到这一点提供任何建议吗?
【问题讨论】:
标签: spring-mvc spring-boot thymeleaf