用Struts2上传商品信息和图片

这里连接数据库就不用多说了,前面已经发过


商品添加页面
addShop.jsp
<form action="shop_addShop" method="post" enctype="multipart/form-data">
商品名称:<input type="text" name="picName"/></br>
商品价格:<input type="text" name="picPrice"/></br>
商品介绍:<input type="text" name="picIns"/></br>
<!-- accept="image/gif" -->
商品图片:<input type="file" name="picIm"/></br>
<input type="submit" value="添加"/>
</form>


实体类


这里的setter和getter没有写,用eclip自动生成


/**
 * 
 * @author zhang
 *属性必须和添加页面的name对应,可以多,但是一定不能少
 */
public class Pic {


private int picId;
private String picName;
private double picPrice;
private String picIns;
private String picImg;
//获取上传的文件
private File picIm;
//这里调用默认的Struts拦截器,inputname(picIm)+FileName
private String picImFileName;


Struts.xml


这里用的是Struts2.5.10
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- 这里用的是通配符配置,都是很简单的-->
  <action name="*_*" class="com.phone.action.{1}Action" method="{2}">
               <result name="success">/PicShop/{2}.jsp</result>
               <result name="error">/PicShop/{2}.jsp</result>
               <result name="suc" type="redirect">shop_ShowPic</result>
               <allowed-methods>addShop,ShowPic</allowed-methods>
          </action>
</package>
</struts>


Action业务逻辑处理
/*
 * 这里继承ActionSupport和实现ModelDriven<T>
 * 也可以和bean写在一起,那样就不用实现,但是本人觉得实现ModelDriven<T>接口比较好
 * 这样分离比较简便
 */
public class shopAction extends ActionSupport implements ModelDriven<Pic> {


/**

*/
private static final long serialVersionUID = 1L;
//这里调用服务层的逻辑
shopServiceImp ssi = new shopServiceImp();
//获取实体类对象,用模型驱动
Pic p = new Pic();
//实现方法
@Override
public Pic getModel() {
return p;
}
/*
 * 从数据库里获取添加信息并且展示在页面上
 */
public String ShowPic(){
List<Pic> list = ssi.showShop();
//获取request属性范围
ActionContext context = ActionContext.getContext();
context.put("goodsList", list);
return SUCCESS;
}
/*
* 添加商品方法
*/
public String addShop() {
//获取Struts默认拦截器的文件
File picIm = p.getPicIm();
//获取图片的名称
String picImFileName = p.getPicImFileName();
//获取图片的存储的真实路径
ServletContext servletContext = ServletActionContext.getServletContext();
String pathstr = servletContext.getRealPath("/PicShop/img") + File.separator + picImFileName;
File path = new File(pathstr);
//调用文件流读写方法
upLoad(picIm, path);
//获取上下文的绝对路径
String picImg = servletContext.getContextPath() + "/PicShop/img/" + picImFileName;
//将从页面获取的数据和图片插入数据库
boolean shop = ssi.addShop(p.getPicName(), p.getPicPrice(), p.getPicIns(), picImg);
if (shop) {
return "suc";
}
return ERROR;
}
/**
 * 
 * @param src从界面获取的文件
 * @param aim目标文件
 * 这里不做详细,能学到这里,应该都懂
 */
public void upLoad(File src, File aim) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(aim);
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null && fos != null) {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值