乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      spring MVC之返回JSON數(shù)據(jù)(Spring3.0 MVC)

       老鼠愛上美貓 2012-08-09
      我們在本示例中,使用Spring3.0 MVC, Spring 3.0 MVC已經(jīng)支持JSON了。

      這里我們使用基于注解的方式,也是springMVC 3.0所支持的RESTFul風(fēng)格的。







      1.首先加入兩個jar依賴,這里以maven構(gòu)建為例:

      Xml代碼 
      <dependency> 
          <groupId>org.codehaus.jackson</groupId> 
          <artifactId>jackson-core-asl</artifactId> 
          <version>1.8.4</version> 
      </dependency> 
      <dependency> 
          <groupId>org.codehaus.jackson</groupId> 
          <artifactId>jackson-mapper-asl</artifactId> 
          <version>1.8.4</version> 
      </dependency> 

      <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-asl</artifactId>
      <version>1.8.4</version>
      </dependency>
      <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.8.4</version>
      </dependency>這是spring MVC處理json數(shù)據(jù)時,所必須的jar依賴。







      2.在spring mvc的配置文件中加入配置

      這里我們使用的是spring mvc的注解,如例:

      Xml代碼 
      <context:component-scan base-package="com.hundsun.twioo.business.action" /> 
      <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> 
      <context:annotation-config/> 

      <context:component-scan base-package="com.hundsun.twioo.business.action" />
      <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>
      <context:annotation-config/>注 意:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 的Bean配置中,我們加入了messageConverters屬性,在該屬性中我們有配置 mappingJacksonHttpMessageConverter這個Bean,它就是用來處理json數(shù)據(jù)轉(zhuǎn)換的。

      在mappingJacksonHttpMessageConverter的Bean配置中,我們有一個supportedMediaTypes屬性,在這個屬性

      中我們添加了text/html;charset=UTF-8這個值,它是為了處理返回的json數(shù)據(jù)的編碼,默認(rèn)是ISO-88859-1的,這里我們把它設(shè)置為UTF-8,如果有亂碼的情況,我們只需要修改這里就可以了。







      3.控制器Controller上的注解使用

      如例:

      Java代碼 
      @Controller 
      @RequestMapping("/logins")  
      public class LoginController {  
       
          @Autowired 
          private LoginManagerService loginManagerService;  
          @Autowired 
          private PermitManagerService permitManagerService;  
          private Logger log = Logger.getLogger(LoginController.class);  
            
            
          /** 
           * 用戶登錄系統(tǒng) 
           * @return 
           */ 
          @RequestMapping(value="/login")  
          public ModelAndView login(HttpServletRequest request,HttpSession session){  
                
              User user = null;  
              List<Module> moduleList = null;  
              try {  
                  //TwiooID  
                  Integer twiooId = ServletRequestUtils.getIntParameter(request, "twiooId");   
                  //群ID  
                  Integer groupId = ServletRequestUtils.getIntParameter(request, "groupId");   
                  user = loginManagerService.login(twiooId, 3);  
                  if(null != user){  
                      moduleList = permitManagerService.findPermit(user.getId());  
                  }  
                  session.setAttribute("user", user);  
                  session.setAttribute("permit", moduleList);  
                  session.setAttribute("twiooId", twiooId);  
                  session.setAttribute("groupId", groupId);  
              } catch (Exception e) {  
                  e.printStackTrace();  
                  log.error("LoginController.login() execute error!!!");  
              }  
              System.out.println("login...................");  
              return new ModelAndView("index");  
          }  
            
            
          /** 
           * 用戶退出系統(tǒng) 
           * @return 
           */ 
          @RequestMapping(value="/exit")  
          public ModelAndView exit(HttpServletRequest request,HttpSession session){  
                
              session.removeAttribute("user");  
              session.removeAttribute("permit");  
              session.removeAttribute("twiooId");  
              session.removeAttribute("twiooId");  
              System.out.println("exit....................");  
              return new ModelAndView("index");  
          }  
            
            
          /** 
           * 測試返回JSON數(shù)據(jù) 
           * @param session 
           * @return 
           */ 
          @RequestMapping(value="/test")  
          @ResponseBody 
          public Object test(HttpSession session){  
                
              System.out.println("test....................");  
              return session.getAttribute("permit");  
          }  
            
            
       
          public LoginManagerService getLoginManagerService() {  
              return loginManagerService;  
          }  
          public void setLoginManagerService(LoginManagerService loginManagerService) {  
              this.loginManagerService = loginManagerService;  
          }  
          public PermitManagerService getPermitManagerService() {  
              return permitManagerService;  
          }  
          public void setPermitManagerService(PermitManagerService permitManagerService) {  
              this.permitManagerService = permitManagerService;  
          }  
      }

      注意:test()方法上,我們使用了一個@ResponseBody的注解,Spring3.0 MVC @ResponseBody的作用是把返回值直接寫到HTTP response body里。 test()這個方法就是我們這里關(guān)注的地方,它就是返回json數(shù)據(jù)到客戶端去的。還要注意,這個test()方法返回的是Object(這里可以是任 意類型),而不ModelAndView。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多