spring组件,自己选择添加
jackson-all-1.7.6.jar
mvc配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mvc:resources mapping="/styles/**" location="/styles/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.villins.controller" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
后台代码:
@RequestMapping("/load")
public @ResponseBody List<Post> loadMore(@RequestParam("pageNo") int pageNo){
System.out.println(pageNo);
Page page = postService.pagedQuery("from Post", pageNo+1, 10);
List<Post> posts = page.getResult();
System.out.println(posts.size());
return posts;
}
注意:后代查询的对象如果是有关联比如一对多需要在要查询表对象加上如下格式代码:
@JsonIgnoreProperties(value={"tags","comments"})
@Entity
@Table(name = "tb_post")
@JsonIgnoreProperties(value={"tags","comments"})
public class Post {
否则json解析关联的的对象会引起死循环:
比如上面代码post与tags等关联
前台代码:
$(function(){
var i = 1;
$('#load').click(function(){
alert("nothing");
$.getJSON("<%=path%>/article/load",{pageNo:i},function(data){
alert(data);
if(data){
var str = "";
alert(data);
$.each(data,function(index,post){
str = "<div class=\"title\"><h2><a href=\"<%=path %>/article/"+post[post_id]+"\">"+post[title]+"</a></h2></div>"
$("#container").append(str);
});
i++;
}else{
$(".nodata").show().html("别滚动了,已经到底了。。。");
alert("nothing");
return false;
}
});
})
})