修改商品数据之后出现乱码

修改商品数据之后出现乱码

http://img1.sycdn.imooc.com//climg/5c680dcb0001a3f218510989.jpg

package com.imooc.shop.action;

import com.imooc.shop.bean.Article;
import com.imooc.shop.bean.ArticleType;
import com.imooc.shop.service.ShopService;
import com.imooc.shop.utils.Pager;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/list")
@MultipartConfig
public class ListServlet extends HttpServlet {

    private HttpServletRequest request;

    private HttpServletResponse response;

    private ShopService shopService;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = this.getServletContext();
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        shopService = (ShopService)context.getBean("shopService");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try{
            this.request=req;
            this.response=resp;
            request.setCharacterEncoding("UTF-8");
            String method = request.getParameter("method");
            switch (method){
                case "getAll":
                    getAll();
                    break;
                case "deleteById":
                    deleteById();
                    break;
                case "preArticle":
                    preArticle();
                    break;
                case "showUpdate":
                    showUpdate();
                    break;
                case "updateArticle":
                    updateArticle();
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void updateArticle(){
            //接收表单数据
            String code = request.getParameter("code");
            String title = request.getParameter("titleStr");
            String supplier = request.getParameter("supplier");
            String locality = request.getParameter("locality");
            String price = request.getParameter("price");
            String storage = request.getParameter("storage");
            String description = request.getParameter("description");
            String id = request.getParameter("id"); // 物品编号
            String picUrl = request.getParameter("picUrl"); // 物品旧封面
            //定义一个商品对象,封装界面提交的参数
            Article article = new Article();
            //接收用户可能上传的封面
            try {
                //如果用户上传了这里代码是不会出现异常了
                //如果没有上传这里出现异常
                Part part = request.getPart("image");
                //保存到项目的路径中去
                String sysPath = request.getSession().getServletContext().getRealPath("/resources/images/article");
                //定义一个新的图片名称
                String fileName = UUID.randomUUID().toString();
                //上传文件的内容性质
                String contentDispostion = part.getHeader("content-disposition");
                //获取上传文件的后缀名
                String suffix = contentDispostion.substring(contentDispostion.lastIndexOf("."),contentDispostion.length()-1);
                fileName += suffix;
                //把图片保存到路径中去
                part.write(sysPath+"/"+fileName);
                picUrl = fileName;
            }catch (Exception e){
                e.printStackTrace();
            }

            article.setId(Integer.valueOf(id));
            article.setImage(picUrl);

            ArticleType type = new ArticleType();
            type.setCode(code);
            article.setArticleType(type);
            article.setTitle(title);
            article.setSupplier(supplier);
            article.setLocality(locality);
            article.setPrice(Double.parseDouble(price));
            article.setStorage(Integer.parseInt(storage));
            article.setDescription(description);
            shopService.updateArticle(article);
            request.setAttribute("tip","修改商品成功");
            showUpdate();
    }

    private void showUpdate(){
        try {
            String id = request.getParameter("id");
            Article article = shopService.getArticleById(id);
            //查询出所有的商品类型
            List<ArticleType> types = shopService.getArticleTypes();
            request.setAttribute("types",types);
            request.setAttribute("article",article);
            request.getRequestDispatcher("/WEB-INF/jsp/updateArticle.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void preArticle(){
        try {
            String id = request.getParameter("id");
            Article article = shopService.getArticleById(id);
            request.setAttribute("article",article);
            request.getRequestDispatcher("/WEB-INF/jsp/preArticle.jsp").forward(request,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void deleteById() throws ServletException, IOException {
        try {
            String id = request.getParameter("id");
            shopService.deleteById(id);
            request.setAttribute("tip","删除成功");
        }catch (Exception e){
            request.setAttribute("tip","删除失败");
            e.printStackTrace();
        }
        request.getRequestDispatcher("/list?method=getAll").forward(request,response);
    }


    private void getAll() throws ServletException, IOException {
        //考虑分页查询
        Pager pager = new Pager();
        //看是否传入了分页参数的页码
        String pageIndex = request.getParameter("pageIndex");
        if(!StringUtils.isEmpty(pageIndex)){
            int pSize = Integer.valueOf(pageIndex);
            pager.setPageIndex(pSize);
        }
        //接收一级类型编号查询
        String typeCode = request.getParameter("typeCode");
        //接收二级类型编号查询
        String secondType = request.getParameter("secondType");
        request.setAttribute("secondType",secondType);
        //接收商品标题
        String title = request.getParameter("title");
        request.setAttribute("title",title);
        //根据一级类型查询对应的二级类型
        if(!StringUtils.isEmpty(typeCode)){
            List<ArticleType> secondTypes = shopService.loadSecondTypes(typeCode);
            request.setAttribute("secondTypes",secondTypes);
            request.setAttribute("typeCode",typeCode);
        }
        //查询所有一级类型数据
        List<ArticleType> firstArticleType = shopService.loadFirstArticleType();
        //查询所有商品信息
        List<Article> articles = shopService.searchArticles(typeCode,secondType,title,pager);

        request.setAttribute("firstArticleTypes",firstArticleType);
        request.setAttribute("articles",articles);
        request.setAttribute("pager",pager);

        request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request,response);
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

你好同学,请问你在创建数据库的时候有没有将字符编码格式设置为utf8呢?建议你使用workbench查询一下数据库中的数据是否为乱码呢?祝学习愉快~

  • 精慕门6573819 提问者 #1
    数据库直接使用的老师提供的源码,还是不行,数据库中的数据也是乱码
    2019-02-17 14:46:22
  • chrismorgen 回复 提问者 精慕门6573819 #2
    你好同学,建议你参考一下这个被采纳的问答http://class.imooc.com/course/qadetail/88858,修改一下自己数据库的编码格式,祝学习愉快~
    2019-02-18 11:12:46
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师