Vikings

          調用jsonrpc的技巧

          一段不用每個jsp頁面都JSONRPCBridge.registerObject的方法

          1. JsonContextLinster
          在spring文件里讀取需要注冊bridge的bean名單
          registerObject
          public class JSONContextListener implements ServletContextAttributeListener {
              
          private final Log log = LogFactory.getLog(JSONContextListener.class);

              
          public void attributeAdded(ServletContextAttributeEvent event) {
                  
          if (event.getName().equals(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)) {
                      
          // register LookupHelper so we can call methods on it
                      ApplicationContext ctx = (ApplicationContext) event.getValue();

                      
          // check for null so we don't have to initialize Spring in tests
                      if (ctx != null{
                          log.debug(
          "Registering jsonObjects for XmlHttpRequest to GlobalBridge");
                          
                          JSONRPCBridge jsonBridge 
          = JSONRPCBridge.getGlobalBridge();

                          Map jsonObjects 
          = (Map) ctx.getBean("jsonObjects");
                          
          for (Object name : jsonObjects.keySet()) {
                              jsonBridge.registerObject(name, jsonObjects.get(name));
                          }

                          
                      }

                  }
                  
              }

          public void attributeReplaced(ServletContextAttributeEvent event) {
                  attributeAdded(event);
              }

          }


          2.spring配置里的jsonObject
              <!-- json registration -->
              
          <bean name="jsonObjects" class="java.util.HashMap">
                  
          <constructor-arg>
                      
          <map>
                          
          <entry key="information" value-ref="informationJson"></entry>
                      
          </map>
                  
          </constructor-arg>
              
          </bean> 

              
          <bean id="informationJson" class="com.vikings.tools.json.Information" >
                  
          <property name="informationDAO" ref="informationDAO" />
              
          </bean>

          3.web.xml里增加配置剛才寫的listener
          <listener>
                  
          <listener-class>com.vikings.tools.json.JSONContextListener</listener-class>
              
          </listener>

          到這里就不用這么寫了
          <jsp:useBean id="JSONRPCBridge" scope="session"
             class
          ="com.metaparadigm.jsonrpc.JSONRPCBridge" />

          <% JSONRPCBridge.registerObject("myTestObject", aTestObject); %>
          這個也不需要
          // Find the JSONRPCBridge for this session or create one
          // if it doesn't exist. Note the bridge must be named "JSONRPCBridge"
          // in the HttpSession for the JSONRPCServlet to find it.
          HttpSession session = request.getSession();
          JSONRPCBridge json_bridge 
          = null;
          json_bridge 
          = (JSONRPCBridge) session.getAttribute("JSONRPCBridge");
          if(json_bridge == null{
              json_bridge 
          = new JSONRPCBridge();
              session.setAttribute(
          "JSONRPCBridge", json_bridge);
          }

          posted on 2007-11-01 22:19 Vikings 閱讀(1478) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 镇赉县| 华安县| 灵宝市| 长宁县| 绍兴市| 周口市| 宜丰县| 五寨县| 石柱| 贵州省| 通海县| 无锡市| 台中县| 资阳市| 佳木斯市| 靖边县| 古田县| 宁南县| 西青区| 扶沟县| 清镇市| 西城区| 湖口县| 新蔡县| 新营市| 包头市| 九江市| 宁都县| 双桥区| 天全县| 横峰县| 长寿区| 东安县| 天门市| 漯河市| 东港市| 昌吉市| 济源市| 涟水县| 兰州市| 马山县|