Using class reflection mechanism to simplify the development of struts application

Source: Internet
Author: User

1, first define 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. Write the common method of assigning values to Actionformbean:

//function: Completes the transformation of the ResultSet object to the object of the ArrayList object for the collection
//para:sql, the specified query SQL
//para:classname, SQL corresponds to the Javabean/formbean class name
//return: a result set with a class classname as a record,
// Completes the transformation of the ResultSet object to the ArrayList object as a collection of ClassName objects

Public ArrayList Select (String sql,string className) {
Arrayl ist 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) {
}ca TCH (InvocationTargetException e) {
}catch (illegalaccessexception e) {
}catch (instantiationexception e) {
} finaly{
CloseConnection ();
return paralist;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.