学习过Boostrap模态窗口,一直想用Bootstrap的模态窗口实现数据的更新操作,最近刚好遇到这个问题,然后就研究了一下。
一、纯数据的传递
如果是纯数据的传递,只要从table表中获取行信息,然后在Bootstrap模态窗口中进行数据的回填即可。
关键代码:
GoodsQueryServlet.java
package com.cate.action.goods;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cate.DAO.FoodGoodsDAO;
import com.cate.DAO.DAOImpl.FoodGoodsDAOImpl;
//食品查询
public class GoodsQueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FoodGoodsDAO dao = new FoodGoodsDAOImpl();
List
DAO实现了类FoodGoodDAOImpl.java
package com.cate.DAO.DAOImpl;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.cate.DAO.FoodGoodsDAO;
import com.cate.bean.FoodDictionariesBean;
import com.cate.bean.FoodGoodsBean;
import com.cate.comm.DBUtil;
//食品管理的DAO实现类
public class FoodGoodsDAOImpl implements FoodGoodsDAO {
..
@Override
public boolean updateGood(FoodGoodsBean bean) throws Exception {
List
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
产品查询
编号
商品名称
商品价格
商品分类
图片
浏览次数
操作
${i.count }
${map.fname }
${map.price }
${map.classify }
${map.fcount }
package com.cate.action.goods;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.cate.bean.*;
import com.cate.DAO.DAOImpl.*;
public class GoodsUpdateServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
boolean flag = false;
try {
FoodGoodsBean foodgoods = new FoodGoodsBean();
foodgoods.setId(Integer.parseInt(request.getParameter("id")));
foodgoods.setGname(request.getParameter("name"));
foodgoods.setPrice(Integer.parseInt(request.getParameter("price")));
foodgoods.setClassify(request.getParameter("classify"));
FoodGoodsDAOImpl fd = new FoodGoodsDAOImpl();
//修改数据
flag = fd.updateGood(foodgoods);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("!!!!!!添加失败!!!!!!");
e.printStackTrace();
}
if(flag) {
System.out.println("成功");
} else {
System.out.println("失败");
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
}
提交更改后:
修改成功!
二、带图片的数据传递
可是,当设计图片上传时,上面这种方法就不行了。关键在于将multipart/form-data换成formdata,并且$.ajax()方法中的contentType设置为false。
核心代码: