2017-07-28 179 views
1

我有一個問題,如何使用內部類的變量。使用forEach爲內部類的JSP

在myne servlet中,我發送對象列表並且有內部類。

request.setAttribute("users", dao.getAllUsersandAdress()); 
    RequestDispatcher view = request.getRequestDispatcher(forward); 
    view.forward(request, response); 

發送至JSP 我在哪裏使用forEach。

<c:forEach items="${users}" var="user"> 
    <tr> 
     <td><c:out value="${user.idUser}" /></td> 
     <td><c:out value="${user.firstName}" /></td> 
     <td><c:out value="${user.lastName}" /></td> 
     <td><c:out value="${user.loginName}" /></td> 
     <td><c:out value="${user.email}" /></td> 
     <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.birthday}" /></td> 
     <td><c:out value="${user.isActive}" /></td> 
     <td><c:out value="${user.isAdmin}" /></td> 
     <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.createdTimeStamp}" /></td> 
     <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.lastupdateTimeStamp}" /></td> 
     <td><c:out value="${user.adressClass.zip}" /></td> 
     <td><c:out value="${user.adressClass.Country}" /></td> 
     <td><c:out value="${user.adressClass.City}" /></td> 
     <td><c:out value="${user.adressClass.District}" /></td> 
     <td><c:out value="${user.adressClass.Street}" /></td> 

     <td><a href="UserController?action=edit&userId=<c:out value="${user.idUser}"/>">Update</a></td> 
     <td><a href="UserController?action=delete&userId=<c:out value="${user.idUser}"/>">Delete</a></td> 
    </tr> 
</c:forEach> 

最終的Tomcat下去,直到他得到內部類的第一個對象和談話,這不是找。

代碼模型: 正如你可以看到有2的內部類,ADRESS和

集團
public class User { 

private int idUser; 
private String 
     firstName, 
     lastName, 
     loginName, 
     password, 
     email; 
private Date 
     birthday, 
     createdTimeStamp; 
private Timestamp 
     lastupdateTimeStamp; 
private boolean 
     isActive, 
     isAdmin; 

public Adress adressClass; 
public Group groupClass; 

public User() { 
    adressClass = new Adress(); 
    groupClass = new Group(); 
} 

public int getIdUser() { 
    return idUser; 
} 

public void setIdUser(int idAdress) { 
    this.idUser = idAdress; 
} 

public String getPassword() { 
    return password; 
} 

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

public Date getBirthday() { 
    return birthday; 
} 

public void setBirthday(Date birthday) { 
    this.birthday = birthday; 
} 

public boolean isActive() { 
    return isActive; 
} 

public void setActive(boolean active) { 
    isActive = active; 
} 

public boolean isAdmin() { 
    return isAdmin; 
} 

public void setAdmin(boolean admin) { 
    isAdmin = admin; 
} 

public Date getCreatedTimeStamp() { 
    return createdTimeStamp; 
} 

public void setCreatedTimeStamp(Date createdTimeStamp) { 
    this.createdTimeStamp = createdTimeStamp; 
} 

public Date getLastupdateTimeStamp() { 
    return lastupdateTimeStamp; 
} 

public void setLastupdateTimeStamp(Timestamp lastupdateTimeStamp) { 
    this.lastupdateTimeStamp = lastupdateTimeStamp; 
} 


public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public String getLoginName() { 
    return loginName; 
} 

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

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 
@Override 
public String toString() { 
    return "User [idUser=" + idUser + ", firstName=" + firstName 
      +", loginName="+ loginName 
      +", LastName=" + lastName + ", birthday=" + birthday + ", email=" 
      + email +",Adress country "+adressClass.country +"]"; 
} 

public class Adress{ 
    private int 
      idAdress, 
      zip; 
    private String 
      country, 
      city, 
      district, 
      street; 

    public int getIdAdress() { 
     return idAdress; 
    } 

    public void setIdAdress(int idAdress) { 
     this.idAdress = idAdress; 
    } 

    public int getZip() { 
     return zip; 
    } 

    public void setZip(int zip) { 
     this.zip = zip; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public String getDistrict() { 
     return district; 
    } 

    public void setDistrict(String district) { 
     this.district = district; 
    } 

    public String getStreet() { 
     return street; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 
} 

public class Group{ 
    private int idGroup; 
    private String role; 

    public int getIdGroup() { 
     return idGroup; 
    } 

    public void setIdGroup(int idGroup) { 
     this.idGroup = idGroup; 
    } 

    public String getRole() { 
     return role; 
    } 

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

}

Tomcat的寫下一個:

HTTP Status 500 - An exception occurred processing JSP page /index.jsp at line 9 
    javax.el.PropertyNotFoundException: Property 'adressClass' not found on type model.User 
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:291) 

任何建議? 謝謝。

+1

你發佈了一個有很多方法和變量的問題。請嘗試舉一個最簡單的例子來問你的問題 - 如果它更容易閱讀,那麼人們更有可能閱讀,理解和回答你的問題:) – MariusSiuram

回答

0

感謝RU community stackoverflow。 答案是 - 需要爲內部類創建getter和setter。 還有一件事,如果有人會使用MYNE代碼,有必要在JSP頁面旁邊的改變:

<td><c:out value="${user.adressClass.zip}" /></td> 
    <td><c:out value="${user.adressClass.Country}(Country on country)" /></td> 
    <td><c:out value="${user.adressClass.City(city)}" /></td> 
    <td><c:out value="${user.adressClass.District(district)}" /></td> 
    <td><c:out value="${user.adressClass.Street(street)}" /></td> 

因爲如果你不這樣做,JSP頁面給予u個錯誤,說找不到對象。