简体   繁体   中英

how to access domain class object [only one class object] in jsp using JSTL

Profile.jsp

<c:forEach var="p" items="${profile}">
            <tr>
            <td>${p.userId} </td>
             <td><c:out value="${p.name}" /></td>
            <td>${p.phone}</td>
            <td>${p.email}</td>
            <td>${p.address}</td>
            <td>${p.loginName}</td>
            </c:forEach>

userController.java

  @RequestMapping("/profile")
public String profile(Model m,HttpSession session) {
    Integer userId = (Integer) session.getAttribute("userId");
    m.addAttribute("profile", userService.profile(userId));
    return "profile"; // JSP
}

Domain class

package in.capp.domain;

 public class User {
private Integer userId;
private String name;
private String phone;
private String email;
private String address;
private String loginName;
private String password;
private Integer role;
private Integer loginStatus;


public User() {
    super();
}

public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getLoginName() {
    return loginName;
}

public void setLoginName(String loginName) {
    this.loginName = loginName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Integer getRole() {
    return role;
}

public void setRole(Integer role) {
    this.role = role;
}

public Integer getLoginStatus() {
    return loginStatus;
}

public void setLoginStatus(Integer loginStatus) {
    this.loginStatus = loginStatus;
}

 }

Error in browser

    org.apache.jasper.JasperException: An exception occurred processing JSP 
   page /WEB-INF/view/profile.jsp at line 46

  43:               </tr>
  44:               </c:if>
  45:               ${profile} }
   46:               <c:forEach var="p" items="${profile}">
  47:               <tr>
 48:                <td>${p.userId} </td>
 49:                 <td><c:out value="${p.name}" /></td>

If I just print ${profile} in my JSP in using h1 tag then it print Object [in.capp.domain.User@812ee27] successfully. It means i am getting object[One object only] but can't iterate that object how can i print that object values in my jsp?? i have added jstl jar in my project but facing this issue.

It looks like you are doing almost everything right, but you are using the wrong jstl tag. c:forEach would be used to iterate through a collection, something you don't need right now. Try just:

<tr>
<td><c:out value="profile.userId" /></td>
<td><c:out value="profile.name" /></td>
<td><c:out value="profile.phone" /></td>
<td><c:out value="profile.email" /></td>
<td><c:out value="profile.address" /></td>
<td><c:out value="profile.loginName" /></td>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM