struts下开发

休息来一早上,现在来吧poi应用移植到struts下,周末快结束了,我必须迅速了结这个任务

,为了简单开发,我还是用eclipse2.1+easy struts ,恩如果你正在我的日记,(实际上,我倒是一直努力将它写成一个给初学者的实例教程)并对easy struts插件不是很了解,你应该先看看"使用 Easy Struts for Eclipse 开发 Struts "这篇文章(在网站内我已经给过它的连接),现在我的各种插件已经配置完成,  开始开发吧!!

 1 ,创建一个tomcat应用,并添加easy struts 支持,看过文章后 这很使简单。

 2 ,创建一个fileuploadfom  and fileuploadbean   ,还是很简单,唯一要提及的form 属性:

     FormFile file;   String fileName; String size;   恩如果你在提示里找不到FormFile 这个类请在类型里键入

     org.apache.struts.upload.FormFile 他关联的html标签相应的是html:file;接着你应该修改一下,formbean

     的reset函数,

  3,恩,还需要给函数加上验证,加入一下代码!同时在propertie 文件中加入error.file 消息

   ActionErrors errors = new ActionErrors();

    if (file == null )

       {

          errors.add("file", new ActionError("error.file")); 

       }

     return errors;

 4, 在我写action志前,我应当将poitest.java 进行修改以适应action的调用,首先在我们的项目文件里加入

    javabean包!现在声成一个javabean excelReadBean,显然它的目的,一眼就可以看出来。

    现在把poitest.java 的read(String table) 方法的代码拷贝过来,恩很显然,少了全局变量:xlsfile(参照原     代码);这里不得不注意这样的问题,为了满足,线程无关性,将read方法变为,read(String table,String xlsfile);

    现在似乎完成了,然而,我们不准备使用,手动连接,为了更方便以后更改,现在来吧数据源放在struts的配置文件里,(参照"使用 Easy Struts for Eclipse 开发 Struts ",现在来完成 DATABASE配置)

  5,既然,要通过Struts框架连接数据库,我想我们的代码也应改修改一下,不过我总是期望以后再次使用这个类,也许并不在Struts框架下,所以我决定使用夺态,在建一个read方法(懒家伙总是向我这样)。

   public boolean read(String table,String xslfile,Connection conn)

{

return false;

} // 为此又不得不引进,sql包,

   现在有理由相信,里面很简单,我们要做的仅仅是将同明方法的代码拷过来!然后删除,曾在上文提及的Rs方法,因为我们用不着他了,加入一下代码:

   Statement stmt = null;

   boolean   rs= false;

  然后将,rs.execute(sql.toString())替换为

  stmt=conn.createStatement();rs=stmt.execute(sql.toString()); 最后return rs;  好一切噢可!如果不想再改观这个类,我想这份工作已经完成了,可是我还是一个类200多行的代码,就是冗余阿,还是修改修改 恩,把读和写剥离开,就这枚简单!  现在来看看修改了后的javabean,  代码又和以前差不多了,呵呵,功能却没有减

/*

 * Created on 2004-11-21

 *

 * To change the template for this generated file go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

package com.phone.struts.javabean;

 

import java.io.*;

import java.util.*;

import javax.sql.*;

import java.sql.*;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.model.*;

import org.apache.poi.poifs.filesystem.*;

import com.cims.db.*;

/**

 * @author Administrator

 *

 * To change the template for this generated type comment go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

public class excelReadBean {

public excelReadBean()

{

}

/**

*

* @param table  this is the name of table in databse

* @param xslfile the excel file which you want to deel with

* @return

*/

public boolean read(String table,String xslfile)

{  

boolean   rs= false;

   rs=read(table,xslfile,null);

return rs;

}

/**

*

* @param table

* @param xslfile

* @param conn

* @return

*/

public boolean read(String table,String xslfile,Connection conn)

{

//@rs for the the result of executing sql

boolean   rs= false;

//construts a poifile

POIFSFileSystem fs=null;

HSSFWorkbook   wb=null; 

try

{

fs =new POIFSFileSystem(new FileInputStream(xslfile));

wb= new HSSFWorkbook(fs);

}catch(IOException e)

{

System.out.println("io exception :"+e);

}

HSSFSheet sheet = wb.getSheetAt(0);

//

System.out.println("the sheet name of  poifile="+wb.getSheetName(0));

int rownum=sheet.getPhysicalNumberOfRows();

System.out.println("the physical Number of rows="+rownum);

for(int rowflag=1;rowflag<rownum;rowflag++)

{

      //enter into row loop

      HSSFRow row = sheet.getRow(rowflag);

      int cells = row.getPhysicalNumberOfCells();

      System.out.println("the total number of cell in this row"+cells);

      System.out.println("this ROW= " + row.getRowNum());

            

      //

      String cellV[]=new String[cells];

      for(int cellflag=0;cellflag<cells;cellflag++)

  {

         //enter into cell loop

                 

         HSSFCell  cell= row.getCell((short)cellflag);

         double    valuenum = 0;

         String    valuestr=null;

         String    type=null;

// i got the code below from org.apache.poi.hssf.dev.hssf class

// and modify some

switch (cell.getCellType())

{

                             case HSSFCell.CELL_TYPE_FORMULA :

      type = "FORMULA ";

      valuestr="null";

  break;

case HSSFCell.CELL_TYPE_NUMERIC :

type = "NUMERIC";

valuenum=cell.getNumericCellValue();

break;

case HSSFCell.CELL_TYPE_STRING :

type = "STRING";

valuestr=cell.getStringCellValue();

break;

   default :

type=null;

}

if(type!=null)

{

System.out.print("the type of this cell is==="+type);

if(valuestr!=null)

{

cellV[cellflag]=valuestr.trim();

System.out.println("the value of this cell is==="+valuestr);

  } else  

  {

  cellV[cellflag]=Double.toString(valuenum);

  System.out.println("the value of this cell is==="+valuenum);

  }

} else

{

  cellV[cellflag]="null";

  System.out.println("erro in the file ,there is a null type cell :"+cellflag);

}

  }

  //here i will manipulate the 'insert' directive of database

  StringBuffer sql=new StringBuffer("insert into "+table+"  values (");

  for(int j=1;j<cells;j++)

  {

sql.append("'"+cellV[j-1]+"',");

  }

  sql.append("'"+cellV[cells-1]+"','"+rowflag+"')");

  System.out.println("sql  "+"="+sql.toString());

//call the write() function

      if(conn!=null)

  {

       rs=this.write(conn,sql.toString());

  }else

        rs=this.write(sql.toString());

     

}

return rs;

}

/**

*

* @param conn  the jdbc connection

* @param sql   the sentence of sql

* @return

*/

private boolean write(Connection conn,String sql)

{

Statement stmt = null;

boolean   rs= false;

try{ 

System.out.println("Start connet to db");

             stmt=conn.createStatement();

         rs=stmt.execute(sql.toString());

        }catch(Exception e)

        {

                 System.out.println("  sql exception "+e);

        }

return rs;

}

/**

*

* @param sql

* @return

*/

private boolean write(String sql)

    {

Rs  resultset=new Rs();

    boolean   rs= false;

    try



   System.out.println("Start connet to db");

           rs=resultset.execute(sql.toString());

}catch(Exception e)

    {

System.out.println("  sql exception "+e);

}

return rs;

}

}

One thought on “struts下开发”

Comments are closed.