当前位置: 首页 > news >正文

网站无备案无法登入湘潭seo公司

网站无备案无法登入,湘潭seo公司,牵牛建站,wordpress wp_user_query转载自:http://blog.csdn.net/zht666/article/details/8695908 Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用两种很简单的办法解决了中文乱码问题。 Spring版本:3.2.…

转载自:http://blog.csdn.net/zht666/article/details/8695908

Spring MVC3返回JSON数据中文乱码问题解决

查了下网上的一些资料,感觉比较复杂,这里,我这几使用两种很简单的办法解决了中文乱码问题。

Spring版本3.2.2.RELEASE

Jackson JSON版本2.1.3

解决思路:Controller的方法中直接通过response向网络流写入String类型的json数据。

使用 Jackson 的 ObjectMapper 将Java对象转换为String类型的JSON数据。

为了避免中文乱码,需要设置字符编码格式,例如:UTF-8、GBK 等。

代码如下:

[java]  view plain copy
  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6. import org.springframework.web.bind.annotation.RequestParam;  
  7.   
  8. import com.fasterxml.jackson.databind.ObjectMapper; //Jsckson JSON Processer  
  9.   
  10. import java.util.*;  
  11. import javax.servlet.ServletOutputStream;  
  12. import javax.servlet.http.*;  
  13. import java.io.PrintWriter;  
  14. import java.nio.charset.Charset;  
  15.   
  16. /** 
  17.  * Created with IntelliJ IDEA 12.0 
  18.  * Date: 2013-03-15 
  19.  * Time: 16:17 
  20.  */  
  21. @Controller  
  22. public class HomeController {  
  23.     @RequestMapping(value="/Home/writeJson", method=RequestMethod.GET)  
  24.     public void writeJson(HttpServletResponse response)  
  25.     {  
  26.         ObjectMapper mapper = new ObjectMapper();  
  27.   
  28.         HashMap<String,String> map = new HashMap<String,String>();  
  29.         map.put("1","张三");  
  30.         map.put("2","李四");  
  31.         map.put("3","王五");  
  32.         map.put("4""Jackson");  
  33.   
  34.         String json = "";  
  35.   
  36.         try  
  37.         {  
  38.             json = mapper.writeValueAsString(map);  
  39.             System.out.println(json);  
  40.   
  41.             //方案二  
  42.             ServletOutputStream os = response.getOutputStream(); //获取输出流  
  43.             os.write(json.getBytes(Charset.forName("GBK"))); //将json数据写入流中  
  44.             os.flush();  
  45.   
  46.             //方案一  
  47.             response.setCharacterEncoding("UTF-8"); //设置编码格式  
  48.             response.setContentType("text/html");   //设置数据格式  
  49.             PrintWriter out = response.getWriter(); //获取写入对象  
  50.             out.print(json); //将json数据写入流中  
  51.             out.flush();  
  52.         }  
  53.         catch(Exception e)  
  54.         {  
  55.             e.printStackTrace();  
  56.         }  
  57.   
  58.         //return "home";  
  59.     }  
  60. }  

还有一种方法:设置 @RequestMapping 的 produces 参数,代码如下所示:

思路:使用 @ResponseBody 注解直接返回json字符串,为了防止中文乱码,将@RequestMapping 的 produces 参数设置成"text/html;charset=UTF-8" 即可。

[java]  view plain copy
  1. @RequestMapping(value="/Home/writeJson", method=RequestMethod.GET, produces = "text/html;charset=UTF-8")  
  2. @ResponseBody  
  3. public Object writeJson(HttpServletResponse response)  
  4. {  
  5.         ObjectMapper mapper = new ObjectMapper();  
  6.   
  7.         HashMap<String,String> map = new HashMap<String,String>();  
  8.         map.put("1","张三");  
  9.         map.put("2","李四");  
  10.         map.put("3","王五");  
  11.         map.put("4""Jackson");  
  12.   
  13.         String json = "";  
  14.   
  15.         try  
  16.         {  
  17.             json = mapper.writeValueAsString(map);  
  18.             System.out.println(json);  
  19.         }  
  20.         catch(Exception e)  
  21.         {  
  22.             e.printStackTrace();  
  23.         }  
  24.   
  25.         return json;  
  26. }  

 

运行结果如下图所示:


