JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Web Framework » Struts  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
話題被移動
該話題已被移動 - browser , 2004-07-14 09:11
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 分享:如何在Struts AP中使用DynaActionForm, DynaBean, DynaClass [精華]
leohong





發文: 125
積分: 1
於 2004-07-14 09:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在Struts中有搭配jakarta其他一些好用的packages,敝人最近研究DynaActionForm, DynaBean, DynaClass發現可以減低不少coding loading
我們以兩種情形來作探討:
假設我們在資料庫中有一個table: AccountProfile
table fields:
  1.USERNAME
  2.PASSWORD
  3.FULL_NAME
  4.ROLE
  5.TEL
  6.MOBILE_TEL
  7.ADDRESS

case 1:如果從資料庫中取出的資料僅供一支報表使用,亦即取出之資料的可重覆使用性不高
1.不需要特別設計一個bean來存放此取出之資料集合
2.org.apache.commons.beanutils.RowSetDynaClass提供類似ResultSet的供能
3.xxxAction.java
1
2
3
4
5
...
AccountProfileDAO accountProfileDAO = new AccountProfileDAO(con, session);
Collection accountProfileList = accountProfileDAO.findAll();
request.setAttribute("accountProfileList", accountProfileList);
...

4.AccountProfileDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
...
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList list = new ArrayList();
String sql = "select * from AccountProfile";
try {
  ps = con.prepareStatement(sql);
  rs = ps.executeQuery();
  org.apache.commons.beanutils.RowSetDynaClass dynaClassResultSet = new org.apache.commons.beanutils.RowSetDynaClass(rs);
   list = (ArrayList)dynaClassResultSet.getRows();//每一個row皆是一個DynaBean,細節請參照RowSetDynaClass的API
   ...
   return list;

5.xxx.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
java.util.ArrayList accountProfileList = (java.util.ArrayList)request.getAttribute("accountProfileList");
//顯示每一筆傳回之record有多少個fields
org.apache.commons.beanutils.DynaBean bean = (org.apache.commons.beanutils.DynaBean)accountProfileList.get(0);
System.out.println(bean.getDynaClass().getDynaProperties().length);
 
//顯示每一筆record的field name
for ( int counter = 0; counter < bean.getDynaClass().getDynaProperties().length; counter++ )
  System.out.println(bean.getDynaClass().getDynaProperties()[counter].getName());
 
//顯示所有從AccountProfile 中所取出之內容
for ( int counter = 0; counter < accountProfileList.size(); counter++ ) {
  org.apache.commons.beanutils.DynaBean b = (org.apache.commons.beanutils.DynaBean)accountProfileList.get(counter);
  int fieldCount = b.getDynaClass().getDynaProperties().length;
  for ( int loop = 0; loop < fieldCount; loop++ ) {
    //顯示每一筆記錄之內容
    System.out.print(b.get(b.getDynaClass().getDynaProperties()[loop].getName()) + "   ");
  }
  System.out.println("\r\n");
}


case 2:如果從資料庫中取出的資料會供大量報表使用,我們會需要一個bean來儲存
1.一般我們會設計一個實體的Java Bean,但此種情形只要需求一變動,就需要更改Java Bean的內容,jakarta提供DynaActionForm來解決這個問題
2.struts-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
<form-beans>
  ...
  <form-bean name="accountInfo" type="org.apache.struts.action.DynaActionForm">
    <form-property name="USERNAME" type="java.lang.String"/>
    <form-property name="PASSWORD" type="java.lang.String"/>
    <form-property name="FULL_NAME" type="java.lang.String"/>
    <form-property name="ROLE" initial="user" type="java.lang.String"/>
    <form-property name="TEL" type="java.lang.String"/>
    <form-property name="MOBILE_TEL" type="java.lang.String"/>
    <form-property name="ADDRESS" type="java.lang.String"/>
  </form-bean>
</form-beans>
...
<action-mappings>
  ...
  <!-- 此action中之name="accountInfo" 需與上面<form-bean name="accountInfo" 相同 -->
  <action path="/browseAccountProfile" type="com.anmei.actions.BrowseAccountProfileAction"
          input="/admin.do" name="accountInfo">
    <forward name="success" path="/pages/browseAccountProfile.jsp"/>
  </action>
</action-mappings>


3.xxxAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
...
public ActionForward execute(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response)
                      throws IOException, ServletException {
  ...
  //透過下方之code, struts會將傳入本method中之form的資料填入我們定義在struts-config.xml中form-bean: accountInfo
  DynaActionForm accountInfo = (DynaActionForm)form;
  AccountProfileDAO accountProfileDAO = new AccountProfileDAO(con, session);
  
  //將accountInfo傳入DAO之用意是要取得該bean的class type
  Collection accountProfileList = accountProfileDAO.findAll(accountInfo);
  request.setAttribute("accountProfileList", accountProfileList);
  ...

4.AccountProfileDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Collection findAll1(DynaActionForm accountInfo) {
  ...
  PreparedStatement ps = null;
  ResultSet rs = null;
  ArrayList list = new ArrayList();
  String sql = "select * from AccountProfile";
  try {
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();
    while ( rs.next() ) {
      org.apache.commons.beanutils.DynaClass beanType = (org.apache.commons.beanutils.DynaClass)accountInfo.getDynaClass();
      org.apache.commons.beanutils.DynaBean accountInfoObj = (org.apache.commons.beanutils.DynaBean)beanType.newInstance();
      accountInfoObj.set("USERNAME", rs.getString(1));
      accountInfoObj.set("PASSWORD", rs.getString(2));
      accountInfoObj.set("FULL_NAME", rs.getString(3));
      accountInfoObj.set("TEL", rs.getString(4));
      accountInfoObj.set("MOBILE_TEL", rs.getString(5));
      accountInfoObj.set("ADDRESS", rs.getString(6));
      
      list.add(accountInfoObj);
    }
    ...
    return list;
    

5.xxx.jsp
1
2
3
4
5
6
7
...
java.util.ArrayList accountProfileList = (java.util.ArrayList)request.getAttribute("accountProfileList");
//顯示所有從AccountProfile 中取出之USERNAME
for ( int counter = 0; counter < accountProfileList.size(); counter++ ) {
  org.apache.commons.beanutils.DynaBean accountProfile = (org.apache.commons.beanutils.DynaBean)accountProfileList.get(counter);
  System.out.println(accountProfile.get("USERNAME"));
}


leohong edited on 2004-07-14 12:39
reply to postreply to post
» JWorld@TW »  Web Framework » Struts

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8