我有一个问题,如何使用内部类的变量。
在myne servlet中,我发送对象列表和内部类。
request.setAttribute("users", dao.getAllUsersandAdress());
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
发送到JSP 我在哪里使用每个。
<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一直走到内心阶层的第一个对象,然后说话就找不到了。
型号代码: 正如您所看到的,有两个内部类,Adress和Group
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)
有什么建议吗? 感谢。
答案 0 :(得分:0)
感谢RU社区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页面会给你错误并说找不到该对象。