Spring Json View之快速开始(一)

 

快速开始-用Controller-Interface提交Get请求

 

Spring中不支持控制器接口的验证或绑定。它却能容易地处理来自Get方式的请求。

 

这个示例在Controller中仅仅返回一个Model-MapJson字符串,没有包含错误或者格式转换。

 

下面显示了创建一个简单的Json GET 控制器的所有代码。

 

Spring ApplicationContext

 

Xml代码 复制代码
  1. <beans>  
  2. <bean name="simpleJsonGetController"  
  3.    class="org.thing.spring.json.controller.SimpleJsonGetController"/>  
  4. <bean name="urlMapping"  
  5. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  

 

  1. <property name="mappings">  
  2. <props>  
  3. <prop key="/hello.json">simpleJsonGetController</prop>  
  4. </props>  
  5. </property>  
  6. </bean>  
  7. <bean name="viewResolver"  
  8. class="org.springframework.web.servlet.view.XmlViewResolver" />  
  9. </beans>  

  

 

 

 

Spring view.xml

 

 

Xml代码 复制代码
  1. <beans>  
  2. <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>  

     

    1. </beans>  

     

     

     

    form.html

     

     

    Html代码 复制代码
    1. <head>  
    2.   
    3. <title>  
    4. First Test Spring Json Demo   
    5. </title>  
    6.   
    7. <script type="text/javascript" src="script/prototype.js"></script>  

     

    1. <script type="text/javascript" src="script/behaviour.js"></script>  
    2. <script type="text/javascript" src="script/behaviour-roles.js"></script>  
    3.   
    4. <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>  
    5. </head>  
    6.   
    7. <body>  
    8.   
    9. <h1>Spring JSON DEMO</h1>  
    10.   
    11. <h2>Spring Ajax Get (ControlerInterface)</h2>  
    12.   
    13. <b>firstname : </b><span id="firstname"></span><br/>  
    14.   
    15. <b>secondname : </b><span id="secondname"></span><br/>  
    16.   
    17. </br>  
    18.   
    19. <button id="getName">get name</button>  
    20.   
    21. <button id="clearName">clear name</button><br/>  
    22.   
    23. </body>  
    <head>
    
    <title>
    First Test Spring Json Demo
    </title>
    
    <script type="text/javascript" src="script/prototype.js"></script>
    <script type="text/javascript" src="script/behaviour.js"></script>
    <script type="text/javascript" src="script/behaviour-roles.js"></script>
    
    <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
    </head>
    
    <body>
    
    <h1>Spring JSON DEMO</h1>
    
    <h2>Spring Ajax Get (ControlerInterface)</h2>
    
    <b>firstname : </b><span id="firstname"></span><br/>
    
    <b>secondname : </b><span id="secondname"></span><br/>
    
    </br>
    
    <button id="getName">get name</button>
    
    <button id="clearName">clear name</button><br/>
    
    </body>
    

     

     

     

     

    JavaScript behaviour-roles.js

     

     

    Js代码 复制代码
    1. var printResult = function(transport){   
    2.   
    3. var result =   
    4.   
    5. "Status : " + transport.status   
    6.   
    7. "/n"  

       

      1.   
      2. "/n"  
      3.   
      4. "Json-Result:"  
      5.   
      6. "/n" + transport.responseText;   
      7.   
      8. alert(result);   
      9.   
      10. };   
      11.   
      12. var myrules = {   
      13.   
      14. 'button#getName' : function(element){   
      15.   
      16. element.onclick = function(){   
      17.   
      18. new Ajax.Request('hello.json', { method:'get',   
      19.   
      20. onSuccess: function(transport, json){   
      21.   
      22. var json = transport.responseText.evalJSON();   
      23.   
      24. printResult(transport);   
      25.   
      26. $('firstname').innerHTML = json.firstname;   
      27.   
      28. $('secondname').innerHTML = json.secondname;   
      29.   
      30. }   
      31.   
      32. });   
      33.   
      34. }   
      35.   
      36. },   
      37.   
      38. 'button#clearName' : function(element){   
      39.   
      40. element.onclick = function(){   
      41.   
      42. $('firstname').innerHTML = '';   
      43.   
      44. $('secondname').innerHTML = '';   
      45.   
      46. }   
      47.   
      48. }   
      49.   
      50. };   
      51.   
      52. Behaviour.register(myrules);  

       

       

       

       

       Controller 源码

       

       

      Java代码 复制代码
      1. public class SimpleJsonGetController implements Controller {   
      2.   
      3. public ModelAndView handleRequest(HttpServletRequest request,   
      4. HttpServletResponse response) throws ServletException, IOException {   
      5. Map model = new HashMap();   

         

        1. model.put("firstname""Peter");   
        2. model.put("secondname""Schmitt");   
        3. return new ModelAndView("jsonView", model);   
        4. }   
        5. }  
        public class SimpleJsonGetController implements Controller {
        
        public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        Map model = new HashMap();
        model.put("firstname", "Peter");
        model.put("secondname", "Schmitt");
        return new ModelAndView("jsonView", model);
        }
        }
        
        

          

         

         

         

          效果:

           Status : 200

         Result:

                   {"firstname":"Peter","secondname":"Schmitt"}

         

         

         

         

         

         

         

         

         

         

         

         

        快速开始- Command-Controller提交Post请求

         

           Command-Controller提供一个完整的CommandBeanSpring对它提供校验和绑定支持。但是你必须在你的控制器类里添加校验和绑定结果。它处理简单的Post请求非常容易。这个示例在Command-Controller中仅仅返回一个Model-MapJson字符串,没有包含错误或者格式转换。

         

         

        Spring ApplicationContext

         

        Xml代码 复制代码
        1. <beans>  
        2.     <bean name="simpleJsonPostFormController"  
        3.           class="org.thing.spring.json.controller.SimpleJsonPostFormController">  
        4.             <property name="commandClass">            <value>org.thing.spring.json.controller.SpringJsonForm</value>  
        5.             </property>  
        6.     </bean>  
        7.     <bean name="urlMapping"  
        8. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
        9.         <property name="mappings">  
        10.             <props>  
        11.                 <prop key="/hello.json">simpleJsonPostCommandController</prop>  
        12.            </props>  
        13.         </property>  
        14.     </bean>  
        15.     <bean name="viewResolver"  
        16. class="org.springframework.web.servlet.view.XmlViewResolver" />  
        17. </beans>  

         

         Spring view.xml

         

        Xml代码 复制代码
        1. <beans>  
        2.     <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>  
        3. </beans>  

         

         

         

         form.html

         

        Html代码 复制代码
        1. <head>  
        2.         <title>  
        3.                 First Test Spring Json Demo   
        4.         </title>  
        5.         <script type="text/javascript" src="script/prototype.js"></script>  
        6.         <script type="text/javascript" src="script/behaviour.js"></script>  
        7.         <script type="text/javascript" src="script/behaviour-roles.js"></script>  
        8.         <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>  
        9. </head>  
        10. <body>  
        11.         <h1>Spring JSON DEMO</h1>  
        12.         <h2>Spring Ajax Post (SimpleFormControler and CommandController)</h2>  
        13.         <form method="post" id="form">  
        14.                 <input id="placeofbirth" type="text" name="placeofbirth" ><br>  
        15.                 <input id="birthday" type="text" name="birthday" ><br/>  
        16.                 <br/>  
        17.                 <b>place of birth : </b><span id="t_placeofbirth"></span><br/>  
        18.                 <b>birthday : </b><span id="t_birthday"></span><br/>          
        19.         </form>  
        20.         <br/>  
        21.         <button id="clearData">clear name</button>  
        22.         <button id="cc_postData">send data to CommandController</button>  
        23. </body>  
        <head>
                <title>
                        First Test Spring Json Demo
                </title>
                <script type="text/javascript" src="script/prototype.js"></script>
                <script type="text/javascript" src="script/behaviour.js"></script>
                <script type="text/javascript" src="script/behaviour-roles.js"></script>
                <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
        </head>
        <body>
                <h1>Spring JSON DEMO</h1>
                <h2>Spring Ajax Post (SimpleFormControler and CommandController)</h2>
                <form method="post" id="form">
                        <input id="placeofbirth" type="text" name="placeofbirth" ><br>
                        <input id="birthday" type="text" name="birthday" ><br/>
                        <br/>
                        <b>place of birth : </b><span id="t_placeofbirth"></span><br/>
                        <b>birthday : </b><span id="t_birthday"></span><br/>       
                </form>
                <br/>
                <button id="clearData">clear name</button>
                <button id="cc_postData">send data to CommandController</button>
        </body>
        

         

         

         

         

        JavaScript behaviour-roles.js

         

        Js代码 复制代码
        1. var printResult = function(transport){   
        2.                 var result =   
        3.                         "Status : " + transport.status   
        4.                         + "/n"  
        5.                         + "/n"  
        6.                         + "Json-Result:"  
        7.                         + "/n" + transport.responseText;   
        8.                 alert(result);   
        9. };   
        10.   
        11. var myrules = {   
        12.         'button#clearData' : function(element){   
        13.                 element.onclick = function(){   
        14.                         $('t_placeofbirth').innerHTML = '';   
        15.                         $('t_birthday').innerHTML = '';   
        16.                         $('error').innerHTML = '';   
        17.   
        18.         },   
        19.         'button#cc_postData' : function(element){   
        20.                element.onclick = function(){   
        21.                         new Ajax.Request('hello.json', {   
        22.                                 method:'post',   
        23.                                 parameters: $('form').serialize(false),   
        24.                                 onSuccess: function(transport){   
        25.                                         var json = transport.responseText.evalJSON();   
        26.                                         printResult(transport);   
        27.                                         $('t_placeofbirth').innerHTML = json.placeofbirth;   
        28.                                         $('t_birthday').innerHTML = json.birthday;   
        29.                                         $('error').innerHTML = '';   
        30.                         },   
        31.                         onFailure: function(transport){   
        32.                                 printResult(transport);   
        33.                                 addErrors(transport);   
        34.                         }   
        35.                         });   
        36.                 }   
        37.         }   
        38. };   
        39. Behaviour.register(myrules);  

         

         

        CommandBean

         

        Java代码 复制代码
        1. public class SpringJsonForm {   
        2.   
        3.         private String placeofbirth;   
        4.         private Date birthday;   
        5.         public String getPlaceofbirth() {   
        6.                 return placeofbirth;   
        7.         }   
        8.         public void setPlaceofbirth(String placeofbirth) {   
        9.                 this.placeofbirth = placeofbirth;   
        10.         }   
        11.         public Date getBirthday() {   
        12.                return birthday;   
        13.         }   
        14.         public void setBirthday(Date birthday) {   
        15.                 this.birthday = birthday;   
        16.         }   
        17.   
        18. }  
        public class SpringJsonForm {
        
                private String placeofbirth;
                private Date birthday;
                public String getPlaceofbirth() {
                        return placeofbirth;
                }
                public void setPlaceofbirth(String placeofbirth) {
                        this.placeofbirth = placeofbirth;
                }
                public Date getBirthday() {
                       return birthday;
                }
                public void setBirthday(Date birthday) {
                        this.birthday = birthday;
                }
        
        }
        
        

         

         

           控制器源码

          

        Java代码 复制代码
        1. public class SimpleJsonPostCommandController extends AbstractCommandController {   
        2.         @Override  
        3.         protected void initBinder(HttpServletRequest request,     
        4.                         ServletRequestDataBinder binder) throws Exception{   
        5.                 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");   
        6.                 CustomDateEditor editor = new CustomDateEditor(dateFormat, true);   
        7.                 binder.registerCustomEditor(Date.class, editor);   
        8.         }   
        9.            
        10.         @Override  
        11.         protected ModelAndView handle(HttpServletRequest request,   
        12.                         HttpServletResponse response, Object command,   
        13.                         BindException exception) throws Exception {   
        14.                    
        15.                 SpringJsonForm bean = (SpringJsonForm) command;   
        16.                    
        17.                 ModelAndView modelAndView = new ModelAndView("jsonView");   
        18.                    
        19.                 modelAndView.addObject("birthday",  bean.getBirthday());   
        20.                 modelAndView.addObject("placeofbirth",  bean.getPlaceofbirth());   
        21.   
        22.                 return modelAndView;   
        23.         }   
        24.   
        25. }  
        public class SimpleJsonPostCommandController extends AbstractCommandController {
                @Override
                protected void initBinder(HttpServletRequest request,  
                                ServletRequestDataBinder binder) throws Exception{
                        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
                        binder.registerCustomEditor(Date.class, editor);
                }
                
                @Override
                protected ModelAndView handle(HttpServletRequest request,
                                HttpServletResponse response, Object command,
                                BindException exception) throws Exception {
                        
                        SpringJsonForm bean = (SpringJsonForm) command;
                        
                        ModelAndView modelAndView = new ModelAndView("jsonView");
                        
                        modelAndView.addObject("birthday",  bean.getBirthday());
                        modelAndView.addObject("placeofbirth",  bean.getPlaceofbirth());
        
                        return modelAndView;
                }
        
        }

         

         

        结果 

         

        Status : 200

         

         Result:

         {"placeofbirth":"Sydney","birthday":"Wed Jan 30 00:00:00 GMT 2008"}

        http://www.javaeye.com/topic/290777

         

        • 0
          点赞
        • 0
          收藏
          觉得还不错? 一键收藏
        • 0
          评论

        “相关推荐”对你有帮助么?

        • 非常没帮助
        • 没帮助
        • 一般
        • 有帮助
        • 非常有帮助
        提交
        评论
        添加红包

        请填写红包祝福语或标题

        红包个数最小为10个

        红包金额最低5元

        当前余额3.43前往充值 >
        需支付:10.00
        成就一亿技术人!
        领取后你会自动成为博主和红包主的粉丝 规则
        hope_wisdom
        发出的红包
        实付
        使用余额支付
        点击重新获取
        扫码支付
        钱包余额 0

        抵扣说明:

        1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
        2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

        余额充值