Struts2:在jsp页面获取action中的没有set/get方法的属性
本人没没有用过Struts2,所以不是很了解。但我知道在页面获取action中的属性的话,那么属性就必须有set
/get方法,但是没有set/get方法的属性也能在页面中获取到,求高人指点。下面是代码:
bean:
public class Student {
private int id;
private String name;
private int age;
private String sex;
private String address;
public Student() {
}
public Student(int id, String name, int age, String sex, String address) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class StudentService {
public String message = "这是提示信息";
public List<Student> getStudents(){
List<Student> stuList = new ArrayList<Student>();
Student s1 = new Student();
s1.setId(1);
s1.setName("张三");
s1.setAge(20);
s1.setSex("男");
s1.setAddress("广州市天河区");
Student s2 = new Student();
s2.setId(2);
s2.setName("李四");
s2.setAge(21);
s2.setSex("男");
s2.setAddress("广州市萝岗区");
Student s3 = new Student();
s3.setId(3);
s3.setName("文静");
s3.setAge(18);
s3.setSex("女");
s3.setAddress("广州市白云区");
Student s4 = new Student();
s4.setId(4);
s4.setName("齐名");
s4.setAge(28);
s4.setSex("男");
s4.setAddress("广州市番禺区");
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
stuList.add(s4);
return stuList;
}
}
public class StudentAction {
public StudentService service = new StudentService();
public List<Student> stuList;
public String getStudents(){
stuList = service.getStudents();
//ValueStack vs = ActionContext.getContext().getValueStack();
//System.out.println(vs.toString());
return "SUCCESS";
}
}
<body>
<table>
<thead>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
</tr>
</thead>
<tbody>
<s:iterator value="stuList">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="address"/></td>
</tr>
</s:iterator>
</tbody>
</table>
<s:property value="stuList.get(1).name"/><br/>
<s:property value="service.message"/>
</body>
序号姓名年龄性别地址
1张三20男广州市天河区
2李四21男广州市萝岗区
3文静18女广州市白云区
4齐名28男广州市番禺区
李四
这是提示信息//这是service层中的属性