2016-02-29 52 views
2

我有一个Spring Boot 1.3应用程序,它在HTML上使用Thymeleaf。我有用户页面,当我编辑它们时,我希望从列表中选择用户机构。我的窗口正确显示了一切,但是当我选择机构时,控制器不显示选定的值。Thymeleaf和SpringBoot的下拉列表

这里是我的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", [email protected](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”从用户界面获取用户对象时,该机构为空。

编辑 我用Sergios建议使用select th:field="${user.institution}"以及select th:field="*{institution}"

但给我一个错误:

org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'user' on field 'institution': rejected value [[email protected]]; codes [typeMismatch.user.institution,typeMismatch.institution,typeMismatch.com. .security.Institution,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.institution,institution]; arguments []; default message [institution]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.security.Institution' for property 'institution'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.lang.Long for value '[email protected]'; nested exception is java.lang.NumberFormatException: For input string: "com. [email protected]"]

不知道如果我正确读这篇文章,但这是否意味着Thymeleaf是试图将Institution.name传递给user.institution字段?

任何人都可以提供任何建议如何做到这一点?

回答

5

您已忘记指示从那里必须采取所选择的值的字段:

例:I supousse用户类具有用于机构的属性。

<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

编辑: 你需要告诉你的应用程序是如何转换的单位信息的1D内部格式(字符串型)返回到实体机构。为此,你必须使用一个转换器。

首先改变的选项来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); 
    } 

} 
+0

当我做出的改变,我得到类型转换错误“org.springframework.core.convert.ConversionFailedException:无法从java.lang.String类型转换为类型java.lang.Long中的价值” [email protected]' ;嵌套异常是java.lang.NumberFormatException:对于输入字符串:“com.security。Institution @ 27d1fda1“'我的用户有一个机构属性,我的下拉列表是这种类型 - 所以不知道为什么它不设置对象 – sonoerin

+0

当你在外勤机构发送表格时,你发送Insitution的名称,并在你的用户你有一个Institution类的属性,那么Spring试图从DB恢复实体,但它不能因为Spring有一个字符串而且Institution的ID是Long。 –

+0

[Continuation] 首先你需要返回院校的Id值: 日:值=“$ {} dropDownItem.id”(或ID属性的名称) 而在你的应用程序,你需要有从字符串到研究所的Convert类,在这种转换可以将字符串转换为长并使用您的InstitutionRepository获得该机构。 –