gpt4 book ai didi

java - 在jsp页面中获取LinkedHashMap中存储的元素

转载 作者:行者123 更新时间:2023-12-02 05:40:35 24 4
gpt4 key购买 nike

如何在Spring Controller 中的ModelAndView对象中添加的jsp页面中获取LinkedHashMap,请看下面的代码:

用户DTO:

public class UserDTO{

public byte[] data;
public String user;
public Long id;

public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

Controller :

@RequestMapping(value = "/showData", method = RequestMethod.GET)
public String showRequest(final HttpServletRequest request,
HttpServletResponse response, ModelMap model,
@RequestParam("ID") String Id) {
MyDTO myRequestDTO = new MyDTO();
String viewName = "myData";
final ModelAndView mav = new ModelAndView();

LinkedHashMap<Long,UserDTO> myList = myRequestsDTO.getAllRecords();
/* UserDTO userDoc = null;
Long userDocKey;
if(!myList.isEmpty()){
for (Map.Entry<Long,UserDTO> doc : myList.entrySet()) {
userDoc = doc.getValue();
userDocKey = doc.getKey();
System.out.println("User = " + userDoc.getUser() + " User Name = " + userDoc.getData());

} } */
mav.addObject("myList", myList);//want to get this myList in jsp page and get the details from that.
return viewName;
}

请建议如何获取存储在 ModelAndView 对象中的 LinkedHashMap 并从该映射中获取“userDoc.getUser()”、“userDoc.getId()”和“userDoc.getData()”,如我在上面的 Controller 代码中所示。我知道这可以使用 JSTL 来实现,但由于我是新手,请让我知道对我很有帮助的解决方案。

最佳答案

您可以使用 foreach 通过 JSTL 迭代您的 map 。

<c:forEach var="entry" items="${myList}">
Key : <c:out value="${entry.key}"/>

User : <c:out value="${entry.value.user}"/>
Id : <c:out value="${entry.value.id}"/>
Data : <c:out value="${entry.value.data}"/>

</c:forEach>

关于java - 在jsp页面中获取LinkedHashMap中存储的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24556976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com