Struts2 控制标签(四)输出 List中JavaBean的属性内容
1.新建JavaBean的实体类
package com.wl.control.Bean;public class StudentInfo { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
2.新建Action类
package com.wl.control.tagsAction;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.wl.control.Bean.StudentInfo;public class ShowListJavaBean extends ActionSupport { private List listJavaBean=new ArrayList(); public List getListJavaBean() { return listJavaBean; } public void setListJavaBean(List listJavaBean) { this.listJavaBean = listJavaBean; } @Override public String execute() throws Exception { StudentInfo studentInfo1=new StudentInfo(); studentInfo1.setName("张三"); studentInfo1.setAge(20); StudentInfo studentInfo2=new StudentInfo(); studentInfo2.setName("李四"); studentInfo2.setAge(23); StudentInfo studentInfo3=new StudentInfo(); studentInfo3.setName("王五"); studentInfo3.setAge(25); listJavaBean.add(studentInfo1); listJavaBean.add(studentInfo2); listJavaBean.add(studentInfo3); return "showlistjavabean"; }}
3.新建JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> 第一种写法:<br> <s:iterator value="listJavaBean" var="studentJavaBean"> 姓名:<s:property value="#studentJavaBean.name"/> 年龄:<s:property value="#studentJavaBean.age"/> <br> </s:iterator> 第二种写法:<br> <s:iterator value="listJavaBean"> 姓名:<s:property value="name"/> 年龄:<s:property value="age"/> <br> </s:iterator> </body></html>
注释:第二种写法,循环时将listJavaBean对象中的每一个元素放到值栈的顶端,直接获取即可。第一种写法,由于每次遍历的元素是一个JavaBean,所以需要使用"#"进行属性的反射取值。
4.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="Struts2.1" extends="struts-default"> <action name="showlistjavabean" /> 清晰,明了,学习了