SpringMvc+Ajax+json

转载 2016年06月01日 21:09:39

1、第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew。

如:<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/> 

或者<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

同样要用jackson的jar包。


2、第二种使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson。

利用HttpServletResponse,然后获取response.getOutputStream()或response.getWriter()

直接输出。

示例:

[java] view plain copy
 print?
  1. public class JsonUtil  
  2. {  
  3.       
  4.     private static Gson gson=new Gson();  
  5.   
  6.   
  7.     /** 
  8.      * @MethodName : toJson 
  9.      * @Description : 将对象转为JSON串,此方法能够满足大部分需求 
  10.      * @param src 
  11.      *            :将要被转化的对象 
  12.      * @return :转化后的JSON串 
  13.      */  
  14.     public static String toJson(Object src) {  
  15.         if (src == null) {  
  16.             return gson.toJson(JsonNull.INSTANCE);  
  17.         }  
  18.         return gson.toJson(src);  
  19.     }  
  20. }  


3、第三种利用spring mvc3的注解@ResponseBody

例如:

[java] view plain copy
 print?
  1. @ResponseBody  
  2.   @RequestMapping("/list")  
  3.   public List<String> list(ModelMap modelMap) {  
  4.     String hql = "select c from Clothing c ";  
  5.     Page<Clothing> page = new Page<Clothing>();  
  6.     page.setPageSize(6);  
  7.     page  = clothingServiceImpl.queryForPageByHql(page, hql);  
  8.       
  9.     return page.getResult();  
  10.   }  

然后使用spring mvc的默认配置就可以返回json了,不过需要jackson的jar包哦。

注意:当springMVC-servlet.xml中使用<mvc:annotation-driven />时,如果是3.1之前已经默认注入AnnotationMethodHandlerAdapter,3.1之后默认注入RequestMappingHandlerAdapter只需加上上面提及的jar包即可!

如果是手动注入RequestMappingHandlerAdapter 可以这样设置

配置如下:

<div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 936.5333251953125px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; "><div class="bar" style="padding-left: 45px; "><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108); "><strong>[html]</strong> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">copy</a></span><div style="position: absolute; left: 487px; top: 1821px; width: 29px; height: 14px; z-index: 99; "><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="29" height="14" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=29&height=14" wmode="transparent" /></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">print</a></span><a target=_blank href="http://blog.csdn.net/shan9liang/article/details/42181345#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-position: initial initial; background-repeat: initial initial; ">?</a></div></div><ol start="1" class="dp-xml" style="margin: 0px 0px 1px 45px !important; padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); "><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">        <span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">p:ignoreDefaultModelOnRedirect</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"true"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">            <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">property</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">name</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"messageConverters"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">                <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">list</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">                    <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">/></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">                <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">list</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">            <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">property</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; line-height: 18px; "><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">        <span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; "></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">bean</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(153, 51, 0); background-color: inherit; font-weight: bold; ">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">  </span></span></li></ol></div>


添加包
jackson-mapper-asl-*.jar
jackson-core-asl-*.jar

SpringMvc+ajax 实现json格式数据传递

转自[(http://www.cnblogs.com/andyfengzp/p/6184752.html)传JSON对象前端function test () { var param = {us...

SpringMVC利用Ajax,JQuery交互Json

  • 2017年08月22日 01:08
  • 10.03MB
  • 下载

springMVC+ajax+json

  • 2014年01月12日 18:28
  • 5.9MB
  • 下载

springMVC使用ajax返回json对象,用jquery进行解析.

springMVC使用ajax返回json对象,用jquery进行解析.
  • vcen_
  • vcen_
  • 2016年12月12日 13:40
  • 3479

ajax向springmvc传递json

  • 2014年12月12日 10:28
  • 1.53MB
  • 下载

SpringMVC——对Ajax的处理(包含 JSON 类型)

http://www.cnblogs.com/solverpeng/p/5821726.html 一、首先要搞明白的一些事情。 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 ...

springmvc spring hibernate ajax json

  • 2016年04月26日 13:18
  • 22.24MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SpringMvc+Ajax+json
举报原因:
原因补充:

(最多只允许输入30个字)