EWeb4J-1.9-文件上传下载和生成建表脚本

毕业了。高兴一下,哈。

一直以来,eweb4j framework新版本都没有发布,因为忙工作、忙毕业、忙答辩。

只好在有空的时候为框架添加新特性、重构旧代码来弥补了。

写本文之前,修复了一些小bug,增加了两个特性:文件上传下载、生成建表脚本。

[b]1.文件上传下载:[/b]

文件上传下载?这要涉及到文件IO流吧。或者,用第三方组件?例如Apache 的common-upload, 不不不,咱都不用,咱直接在控制器里声明一个 File 对象就行了。什么?这么简单?先看看代码吧~

public class UploadControl{
private File file;

public String doAtPost(){
FileUtil.copy(file, new File("c://"+file.getName()));
}

public void setFile(File file){
this.file = file;
}
}


upload.html
<form action="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>

[color=red]PS:注意表单文件input的name要和控制器声明的File对象名一致(实质上是和setter方法名相关)。另外注意 enctype[/color]

就这么简单,在一个Action方法执行前,框架会自动接收所有的文件上传请求,并将其作为一个临时文件保存到临时目录里,然后Action方法执行之后,框架会自动清空这些临时文件。因此,我们只要在Action方法体中,将临时文件拷贝到另外一个目录即可。下面有一个更加全面的例子:
//文件上传

public class UploadControl {

final static String path = ConfigConstant.ROOT_PATH + "/WEB-INF/uploads/";

private File tmpFile1;
private UploadFile tmpFile2;

private File[] tmpFile3;
private UploadFile[] tmpFile4;

public String doAtGet() {

return "fmt:Upload/upload.html";
}

public String doAtPost() {
//为了查看临时文件,当前线程睡眠10秒钟
//Thread.sleep(10*1000);

FileUtil.copy(tmpFile1, new File(path + tmpFile1.getName()));

FileUtil.copy(tmpFile2.getTmpFile(), new File(path + tmpFile2.getFileName()));

for (File f : tmpFile3)
FileUtil.copy(f, new File(path + f.getName()));

for (UploadFile f : tmpFile4)
FileUtil.copy(f.getTmpFile(), new File(path + f.getFileName()));

return StringUtil.getNowTime()+"上传成功!" ;
}

//别忘记setter方法哦
}


upload.html

<h1>EWeb4J Framework File Upload Demo</h1>
<form action="${BaseURL}upload" method="post" enctype="multipart/form-data">
<label>tmpFile1:</label><input type="file" name="tmpFile1" />
<label>tmpFile2:</label><input type="file" name="tmpFile2" />

<label>tmpFile3:</label><input type="file" name="tmpFile3" />
<label>tmpFile3:</label><input type="file" name="tmpFile3" />

<label>tmpFile4:</label><input type="file" name="tmpFile4" />
<label>tmpFile4:</label><input type="file" name="tmpFile4" />

<input type="submit" value="上传" />
</form>

文件上传这么简单,我想聪明的你应该猜到,文件下载大概怎么做了吧!没错,仅需要在Action方法里返回一个File对象即可!如果有多个文件,那么请返回文件数组吧!框架会自动将其打包成zip。

public class DownloadControl {

final static String path = ConfigConstant.ROOT_PATH + "/WEB-INF/uploads/";
final File file = new File(path+"just4download.jpg");

public File doAtGet(){
return file;
}

public File[] doArrayAtGet(){
return new File[]{file};
}

}

PS:框架采用标准的HTTP文件下载的方式进行响应,客户端可以自己修改文件下载保存名字。

response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=xxx");


[b]2.生成建表脚本:[/b]

首先,编写模型类


