struts2从action向jsp传参数

struts2从action向jsp传参数:

 

1.在action类里面的成员变量域那里写上你要返回给jsp的变量和相应的get  set方法(比如list)..

  在execute方法里为list填充了数据..

 

2.直接在jsp调用.

  1).加上<%@ taglib uri="/struts-tags" prefix="s" %>标签

  2).<s:iterator value="list">
          <s:property/>
        </s:iterator>

  另外如果list里面放的是一个model,例如User(包含有name和password),

    则可以:

      <s:iterator value="list">
              <s:property value="name"/>

          <s:property value="password"/>
            </s:iterator>

 

 

附上简单的一个例子:

action:

View Code
 1         private List<String> testnames;
 2     
 3     public List<String> getTestnames() {
 4         return testnames;
 5     }
 6     public void setTestnames(List<String> testnames) {
 7         this.testnames = testnames;
 8     }
 9 
10 
11         public String getUsers(){
12         testnames = new ArrayList<String>();
13         testnames.add("kim");
14         testnames.add("deng");
15         
16         return SUCCESS;
17     }

jsp:

 

View Code
 1 <%@ page language="java" contentType="text/html; charset=GB18030"
 2     pageEncoding="GB18030"%>
 3 <%@ taglib uri="/struts-tags" prefix="s" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <s:iterator value="testnames">
12         <s:property/>
13     </s:iterator>
14 </body>
15 </html>

posted on 2012-05-09 13:52  saobchj  阅读(4605)  评论(0编辑  收藏  举报

导航