我有一个SpringBoot1.3应用程序,它在HTML上使用Thymeleaf。我有一页用户,当我编辑他们,我希望用户机构从一个列表中选择。我有一个窗口正确地显示了一切,但是当我选择机构时,控制器没有显示所选的值。
以下是我的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中获取用户对象时,机构为null。
我使用Sergios建议使用select th:field="${user.institution}"
和select th:field="*{institution}"
但这给了我一个错误:
org.springframework.validation.BeanPropertyBindingResult: 1在字段‘com.security.Institution@3e3945d2’上的对象'user‘中的字段错误:拒绝值com.security.Institution@3e3945d2;代码typeMismatch.user.institution、typeMismatch.institution、typeMismatch.com。参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码,制度;参数[];默认消息机构;默认消息未能将属性' java.lang.String‘类型的属性值转换为属性’制度‘所需的类型'com.security.Institution’;嵌套异常是org.springframework.core.convert.ConversionFailedException:未能从java.lang.String类型转换为值‘com.security.Institution@3e3945d2’的java.lang.Long类型;嵌套异常是java.lang.NumberFormatException: For input string:"com。.security.Institution@3e3945d2“
不确定我是否正确地阅读了这篇文章,但这是否意味着Thymeleaf试图将Institution.name传递给user.institution字段?
有人能就如何做到这一点提出建议吗?
发布于 2016-02-29 06:42:13
您已经忘记指出必须从其中选取所选值的字段:
示例:我假设用户类有一个机构属性。
<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 th:field="*{institution}">
<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>
更多信息:http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dropdownlist-selectors
编辑:您需要向您的应用程序说明如何将表单内返回的Id (字符串类型)转换为“机构”实体。为此,您必须使用转换器。
首先,将选项的值更改为institutionId:
<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 th:field="*{institution}">
<option th:each="dropDownItem : ${institutionList}"
th:value="${dropDownItem.institutionId}"
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>
您必须创建一个实现转换器接口的类。
@Component
public class StringToInstitution implements Converter<String, Institution> {
@Autowired
private InstitutionRepository repository; //Or the class that implments the repository.
@Override
public Institution convert(String arg0) {
Long id = new Long(arg0);
return repository.findOne(id);
}
}
https://stackoverflow.com/questions/35701905
复制