Spring MVC ѧϰ±Ê¼Ç11 ¡ª¡ª ºó¶Ë·µ»Øjson¸ñʽÊý¾Ý
Spring MVC ѧϰ±Ê¼Ç11 ¡ª¡ª ºó¶Ë·µ»Øjson格ʽÊý¾Ý ÎÒÃdz£³£Ìý˵jsonÊý¾Ý£¬Ê×ÏÈ£¬Ê²Ã´ÊÇjsonÊý¾Ý£¬×ܽáÆðÀ´£¬ÓÐÒÔϼ¸µã£º
1. JSONµÄÈ«³ÆÊÇ¡±JavaScript Object Notation¡±£¬Òâ˼ÊÇJavaScript¶ÔÏó±íʾ·¨.
2. ËüÊÇÒ»ÖÖ»ùÓÚÎı¾£¬¶ÀÁ¢ÓÚÓïÑÔµÄÇáÁ¿¼¶Êý¾Ý½»»»格ʽ.
3. jsonµÄÁ½Öֽṹ£º¶ÔÏó{key:value,key:value,...} ºÍ Êý×é [value, value2, ... ]
4. json×Ö·û´®£ºÆÕͨ×Ö·û´®¡¢json×Ö·û´®¡¢json¶ÔÏóµÄÇø±ð
5. ²»Í¬±à³Ì¹¤¾ßʹÓÃjsonµÄ·½·¨
²Î¿¼£º
UserController.java:
±£³ÖÁ½Ì×ÇëÇó£¬Ò»Ì×ÊÇ´«Í³µÄÇëÇó·µ»ØÊý¾Ý£»Ò»Ì×ÊÇÇëÇójson格ʽ·µ»ØÊý¾Ý¡£
//6. ²éÒ»¸öÓû§ show.jsp @RequestMapping(value="/{username}", method=RequestMethod.GET) public String show(@PathVariable String username, Model model){ model.addAttribute("user1",users.get(username)); //user1²ÎÊýÊôÐÔÃû£¬µ½ÁËÊÓͼ£¬¾ÍÊÇuser1 return "user/show"; } //6. ²éÒ»¸öÓû§ show.jsp @RequestMapping(value="/{username}", method=RequestMethod.GET, params="jsoon") @ResponseBody //ÕâÀïÒª¼ÓÒ»ÐÐ public User show(@PathVariable String username){ //ÉùÃ÷ÖÐûÓÐÁËModel //model.addAttribute("user1",users.get(username)); return users.get(username); //²»·µ»ØStringÁË£¬·µ»Øuser¶ÔÏó }˵Ã÷£¬ÒÔÉÏ´úÂëÖУº
1. [email protected],
2. no return String, but User Object
3. no Model
4. directly return users.get(username)//×¢Òâ·µ»ØµÄÊÇÒ»¸öUser¶ÔÏó
5. ¿ÉÒÔÔÚRequestMappingÐÐÔö¼ÓÒ»¸öÇëÇó值£ºParams="jsoon"//ÕâÊǹ涨ÈçÒª½øÕâ¸ö·½·¨·µ»Øjson£¬ÐèÒª´ø¸ö²ÎÊýjsoon
´Ëʱ¿É²âÊÔ£¬·µ»Ø406´íÎóÈçͼ£º£¨Í¨¹ýurl¼ÓÈë?jsoon·ÃÎÊÁË£¬µ«Ã»ÓÐÍ·ÎļþΪËü½øÐнâÊÍ£©
ËùÒÔÎÒÃÇÐèÒª¼ÓÈëÒ»¸öjar°ü£ºjackson-all-1.x.x.jar
´ÓÕâ¸öÍøÖ·¿ÉÒÔÏÂÔص½£º
°Ñjar°üµ½ÏîÄ¿Îļþ¼Ð/libÎļþ¼ÐÏ£¬ÖØÐÂdebug£¬Á´½Ó½øÈ룺:8080/myhello/user/sdy?jsoon
´Ëʱ£¬ÓеÄä¯ÀÀÆ÷Chrome£¬firefoxµÈÄÜÏÔʾÈçÏ£¬ÆäËûä¯ÀÀÆ÷Èç360µÈ£¬»áÌáʾÏÂÔØsdy.jsonÎļþ£¬notepad++´ò¿ªÒÔºóÒ²ÊÇÈçÏÂÄÚÈÝ¡£
Enjoy ;)
¸ü¶àÏà¹ØÎÄÕÂ
ASP.NET MVCÉÏ´«Îļþ³É¹¦·µ»ØJson¸ñʽÊý¾Ýµ¯³öÏÂÔØ¿ò½â¾ö·½°¸
×î½ü×öÒ»¸öÏîÄ¿,ÉÏ´«ÎļþºóÎÒ·µ»ØÒ»¸öJson¸ñʽµÄÊý¾Ý,ÌáʾÉÏ´«³É¹¦,¿ÉÊÇÀϵ¯³öÏÂÔØ¿òÈçÏ ºÜÓôÃÆ,ÍøÉÏÕÒÁ˺ܶà×ÊÁÏ,¶¼²»ÀíÏë,ÄÉÃÆÐí¾Ã,ÖÕÓÚÓÐÁ˽â¾ö·½°¸.ºǫ́ÉÏ´«³É¹¦ºóÊÇÕâôдµÄ:return Json("{msg:'ÉÏ´«³É¹¦'}");ǰ̨ÊÇÕâô½ÓµÄvar json = eva ...
Springѧϰ±Ê¼ÇMVC3.1SpringMVC·µ»ØJsonÊý¾Ý·½Ê½1À©Õ¹
<Springѧϰ±Ê¼Ç-MVC>ϵÁÐÎÄÕÂ,½²½â·µ»ØjsonÊý¾ÝµÄÎÄÕ¹²ÓÐ3ƪ,·Ö±ðΪ:[Springѧϰ±Ê¼Ç-MVC-3]SpringMVC·µ»ØJsonÊý¾Ý-·½Ê½1:[Springѧϰ±Ê¼Ç-M ...
Spring MVC ѧϰ±Ê¼Ç json¸ñʽµÄÊäÈëºÍÊä³ö
±¾ÎÄת×Ô²©¿ÍÔ°,×÷Õß:Öֲ˵ùÏ,ʱ¼ä:2015-4-2 Spring mvc´¦ÀíjsonÐèҪʹÓÃjacksonµÄÀà¿â,Òò´ËΪ֧³Öjson¸ñʽµÄÊäÈëÊä³öÐèÒªÏÈÐÞ¸Äpom.xmlÔö¼Ójackson°üµÄÒýÓà <!-- json --> <dependency> <groupI ...
Spring MVC ѧϰ±Ê¼Ç6 —— ʵÏÖ¼òµ¥µÄÓû§¹ÜÀí2Ìí¼ÓºÍÑéÖ¤
Spring MVC ѧϰ±Ê¼Ç6 -- ʵÏÖ¼òµ¥µÄÓû§¹ÜÀí(2)¶Ôuser modelµÄÔöɾ¸Ä²é Ôö¼ÓÓû§: 1. ¶¥¼¶Ò³ÃæÇëÇóдÔÚclassÍ·Àï ÔÚ"UserController.java"ÎļþÖÐ:¶¥¼¶Ò³ÃæÇëÇóÊÇ/user, ¹Ê RequestMapping("/us ...
Spring MVC ѧϰ±Ê¼Ç12 —— SpringMVC+Hibernate¿ª·¢1ÒÀÀµ°ü´î½¨
¡¡