2008年7月8日 星期二

Weblogic 10 ANTLR issue

Due to the issue of ANTLR that weblogic 10 uses internally, we need to add a file WEB-INF/weblogic.xml with the following contents to solve the conflict:


<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app
xmlns="http://www.bea.com/ns/weblogic/90"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/90
                    http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">

   <container-descriptor>
      <prefer-web-inf-classes>true</prefer-web-inf-classes>
   </container-descriptor>
</weblogic-web-app>


This file make Weblogic use classes and libraries in the web application before other libraries in the classpath.

2008年7月7日 星期一

Struts Download File Example

public ActionForward doViewAttachFile(ActionMapping aMapping, ActionForm aForm,
HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception {
Efp136Form efp136Form = (Efp136Form) aForm;
Efp136m efp136m = (Efp136m) efp136Form.getEntity();

String fileName = URLEncoder.encode(efp136m.getAttachFileName(), "UTF-8");
aResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
aResponse.setContentType(efp136m.getAttachFileType());
aResponse.setContentLength(efp136m.getAttachFileSize().intValue());

ServletOutputStream servletOutputStream = aResponse.getOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(efp136m.getAttachFileData());
BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);
byte[] bytes = new byte[2048];
while (bufferedInputStream.read(bytes) != -1) {
bufferedOutputStream.write(bytes);
}

servletOutputStream.flush();
byteArrayInputStream.close();
servletOutputStream.close();
return aMapping.findForward(VIEW_ATTACH_FILE_SUCCESS);
}

2008年7月6日 星期日

根據hibernate configuration file建立table schema

程式範例如下:

package mytest;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class SchemaExportTester {

public static void main(String[] args) {
new SchemaExportTester().createSchema();
}

private void createSchema() {
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.setOutputFile("schema.sql");
schemaExport.create(true, true);
System.out.print("ok");
}
}


其中
schemaExport.create(true, true);
第一個參數是表示是否要產生schema.sql,第二個參數是表示是否要在database立建立table。