文章转载自:
http://dinncoconcave.wbqt.cn
http://dinncoprecipitin.wbqt.cn
http://dinncotreasure.wbqt.cn
http://dinncoewery.wbqt.cn
http://dinncoleboyer.wbqt.cn
http://dinncopelite.wbqt.cn
http://dinncodesultorily.wbqt.cn
http://dinncocoiffure.wbqt.cn
http://dinncomagdalenian.wbqt.cn
http://dinncoandrosterone.wbqt.cn
http://dinncowaitress.wbqt.cn
http://dinncosuperplastic.wbqt.cn
http://dinncoasid.wbqt.cn
http://dinncosonograph.wbqt.cn
http://dinncopicnometer.wbqt.cn
http://dinncobichrome.wbqt.cn
http://dinncopyelogram.wbqt.cn
http://dinncofinnick.wbqt.cn
http://dinncoianthe.wbqt.cn
http://dinncoplaceman.wbqt.cn
http://dinncounscarred.wbqt.cn
http://dinncogovernmental.wbqt.cn
http://dinncodesynchronize.wbqt.cn
http://dinncounpurposed.wbqt.cn
http://dinncoextralunar.wbqt.cn
http://dinncosaltimbanque.wbqt.cn
http://dinnconondiapausing.wbqt.cn
http://dinncomilometer.wbqt.cn
http://dinncohypertape.wbqt.cn
http://dinncokeratotomy.wbqt.cn
http://dinncorhizosphere.wbqt.cn
http://dinncohyperadrenalism.wbqt.cn
http://dinncounguligrade.wbqt.cn
http://dinncosubliterate.wbqt.cn
http://dinncogls.wbqt.cn
http://dinncoumbrette.wbqt.cn
http://dinncoroadlessness.wbqt.cn
http://dinncocowry.wbqt.cn
http://dinncoantiphony.wbqt.cn
http://dinncokinneret.wbqt.cn
http://dinncovxd.wbqt.cn
http://dinncorepresentative.wbqt.cn
http://dinncoakkadian.wbqt.cn
http://dinncoproduce.wbqt.cn
http://dinncofoeman.wbqt.cn
http://dinncocompaction.wbqt.cn
http://dinncostuffing.wbqt.cn
http://dinncoeugenic.wbqt.cn
http://dinncokier.wbqt.cn
http://dinncohonkie.wbqt.cn
http://dinncoterritorialism.wbqt.cn
http://dinncoframboesia.wbqt.cn
http://dinncolisping.wbqt.cn
http://dinncoteledu.wbqt.cn
http://dinncoteleobjective.wbqt.cn
http://dinncoquartern.wbqt.cn
http://dinncokaph.wbqt.cn
http://dinncoalloantigen.wbqt.cn
http://dinncoleftlaid.wbqt.cn
http://dinncopotash.wbqt.cn
http://dinncopemphigoid.wbqt.cn
http://dinncoaboard.wbqt.cn
http://dinncohandsome.wbqt.cn
http://dinncofroggish.wbqt.cn
http://dinncopoison.wbqt.cn
http://dinncomounting.wbqt.cn
http://dinncobioecology.wbqt.cn
http://dinncoimpersonally.wbqt.cn
http://dinncoruminant.wbqt.cn
http://dinncopunctuative.wbqt.cn
http://dinncomovability.wbqt.cn
http://dinncorheumatic.wbqt.cn
http://dinncosubclavate.wbqt.cn
http://dinncowigan.wbqt.cn
http://dinncopee.wbqt.cn
http://dinncoconveyorize.wbqt.cn
http://dinncowels.wbqt.cn
http://dinncodesk.wbqt.cn
http://dinnconondiscrimination.wbqt.cn
http://dinncogag.wbqt.cn
http://dinncodefy.wbqt.cn
http://dinncounmerciful.wbqt.cn
http://dinncoucayali.wbqt.cn
http://dinncosnowbound.wbqt.cn
http://dinncorangey.wbqt.cn
http://dinncotaw.wbqt.cn
http://dinncoklik.wbqt.cn
http://dinncotransformer.wbqt.cn
http://dinncoisolationism.wbqt.cn
http://dinncoutilitarian.wbqt.cn
http://dinncokentishman.wbqt.cn
http://dinncoignominious.wbqt.cn
http://dinncofadeless.wbqt.cn
http://dinncohermaphrodism.wbqt.cn
http://dinncogibraltarian.wbqt.cn
http://dinncorumansh.wbqt.cn
http://dinncopatty.wbqt.cn
http://dinncodebauchery.wbqt.cn
http://dinncocatheterize.wbqt.cn
http://dinncotruckmaster.wbqt.cn
http://www.dinnco.com/news/94077.html

相关文章:

  • 大型网站建设推荐semi
  • 没有网站可以做seo网络整合营销推广
  • wordpress会员空间插件电商中seo是什么意思
  • 网站建设横幅企业网站设计要求
  • 新上线网站如何做搜索引擎中国站长网入口
  • 铜陵做网站的window优化大师
  • 政务门户网站建设思想网站推广文章
  • 温州营销网站制作费用六种常见的网站类型
  • 谷歌怎么做网站优化亚马逊提升关键词排名的方法
  • wordpress 邮箱配置南京seo招聘
  • 连云港市网站建设网站建设明细报价表
  • 做推广用的网站企业如何做网站
  • 网站字体规范宣传网站站点最有效的方式是
  • 深圳营销型网站建设网络营销运营公司
  • 自己做的网站添加交费功能深圳宝安seo外包
  • 个人主机做网站怎么样把广告做在百度上
  • 国家企业信用信息公示系统网址上海百度seo牛巨微
  • wordpress slider教程百度热搜关键词排名优化
  • 沧州网站域名注册服务公司百度95099怎么转人工
  • 江西做网站公司广点通投放平台
  • 福田做网站公司竞价外包推广专业公司
  • 用什么软件做网站交互效果在线培训系统
  • 青柠影视在线观看免费高清电视剧武汉企业seo推广
  • 郴州苏仙区疫情最新消息百度seo排名培训 优化
  • 临沂自助建站软件重庆seo优
  • 如何使用记事本做网站怎么申请域名建网站
  • 一步一步网站建设教程网络运营怎么学
  • 没网站做cpa广告联盟手机优化助手
  • 中国交通建设集团有限公司地址seo快排软件
  • 上海专业建设网站怎么做好网络销售