java easyui 分页_Spring mvc+easyui做列表展示及分页

Spring mvc有一个注解@ResponseBody可以自己将返回数据解析成json,不用在response.getWriter(),设置response的编码之类的。

1、首先在spring-mvc.xml中配置如下

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

application/json;charset=UTF-8

class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

class="com.liyi.test.common.UTF8StringHttpMessageConverter" />

别忘了,在下面还有一个UTF8StringHttpMessageConcerter类需要引入

package com.liyi.test.common;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.nio.charset.Charset;

import java.util.Arrays;

import java.util.List;

import org.springframework.http.HttpOutputMessage;

import org.springframework.http.MediaType;

import org.springframework.http.converter.StringHttpMessageConverter;

import org.springframework.util.FileCopyUtils;

public class UTF8StringHttpMessageConverter extends StringHttpMessageConverter {

private static final MediaType utf8 = new MediaType("text", "plain", Charset.forName("UTF-8"));

private boolean writeAcceptCharset = true;

@Override

protected MediaType getDefaultContentType(String dumy) {

return utf8;

}

protected List getAcceptedCharsets() {

return Arrays.asList(utf8.getCharSet());

}

protected void writeInternal(String s, HttpOutputMessage outputMessage)    throws IOException {

if (this.writeAcceptCharset) {

outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());

}

Charset charset = utf8.getCharSet();

FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));

}

public boolean isWriteAcceptCharset() {

return writeAcceptCharset;

}

public void setWriteAcceptCharset(boolean writeAcceptCharset) {

this.writeAcceptCharset = writeAcceptCharset;

}

}

2、配置好了,就可以写展示列表的后台代码了,以下,有两个方法,一个是做页面跳转用的,一个是用于ajax请求数据的。

@RequestMapping("/toUserList")

public String redirctUserList(){

return "user/new_user_list";

}

@ResponseBody

@RequestMapping("/userList")

public String userList(@RequestParam Map conds){

//默认每页10条

int pageSize = 10;

//默认第一页 计算开始条数

int currentPage = 1;

//获取页面传来每页显示条数

String row = (String) conds.get("rows");

//获取页面传来当前页码

String page = (String) conds.get("page");

if(null!=row&&!"".equals(row)){

pageSize=Integer.valueOf(row);

}

if(null!=page&&!"".equals(page)){

currentPage= Integer.valueOf(page);

}

Map map = new HashMap();

//计算一共有多少条

int count = userService.getTotalPage();

map.put("pageCount",pageSize);

//计算从第几条开始

map.put("currentPage",new PageUtil().getCurrent(currentPage,pageSize));

List list = userService.findAll(map);

Map map1 = new HashMap();

map1.put("total", count);

map1.put("rows",list);

String json = JSON.toJSONString(map1, true);

System.out.println(json);

return json;

}

只需要把你需要返回的数据,用fastjson将对象转成json串传入到页面,页面直接就可以取到。其中要注意,easyui展示列表的json如下:

[

{

"total": 13,

"rows": [

{

"createTime": 1438678875000,

"id": 1,

"mobile": "123456",

"name": "liyi",

"pwd": "123456"

},

{

"createTime": 1438679219000,

"id": 2,

"mobile": "123456",

"name": "scc",

"pwd": "123456"

},

{

"createTime": 1438679264000,

"id": 3,

"mobile": "123456",

"name": "diudiu",

"pwd": "123456"

},

{

"createTime": 1438679338000,

"id": 4,

"mobile": "123456",

"name": "xiaopaigu",

"pwd": "123456"

},

{

"createTime": 1438680558000,

"id": 5,

"mobile": "123456",

"name": "iphone",

"pwd": "123456"

},

{

"createTime": 1438682344000,

"id": 6,

"mobile": "123456",

"name": "iphone1",

"pwd": "123456"

},

{

"createTime": 1438754235000,

"id": 7,

"mobile": "123456",

"name": "abc",

"pwd": "123456"

},

{

"createTime": 1438852983000,

"id": 8,

"mobile": "11",

"name": "11",

"pwd": "11"

},

{

"createTime": 1438914359000,

"id": 9,

"mobile": "123456",

"name": "123456",

"pwd": "456789"

},

{

"createTime": 1439530418000,

"id": 10,

"mobile": "123",

"name": "123",

"pwd": "123"

}

]

}

]

3、jsp页面首先引入easyui的js 以及css

$(function(){

$('#dg').datagrid({

url:'${app}/userController/userList.do',

columns:[[

{field:'name',title:'姓名',width:100 },

{field:'mobile',title:'手机号',width:100},

{field:'_operate',width:80,align:'center',formatter:function(value,rec){

return "编辑";

},title:'操作'}

]],

toolbar: [{

iconCls: 'icon-add',

handler: function(){alert('编辑按钮')}

},'-',{

iconCls: 'icon-help',

handler: function(){alert('帮助按钮')}

}],

fitColumns:true,

striped:true,

pagination:true,

rownumbers:true,

pageNumber:1,

pageSize:10,

pageList:[10,20,30,40,50]

});

})

4、分页你可以用firefox观察一下,他会传入到后台两个参数,一个是当前页page,一个是rows每页的数量,根据我上篇文章的分页工具即可。在找到上面的List展示方法就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值