paulwong

          spring3MVC+JSON

           <!-- 自動解析action返回值 解析成json格式 -->
          <context:component-scan base-package="com.bplow.*.web" />  
          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
              
          <property name="messageConverters">  
                  
          <util:list id="beanList">  
                      
          <ref bean="mappingJacksonHttpMessageConverter"/>  
                  
          </util:list>  
              
          </property>  
          </bean>  
          <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
              
          <property name="supportedMediaTypes">  
                  
          <list>  
                      
          <value>text/html;charset=UTF-8</value>  
                  
          </list>  
              
          </property>  
          </bean>  
          <!-- 自動解析action返回值 解析成json格式 -->

          然后需要額外的jar包 jackson 1.1,這里讓我很無語,我首先下的1.9放上去什么方法找不到,換1.8x 方法找不到 我一直試到1.6 后來怒了用最老的 好了。。。坑,這絕對是坑啊

          然后你的action中直接返回對象,集合,map吧 非常爽啊。測試如下





          package com.bplow.test.web;

          import java.io.UnsupportedEncodingException;
          import java.util.HashMap;
          import java.util.LinkedList;
          import java.util.List;
          import java.util.Map;

          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.bind.annotation.RequestMethod;
          import org.springframework.web.bind.annotation.ResponseBody;

          @Controller
          public class TestAction {

          // ResponseBody 對應string則對應ajax getText
          @RequestMapping(value = "/testAction", method = RequestMethod.GET)
          @ResponseBody
          public String allOnActivities() throws UnsupportedEncodingException {

          return "{'title':'HelloWorldGOGOGO'}";
          }


          @RequestMapping(value
          = "/testAction2", method = RequestMethod.GET)
          @ResponseBody
          public TestVo getJsonVo() {

          TestVo vo
          = new TestVo();
          vo.setTitle(
          "吃吧");
          return vo;
          }


          @RequestMapping(value
          = "/testAction3", method = RequestMethod.GET)
          @ResponseBody
          public List getJsonList() {

          TestVo vo
          = new TestVo();
          vo.setTitle(
          "吃吧");
          List l
          = new LinkedList();
          l.add(vo);
          l.add(vo);
          l.add(vo);
          l.add(vo);
          l.add(vo);
          return l;
          }


          @RequestMapping(value
          = "/testAction4", method = RequestMethod.GET)
          @ResponseBody
          public Map getMap() {

          TestVo vo
          = new TestVo();
          vo.setTitle(
          "吃吧");
          HashMap hsm
          = new HashMap();
          hsm.put(
          "a", 123);
          hsm.put(
          "b", "123");
          hsm.put(
          "c", vo);
          return hsm;
          }

          }

          posted on 2012-05-18 00:14 paulwong 閱讀(1357) 評論(0)  編輯  收藏 所屬分類: SPRING MVC

          主站蜘蛛池模板: 仙居县| 临澧县| 武城县| 翁源县| 咸宁市| 定远县| 前郭尔| 苍山县| 德令哈市| 临沂市| 砚山县| 甘谷县| 集贤县| 伊吾县| 永靖县| 新乐市| 临城县| 浙江省| 南丰县| 大英县| 射洪县| 云安县| 扬州市| 南乐县| 天祝| 鄂伦春自治旗| 定西市| 凤山县| 吉林市| 莫力| 吴桥县| 陇川县| 柳河县| 刚察县| 湘乡市| 翁牛特旗| 盐源县| 福泉市| 承德县| 法库县| 依兰县|