码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC中文乱码问题解决

时间:2017-10-10 13:05:16      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:str   log4j   etc   color   code   app   data   ext   charset   

一、数据库连接

 

    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中文乱码问题解决

标签:str   log4j   etc   color   code   app   data   ext   charset   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!