小编典典

JSTL从会话获取对象

jsp

我在会话中放置了一个对象:

session.setAttribute("userDTO", currentUser);

我正在尝试使用EL显示它。我已经成功实现了scriplets(证明可以在会话中正确设置对象)。

JSP中的代码:

<body>
    <% UserDTO userdto=(UserDTO)session.getAttribute("userDTO"); %>
    <%=userdto.getUsername() %>
    Username from session:<c:out value="${sessionScope.userDTO.username }"/>
</body>

片段显示用户名,但“会话中的用户名:”之后不显示任何内容。为什么?

UserDTO类:

public class UserDTO {
    private int ID;
    private String email;
    private boolean emailConfirmed;
    private String username;
    private String role;
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public boolean isEmailConfirmed() {
        return emailConfirmed;
    }
    public void setEmailConfirmed(boolean emailConfirmed) {
        this.emailConfirmed = emailConfirmed;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

阅读 371

收藏
2020-06-08

共1个答案

小编典典

步骤1: 放入jstl-x.x.jarlib文件夹或将依赖项放入Maven。

步骤2:jsp档案中

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 ...
 <body>
     <% UserDTO userdto=(UserDTO)session.getAttribute("userDTO"); %>
     <%=userdto.getUsername() %>
     Username from session:<c:out value="${sessionScope.userDTO.username }"/>
      ...
 </body>
2020-06-08