/**
* 宠物 模型
* @author weiwei
*
*/
@Entity
@Table(name="t_pet")
public class Pet extends Model{
public final static Pet inst = new Pet();

/* 宠物名字 */
@Column(unique=true)
private String name;
/* 宠物性别 1公 0母*/
private boolean gender;
/* 物种 */
private String species;
/* 宠物价格 */
private float price;
/* 宠物年龄 单位 月*/
private int age;
/* 是否已上架 */
@Column(name="is_up")
private boolean isUp;
/* 所属主人 */
@Column(nullable=false)
@ManyToOne
private Member master;
/* 所属分类 */
@ManyToOne
private Category category;

@Transient
public long count;

//setter and getter
}

/**
* 购物车 模型
*
* @author weiwei
*
*/
@Entity
@Table(name = "t_cart")
public class Cart extends Model {

@ManyToMany
@JoinTable(name = "t_cart_pet", joinColumns = @JoinColumn(name = "cart_id"), inverseJoinColumns = @JoinColumn(name = "pet_id"))
private List<Pet> pets = new ArrayList<Pet>();

@Column(nullable=false)
private String description;

//setter and getter
}

然后执行这样一句代码:

public static void main(String[] args){
String CONF_DIR = FileUtil.getTopClassPath(EWeb4JConfig.class)+"/conf/";
EWeb4JConfig.setCONFIG_BASE_PATH(CONF_DIR);
EWeb4JConfig.start("start.xml");
/* 框架根据模型类生成数据库pet_store_db建表脚本文件 */
Model2Table.write("pet_store_db");
}

执行之后会生成 ${ClassPath}/conf/mysql-create.sql文件,内容如下:

DROP DATABASE IF EXISTS pet_store_db;
CREATE DATABASE pet_store_db DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
SET FOREIGN_KEY_CHECKS=0;
USE pet_store_db;

-- ----------------------------
-- Created by EWeb4J Framework 1.9-SNAPSHOT
-- at 2012-06-09 11:59:53
-- Models of models.Pet
-- Records of t_pet
-- ----------------------------
DROP TABLE IF EXISTS t_pet;
CREATE TABLE t_pet(
id bigint (20) NOT NULL AUTO_INCREMENT ,
name varchar (255) NOT NULL ,
gender boolean NOT NULL ,
species varchar (255) NOT NULL ,
price float (8) NOT NULL ,
age tinyint (4) NOT NULL ,
is_up boolean NOT NULL ,
master_id bigint (20) ,
category_id bigint (20) NOT NULL ,
UNIQUE KEY name (name),
PRIMARY KEY (id),
KEY master_id (master_id),
CONSTRAINT master_id FOREIGN KEY (master_id) REFERENCES t_member (id),
KEY category_id (category_id),
CONSTRAINT category_id FOREIGN KEY (category_id) REFERENCES t_category (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Created by EWeb4J Framework 1.9-SNAPSHOT
-- at 2012-06-09 11:59:54
-- Models of models.Cart
-- Records of t_cart
-- ----------------------------
DROP TABLE IF EXISTS t_cart;
CREATE TABLE t_cart(
id bigint (20) NOT NULL AUTO_INCREMENT ,
description varchar (255) ,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Created by EWeb4J Framework 1.9-SNAPSHOT
-- at 2012-06-09 11:59:54
-- Records of t_cart_pet
-- ----------------------------
DROP TABLE IF EXISTS t_cart_pet;
CREATE TABLE t_cart_pet(
id bigint (20) NOT NULL AUTO_INCREMENT,
cart_id bigint (20) ,
pet_id bigint (20) ,
PRIMARY KEY (id),
KEY cart_id (cart_id),
CONSTRAINT t_cart_pet_cart_id FOREIGN KEY (cart_id) REFERENCES t_cart (id),
KEY pet_id (pet_id),
CONSTRAINT t_cart_pet_pet_id FOREIGN KEY (pet_id) REFERENCES t_pet (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


可以看到,多对多关系中的第三方表也会自动生成。

谢谢。

PS:如果希望获取EWeb4J最新版本,直接使用Maven添加依赖,版本号为:1.9-SNAPSHOT即可。另外还可以通过项目主页源码svn签出也行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值