当前位置 : 主页 > 编程语言 > java >

jsp – JSTL从会话中获取对象

来源:互联网 收集:自由互联 发布时间:2021-06-25
我在会话中放了一个对象: session.setAttribute("userDTO", currentUser); 我试图用EL显示它.我已成功使用scriplets(证明对象在会话中设置正常). JSP中的代码: body % UserDTO userdto=(UserDTO)session.getAttr
我在会话中放了一个对象:

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>

scriplets显示用户名,但在“会话的用户名:”后没有显示任何内容.为什么?

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;
    }
}
第1步:将jstl-x.x.jar放在lib文件夹中或将依赖项放在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>
网友评论