猿问

百里香的不同行为(通过数据库和resttemplate)

大家。我有两个应用程序。第一个是 CRUD spring boot JPA Web mvc 应用程序。它与这些代码一起工作得很好。(通过存储库数据库)


@RequestMapping(value = { "/newUser" }, method = RequestMethod.GET)

public String newUser(ModelMap model) {

    User user = new User();

    List<UserProfile> roles = userProfileRepository.findAll();


    model.addAttribute("user", user);

    model.addAttribute("allRoles", roles);

    return "registration";

}


@RequestMapping(value = { "/newUser" }, method = RequestMethod.POST)

public String saveUser(@Valid User user, BindingResult result,

                       ModelMap model) {


    if (result.hasErrors()) {

        List<UserProfile> roles = userProfileRepository.findAll();


        model.addAttribute("allRoles", roles);

        model.addAttribute("user", user);

        return "registration";

    }

    userRepository.save(user);


    return "registrationsuccess";

}

Registration.html 的一部分是


                    <select class="form-control input-sm" id="userProfiles" name="userProfiles" multiple="multiple" th:required="true">

                        <option th:each="role : ${allRoles}"

                                th:value="${{role}}"

                                th:text="${role.type}"

                                th:selected="${role.id == 1}"

                        >Role

                        </option>

                    </select>

上面的代码工作正常。


第二个应用程序是基于resttemplate的spring boot


@RequestMapping(value = { "/newUser" }, method = RequestMethod.GET)

public String newUser(ModelMap model) {

    User user = new User();


    RestTemplate restTemplate = new RestTemplate();

    List<UserProfile> userProfiles = Arrays.asList(restTemplate.getForObject("http://localhost:8080/api/roles", UserProfile[].class));

    ArrayList<UserProfile> roles = new ArrayList<>(userProfiles);


    model.addAttribute("user", user);

    model.addAttribute("allRoles", roles);

    return "registration";

}

多重选择形式与第一个应用程序相同(相同)但是第二个应用程序在调用registration.html时抛出异常

附言。我已经通过 PostMan 测试了所有方法(PUT、GET、DELETE 和 POST)。而且效果很好。我不明白百里香的不同行为..



哈士奇WWW
浏览 86回答 2
2回答

米脂

删除双括号语法,th:value="${{role}}"如堆栈跟踪中所述。双括号应用转换服务,通常用于日期等内容。这位于文档的数据转换部分。

撒科打诨

我刚刚实现了 org.springframework.core.convert.converter.Converter;@Componentpublic class RoleToUserProfileConverter implements Converter<Object, UserProfile>{&nbsp; &nbsp; @Autowired&nbsp; &nbsp; UserProfileService userProfileService;&nbsp; &nbsp; public UserProfile convert(Object element) {&nbsp; &nbsp; &nbsp; &nbsp; Integer id = Integer.parseInt((String)element);&nbsp; &nbsp; &nbsp; &nbsp; return userProfileService.findById(id);&nbsp; &nbsp; }}并稍微修改了这个表格&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select class="form-control input-sm" id="userProfiles" name="userProfiles" multiple="multiple" th:required="true">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option th:each="role : ${allRoles}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:value="${role.id}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:text="${role.type}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:selected="${role.id == 1}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; >Role&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>所以现在,thymeleaf 将值转换为对象。谢谢大家!
随时随地看视频慕课网APP

相关分类

Java
我要回答