在我的Spring应用程序中,用户可以保存Address Object
,其中包含两个字符串变量address
和addressnickname
。然后,用户可以将此对象保存到Address Objects
列表中。 address
和addressnickname
值通过发送到Spring Boot Controller的AJAX查询存储在MySQL表中。
控制器中的调试(以及实际查看MySQL cmd.exe中的表)将显示该对象已成功保存到用户的Address Objects
列表中。
现在我想传递每个地址对象address
(其中address
等于" 123假街")和addressnickname
(其中addressnickname
等于"我的房子")到HTML Datalist,以便用户可以在谷歌地图中的路线规划器中选择保存的位置作为其目的地或输入地址没有保存。类似于下面的截图。
上面的数据列表已经硬编码:
<datalist id="places">
<option value = "123 Fake St, Dublin, Ireland">Home</option>
</datalist>
但是可以通过从列表中传递每个对象来生成与屏幕截图相同的结果吗?我尝试过以下方法,但没有运气。
<datalist id="places">
<c:forEach var="o" items="${savedAdresses}">
<option value=<c:out value="${o.address}><c:out value="${o.addressnickname}" /></option>
</c:forEach>
</datalist>
P.S保持问题简洁我没有从控制器,用户对象和地址对象类中省略代码,但是他们是否需要回答我待命的问题以包含它们。谢谢!
编辑从Controller类添加saveAddress()方法
@RequestMapping(value = "/saveAddress", method = RequestMethod.POST)
public @ResponseBody String saveAddressToUser(Address address,
@RequestParam("address") String address,
@RequestParam("addressnickname") String addressnickname) {
// Authenticates the current logged in user
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String username = loggedInUser.getName(); // Authentication for
// Saves address to MySQL DB, and gets the logged in user profile from the DB
addressRepository.save(address);
User user = userRepository.findByUsername(username);
//Debug to show username of user, and the String address, and addressnickname variables successfully passing into the controller
System.out.println(username + " just saved Address + "|called: " + address + "|nickname: " + addressnickname);
// Adds the address object to the User's List of Address Object
user.addAddress(address);
// Updates the user information
userRepository.save(user);
return "welcome";
}
用户对象变量
private Long id;
private String username;
private String password;
private String passwordConfirm;
private List<Area> savedAreas;
private List<Address> savedAddress; <-- List Of addresses the user saves address object to
答案 0 :(得分:0)
根据@GoldDragonTSU的调试建议,我意识到我忘记在控制器中添加另一个实际将列表打印到JSP页面的方法。上面的Controller中的方法将数据保存到列表中,但实际上没有任何内容可以打印列表。我添加了以下方法并正确配置了映射。
// Added the org.springframework.ui.Model as a method parameter for
// use in passing Addresses from this Controller method to the JSP View
@RequestMapping(value = "/addressCheckerPage", method = RequestMethod.GET)
public String addressChecker(@Valid Addresses addresses, BindingResult bindingResult, Model model) {
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String username = loggedInUser.getName(); // Authentication for
addressesRepository.save(addresses);
User user = userRepository.findByUsername(username);
List<Addresses> savedAddresses = user.getSavedAddresses();
for (int i = 0; i < savedAddresses.size(); i++) {
// Do work with a user's individual saved addresses
}
// Make the user's saved address list available to the JSP:
model.addAttribute("savedAddresses ", savedAddresses );
return "addressChecker";
}