Recently use AJAX request SPRINGMVC background query MySQL database, page display Chinese garbled
Initially configured as follows in MyBatis
?
| 123 |
<selectid="queryContentById" resultType = "java.lang.String" parameterType="String"> select text from News where id=#{o} </select> |
Where table News's text field is a BLOB type
The text value so isolated is garbled in the console.
After Google found relevant Resulttype=blob related content without results, and then changed it to Resulttype = "Java.util.Map", and
?
| 12 |
byte[] b = (byte[]) map.get("text");String s = newString(b,"utf-8"); |
Print out S, at this time the Chinese normal display, but the page display is still garbled.
Therefore, the AJAX request, then check the response header information, identified as follows
content-typetext/html;charset=iso-8859-1
Because the database is encoded as UTF-8, the response header information is modified
| 1234567 |
@RequestMapping(value = "/queryContentById", method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")public @ResponseBody String queryContentById(@RequestParam("id") String id) throws SQLException, UnsupportedEncodingException { Map map = (Map) ndrService.queryContentById(id); byte[] b = (byte[]) map.get("text"); String s = new String(b,"utf-8"); return s;} |
Please refer to the following information:
Http://www.itnose.net/detail/6074493.html
Springmvc mybatis MySQL blob Chinese garbled problem processing