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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM