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

          主站蜘蛛池模板: 育儿| 新田县| 新龙县| 井陉县| 山阳县| 双牌县| 白河县| 淮阳县| 扶风县| 米泉市| 夏河县| 科技| 定州市| 大洼县| 大连市| 稷山县| 五大连池市| 井研县| 改则县| 鹤庆县| 玉树县| 邻水| 岱山县| 班玛县| 招远市| 绥中县| 额敏县| 天长市| 莎车县| 米易县| 睢宁县| 清水县| 洛阳市| 凤城市| 乐平市| 绵阳市| 仲巴县| 大邑县| 美姑县| 新龙县| 汉中市|