liuzidong 发表于 2013-1-29 12:02:50

Buffalo之 bind,reply,ajax,view,form应用(三)

预览文章: Buffalo之 bind,reply,ajax,view,form应用(二)
http://liuzidong.iteye.com/blog/629898

四  与spring集成
 1 spring版本为:spring-1.2.3.jar
 web.xml配置代码  
 
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
           /WEB-INF/classes/applicationContext.xml
       </param-value>
    </context-param>
 
<servlet>
    <servlet-name>bfapp</servlet-name>    
<servlet-class>
net.buffalo.web.servlet.ApplicationServlet</servlet-class>
    <load-on-startup>2</load-on-startup>     
</servlet>   
 
<servlet>
       <servlet-name>context</servlet-name>
       <servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
       <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
       <servlet-name>bfapp</servlet-name>
       <url-pattern>/bfapp/*</url-pattern>
</servlet-mapping>
 
 
 
applicationContext.xml的配置代码:
<bean name="userService" class="net.buffalo.service.UserService"></bean> 
<bean name="buffaloConfigBean" class="net.buffalo.service.BuffaloServiceConfigurer">
       <property name="services">
            <map>
                <entry key="springUserService">
                    <ref bean="userService"/>
                </entry>
                //配置更多的代理...
            </map>

 
   </property>
</bean>
 
注意事项:
1 这里的bfapp启动时间要大于spring时间,要保证spring优先于Buffalo加载.
否则就报如下错误,找不到springUserService这个服务.
 
net.buffalo.service.NoSuchServiceException: springUserService  at net.buffalo.service.DefaultServiceRepository.get(DefaultServiceRepository.java:87)  at net.buffalo.service.BuffaloWorker.processRequest(BuffaloWorker.java:60)   at net.buffalo.web.servlet.ApplicationServlet.doRequest(ApplicationServlet.java:119)   at net.buffalo.web.servlet.ApplicationServlet.doPost(ApplicationServlet.java:91)
 
2 当把org.springframework.web.context.ContextLoaderServlet
换成:
org.springframework.web.context.ContextLoaderListener后,控制台报如下错误
java.lang.ClassCastException: org.springframework.web.context.ContextLoaderListener
在spring-1.2.3,spring-2.5.5, spring-framework-3.0.0.M4版本下还是这个错误,不过使用: org.springframework.web.context.ContextLoaderServlet在这三个版本下测试正常.
 
3集成spring后, userService类中的调用方法不能在获取到容器的对象,如这样的代码:
RequestContext.getContext().getHttpSession();类似的调用都会报错,spring提供了这个服务类的所有代理.
 
 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in ServletContext resource : Instantiation of bean failed; nested exception is org.springframework.beans.FatalBeanException: Could not instantiate class ; constructor threw exception; nested exception is java.lang.NullPointerException: null
org.springframework.beans.FatalBeanException: Could not instantiate class ; constructor threw exception; nested exception is java.lang.NullPointerException: null
java.lang.NullPointerException  at net.buffalo.request.RequestContext.getHttpSession(RequestContext.java:105)...
 
五 Buffalo提供的常用JS函数
 
1 获取元素的Y坐标
function getElementY(element){
    var targetTop = 0;
    if (element.offsetParent) {
        while (element.offsetParent) {
           targetTop += element.offsetTop;
            element = element.offsetParent;
       }
    } else if (element.y) {
       targetTop += element.y;
    }
    return targetTop;
}
 
2 获取元素的X坐标
function getElementX(element){
    var targetLeft = 0;
    if (element.offsetParent) {
       while (element.offsetParent) {
           targetLeft += element.offsetLeft;
            element = element.offsetParent;
       }
    } else if (element.y) {
       targetLeft += element.x;
    }
    return targetLeft;
}
 
3 绑定元素值
<INPUT TYPE="checkbox" NAME="" id="testChk">
<INPUT TYPE="button" value="Bind Checkbox" onclick="bindChkbox()">
function bindChkbox() {
        //点击后只能是选中状态
       Buffalo.bind("testChk", "yes");
        //Buffalo.bind("testChk", "no");
 
}
 
4 遍历对象值
function openObj(obj) {
    var tmp = obj+"{";
    for (var x in obj) {
           tmp += x + "=" + obj+";";
    }
    tmp += "}";
    return tmp;
}
页: [1]
查看完整版本: Buffalo之 bind,reply,ajax,view,form应用(三)