1 向值栈中放入string字符串
public class ValueStackDemo extends ActionSupport {
//声明一个字符串变量
private String name;
//声明该变量的get属性
public void getName(String name) {
this.name = name;
}
public String execute()
{
return "success";
}
}
2 从值栈中取出String字符串
(1)在jsp页面导入标签:<%@ taglib uri="/struts-tags" prefix="s" %>
(2)利用标签取出,加上变量的名字取出变量:
<body>
This is my JSP page. <br>
<s:property value = "'haha'.length()"/>
<s:debug></s:debug>
<s:property value="name"/>
</body>
3 向值栈中放入对象
public class AddvsObj extends ActionSupport {
private User user=new User();
public User getUser() {
return user;
}
public String execute()
{
user.setName("grl");
user.setSex("女");
user.setAge(18);
return "success";
}
}
4 从值栈中取出对象数据
<body>
This is my JSP page. <br>
<s:property value = "'haha'.length()"/>
<s:debug></s:debug>
<s:property value="user.name"/>
<s:property value="user.age"/>
<s:property value="user.sex"/>
</body>
5 向值栈中添List集合
public class ListDemoAction extends ActionSupport {
private List<User> list=new ArrayList<User>();
public String execute()
{
User user1= new User();
user1.setName("grl");
user1.setSex("女");
user1.setAge(18);
User user2 = new User();
user2.setName("gxl");
user2.setSex("男");
user2.setAge(19);
list.add(user1);
list.add(user2);
return "success";
}
}
6 从值栈中取出List集合的三种方法:
<body>
This is my JSP page. <br>
<s:property value = "'haha'.length()"/>
<s:debug></s:debug>
<s:property value="user.name"/>
<s:property value="user.age"/>
<s:property value="user.sex"/>
<br/>
取出值栈数据的第一种方法:
<br/>
<s:property value="list[0].name"/>
<s:property value="list[0].sex"/>
<s:property value="list[0].age"/>
<s:property value="list[1].name"/>
<s:property value="list[1].sex"/>
<s:property value="list[1].age"/>
<br/>
取出List的第二种方法
<br/>
<s:iterator value="list">
<s:property value="name"/>
<s:property value="sex"/>
<s:property value="age"/>
</s:iterator>
<br/>
取出List的第三种方法:
<br/>
<s:iterator value="list" var="user">
<s:property value="#user.name"/>
<s:property value="#user.sex"/>
<s:property value="#user.age"/>
</s:iterator>
</body>