2017-07-28 53 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个错误,说找不到对象。