我有一个带有属性狗的person bean,这是另一种对象类型。我正在尝试打印该属性的属性。
人有“狗”狗属性 狗具有字符串“名称”属性
我正在尝试显示该属性的属性:
public abstract class Person {
private Dog dog;
public Dog getDog(){
return dog;
}
public void setDog(Dog dog){
this.dog = dog;
}
}
public class Employee extends Person{
private int empId;
public int getEmpId(){
return empId;
}
public void setEmpId(int empId){
this.empId = empId;
}
}
public class Dog {
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName(){
return name;
}
}
Servlet:
foo.Dog dog = new foo.Dog();
dog.setName("Spike");
Person p = new Employee();
p.setDog(dog);
request.setAttribute("person", p);
request.getRequestDispatcher("/TestBean.jsp").forward(request,
response);
JSP:引用变量类型为Person,对象类型为Employee。
${person.dog.name}
$ {person.dog.name}没有打印任何内容。