类反射简化Struts应用程序的开发
【IT168技术文档】
三、如何应用类反射机制简化Struts应用程序的开发:
1、 先定义Action FormBean:
2、 编写通用的为ActionFormBean赋值的方法: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;
}
}
3、 在JavaBean封装的商业逻辑中调用Select 方法,然后在JSP页面上显示出来:/**//////////////////////////////////////////////////////////////////////////////
file://Function: 完成ResultSet对象向ArrayList对象为集合的对象的转化
file://Para:sql,指定的查询Sql
file://Para:className,Sql相对应得JavaBean/FormBean类的名字
file://Return:以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集
file://合的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;
}
}
4、 在Action的execute方法中调用getUsers()方法:file://Function:取得用户列表
file://Para:
file://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;
}
5、 在JSP中显示: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;
}
四、结语:<table width="700" class="1" border="1" cellspacing="1" align="center">
<tr>
<td class="list" >用户ID</td>
<td class="list" >姓 名</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>
我们通过运用类反射机制,在一个Struts应用开发中,完成了一个通用查询方法的实现。它使得程序员摆脱了在每个应用程序中都要编写枯燥的set、get等方法来访问ActionForm Bean,从而简化了Struts应用程序的开发。
0
相关文章
