paulwong

          spring3MVC+JSON

           <!-- 自動(dòng)解析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>  
          <!-- 自動(dòng)解析action返回值 解析成json格式 -->

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

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





          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 對(duì)應(yīng)string則對(duì)應(yīng)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 閱讀(1354) 評(píng)論(0)  編輯  收藏 所屬分類: SPRING MVC

          主站蜘蛛池模板: 綦江县| 景东| 武穴市| 华池县| 巴楚县| 庆阳市| 连云港市| 甘孜| 阿荣旗| 金华市| 收藏| 玉田县| 连南| 顺义区| 内丘县| 涿鹿县| 谢通门县| 晴隆县| 云龙县| 邹城市| 新乡市| 清苑县| 冕宁县| 南召县| 乌兰浩特市| 拉孜县| 金坛市| 古浪县| 高青县| 永登县| 扎兰屯市| 陕西省| 湾仔区| 齐齐哈尔市| 井研县| 赞皇县| 乡宁县| 大兴区| 张北县| 邢台市| 班玛县|