首页 > 编程语言 > 详细

SpringMVC中文乱码问题解决

时间:2017-10-10 12:12:14      阅读:268      评论:0      收藏:0      [点我收藏+]

一、数据库连接

 

    public static Connection getConnect() throws ClassNotFoundException, SQLException{
        Class.forName("net.sf.log4jdbc.DriverSpy");
        Connection conn = DriverManager.getConnection("jdbc:log4jdbc:mysql://127.0.0.1:3306/mydata?characterEncoding=utf8", "root", "950909");
        return conn;
    }

 

二、controller返回json数据

    @RequestMapping(value="/listHeroJson", produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String listHeroJson() throws ClassNotFoundException, SQLException, JsonGenerationException, JsonMappingException, IOException{
        PreparedStatement ps = DbUtil.getConnect().prepareStatement("select id, name, damage, hp, imgsrc from hero");
        ResultSet rs = ps.executeQuery();
        List<Hero> heroList = new ArrayList<Hero>();
        while(rs.next()){
            Hero hero = new Hero();
            hero.setId(rs.getInt(1));
            hero.setname(rs.getString(2));
            hero.setDamage(rs.getInt(3));
            hero.setHp(rs.getInt(4));
            hero.setImgsrc(rs.getString(5));
            heroList.add(hero);
            System.out.println("heroname:" + rs.getString(2));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", heroList);
        ObjectMapper om = new ObjectMapper();
        String jsonString = om.writeValueAsString(map);
        return jsonString;
    }

二、页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>

 

SpringMVC中文乱码问题解决

原文:http://www.cnblogs.com/Junsept/p/7644897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!