亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

百里香的不同行為(通過數據庫和resttemplate)

百里香的不同行為(通過數據庫和resttemplate)

哈士奇WWW 2023-09-06 16:15:38
大家。我有兩個應用程序。第一個是 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)。而且效果很好。我不明白百里香的不同行為..
查看完整描述

2 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

刪除雙括號語法,th:value="${{role}}"如堆棧跟蹤中所述。

雙括號應用轉換服務,通常用于日期等內容。這位于文檔的數據轉換部分。


查看完整回答
反對 回復 2023-09-06
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

我剛剛實現了 org.springframework.core.convert.converter.Converter;


@Component

public class RoleToUserProfileConverter implements Converter<Object, UserProfile>{


    @Autowired

    UserProfileService userProfileService;


    public UserProfile convert(Object element) {

        Integer id = Integer.parseInt((String)element);

        return userProfileService.findById(id);

    }

}

并稍微修改了這個表格


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

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

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

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

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

                    >Role

                    </option>

                </select>

所以現在,thymeleaf 將值轉換為對象。謝謝大家!


查看完整回答
反對 回復 2023-09-06
  • 2 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號