当向前台传递信息有时候会因为中文的编码问题产生乱码,这样我们可以通过进行转码解决,同时可以在springmvc中进行配置方法来解决
@RequestMapping(value = "/register",produces="text/html;charset=UTF-8", method = {RequestMethod.POST})
@ResponseBody
public String reg(Model model, @RequestParam("name") String name,
@RequestParam("password") String password,
@RequestParam(value = "rember", defaultValue = "0") int rememberme,
HttpServletResponse response) {
try {
Map<String, Object> map = userService.register(name, password);
if (map.containsKey("ticket")) {
Cookie cookie = new Cookie("ticket", map.get("ticket").toString());
cookie.setPath("/");
if (rememberme > 0) {
cookie.setMaxAge(3600 * 24 * 5);
}
response.addCookie(cookie);
return HisUtil.getJSONString(0, "注册成功");
} else {
System.out.println(map);
return HisUtil.getJSONString(1, map);
}
} catch (Exception e) {
return HisUtil.getJSONString(1, "注册异常");
}
}
只需要在requestmaping中加入produces="text/html;charset=UTF-8"