---
title: "JSP中得到action中属性的方法"
author: "xu520"
date: "2012-08-27T19:51:00Z"
tags: []
slug: "jsp-action"
draft: false
lastmod: "2012-08-28T20:22:03Z"
id: 1665815
url: "https://xu520.iteye.com/blog/1665815"
---

1、例如action中有一个pageDAO对象，该对象有一个int型的currentPage属性，可以在jsp中这样

得到currentPage的值：

//注意，action中要有pageDAO对象的get方法，属性currentPage也要有get方法。

int test=(Integer)request.getAttribute("pageDAO.currentPage");

2、EL表达式：${pageDAO.currentPage}

3、使用struts2的标签

&lt;s:property value="pageDAO.currentPage"/&gt;

Action中有一个属性 List&lt;Person&gt; persons;(Person是一个类 有name和age属性)现在通过struts.xml配置 转向到show.jsp问题就是：在show.jsp取得persons中的name和age

1)action定义getPersons()

2)Person中定义getName()和getAge()

3):&lt;s:iterator id="u" value="persons"&gt;

&lt;s:property value='#u.getName()'/&gt;

&lt;s:property value='#u.getAge()'/&gt;

struts2还需这么传值吗？

把要传的值定义为action的成员变量，为其建立set 、get方法，在action中为其赋值；

然后在jsp中直接引用其名即可。

例如：action中的book在jsp中引用：&lt;s:property value="book.bookname" /&gt;

&lt;/s:iterator&gt;