程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 運用類反射機制簡化Struts應用程序開發

運用類反射機制簡化Struts應用程序開發

編輯:關於JAVA

1、先定義Action FormBean:

package com.bhsky.webis.system;
import org.apache.struts.action.*;
import javax.servlet.http.*;

public class UsersActionForm extends ActionForm {
private String usr_id;
private String usr_name;
public void setUsr_id(String usr_id) {
   this.usr_id = usr_id;
}
public String getUsr_id() {
   return usr_id;
}
public String getUsr_memo() {
   return usr_memo;
}
public void setUsr_name(String usr_name) {
   this.usr_name = usr_name;
}
}

2、編寫通用的為ActionFormBean賦值的方法:

//Function: 完成ResultSet對象向ArrayList對象為集合的對象的轉化
  //Para:sql,指定的查詢Sql
  //Para:className,Sql相對應得JavaBean/FormBean類的名字
  //Return:以類className為一條記錄的結果集,
  //完成ResultSet對象向ArrayList對象為集合的className對象的轉化

  public ArrayList Select(String sql,String className){
   ArrayList paraList=new ArrayList();
   try{
    if (conn == null){
     Connection();
    }
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    String recordValue="";
    Object c1=null;
    paraList=new ArrayList();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    while (rs.next()){
      c1=Class.forName(className).newInstance();
      for (int i=1; i<=columnCount; i++) {
       if(rs.getString(rsmd.getColumnName(i))!=null){
        recordValue=rs.getString(rsmd.getColumnName(i));
       }else{
        recordValue="";
       }
Method
m=c1.getClass().getMethod(getSetMethodName(rsmd.getColumnName(i)),
new Class[]{recordValue.getClass()});
       m.invoke (c1, new Object[]{recordValue});
      }
      paraList.add(c1);
    }
   }catch(SQLException ex){

}catch(ClassNotFoundException e){
}catch(NoSuchMethodException e) {
}catch(InvocationTargetException e){
}catch (IllegalAccessException e){
}catch(InstantiationException e){
} finaly{
     closeConnection();
return paraList;
}
  }

3、在JavaBean封裝的商業邏輯中調用Select 方法,然後在JSP頁面上顯示出來:

//Function:取得用戶列表
  //Para:
  //Return:返回用戶列表
  public ArrayList getUsers(){
    ArrayList ret=null;
    DatabaseManage db=new DatabaseManage();
    String sql=" select usr_id,usr_name "
      +" from users " ;
    ret=db.Select(sql," com.bhsky. webis.system.UsersActionForm");
    return ret;
  }

4、在Action的execute方法中調用getUsers()方法:

public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse httpServletResponse)
  {
   /**@todo: complete the business logic here, this is just a skeleton.*/
   UsersActionForm uaf=(UsersActionForm)actionForm;
   SystemService ubb=new SystemService();
   ArrayList userList=ubb.getUsers();
   request.setAttribute("userList",userList);
   ActionForward actionForward=actionMapping.findForward(url);
   return actionForward;
  }

5、在JSP中顯示:

<table width="700" class="1" border="1" cellspacing="1" align="center">
     <tr>
      <td class="list" >用戶ID</td>
      <td class="list" >姓&#160&#160名</td>
     </tr>
     <logic:present name="userList" scope="request">
      <logic:iterate name="userList" id="userList"
type="com.bhsky.webis.system.UsersActionForm">
     <tr>
      <td class="cell1" height="22"><bean:write name="userList"
property="usr_id"/></td>
      <td class="cell1" height="22"><bean:write name="userList"
property="usr_name"/></td>
     </tr>
     </logic:iterate>
    </logic:present>
</table>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved