1,实现
rg.displaytag.pagination.PaginatedList接口
1 package org.cnup.xup.utils;
2
3 import java.util.List;
4
5 import org.displaytag.pagination.PaginatedList;
6 import org.displaytag.properties.SortOrderEnum;
7
8 public class PaginatedListImpl implements PaginatedList {
9
10 /**
11 * 当前显示的的局部数据
12 */
13 private List list;
14 /**
15 * 全部数据总数
16 */
17 private int fullListSize;
18
19 /**
20 * 每页显示数据数目
21 */
22 private int objectsPerPage;
23
24 /**
25 * 当前页数(从 1开始)
26 */
27 private int pageNumber;
28
29 /**
30 *
31 */
32 private String searchId;
33
34 /**
35 * 外部排序条件
36 */
37 private String sortCriterion;
38
39 /**
40 * 外部排序方向
41 */
42 private SortOrderEnum sortDirection;
43
44
45 setters ...
46 getters ...
a47 }
48
2,struts2 [displaytag的属性文件略]
1 /*
2 * Copyright ©2006-11-6 www.cnup.org
3 */
4 package org.cnup.xup.action;
5
6 import org.cnup.xup.utils.PaginatedListImpl;
7 import org.displaytag.properties.SortOrderEnum;
8
9 import com.opensymphony.xwork2.ActionSupport;
10 import com.opensymphony.xwork2.Preparable;
11
12 /**
13 * 通用CRUDAction的abs类
14 *
15 * @author <a href="mailto:lee_eric@163.com">李亦然</a>
16 */
17 public abstract class AbstractCRUDAction extends ActionSupport implements
18 Preparable {
19 protected int page = 1;
20 protected int pageSize = 15;
21 protected String sort = null;
22 protected String dir = "asc";
23
24 protected String selectId;
25 protected String[] selectedIds;
26
27 public void prepare() throws Exception {
28
29 }
30
31 public abstract String save() throws Exception;
32
33 public abstract String delete() throws Exception;
34
35 public abstract String get() throws Exception;
36
37 public abstract String list() throws Exception;
38
39 public String getSelectId() {
40 return selectId;
41 }
42
43 public void setSelectId(String selectId) {
44 this.selectId = selectId;
45 }
46
47 public int getPage() {
48 return page;
49 }
50
51 public void setPage(int page) {
52 this.page = page;
53 }
54
55 public int getPageSize() {
56 return pageSize;
57 }
58
59 public void setPageSize(int pageSize) {
60 this.pageSize = pageSize;
61 }
62
63 public String getSort() {
64 return sort;
65 }
66
67 public void setSort(String sort) {
68 this.sort = sort;
69 }
70
71 public String getDir() {
72 return dir;
73 }
74
75 public void setDir(String dir) {
76 this.dir = dir;
77 }
78
79 public PaginatedListImpl propPaginatedList(PaginatedListImpl p) {
80 p =new PaginatedListImpl();
81 p.setSortCriterion(sort);
82
83 if ("asc".equals(dir)) {
84 p.setSortDirection(SortOrderEnum.ASCENDING);
85 } else if ("desc".equals(dir)) {
86 p.setSortDirection(SortOrderEnum.DESCENDING);
87 }
88 p.setObjectsPerPage(pageSize);
89 p.setPageNumber(page);
90 return p;
91 }
92
93 }
94
3,PersonAction
1 /*
2 * gen from template[struts2.action.crudaction]
3 */
4 package org.cnup.xup.action;
5
6 import org.cnup.xup.model.Person;
7 import org.cnup.xup.service.PersonService;
8 import org.cnup.xup.utils.PaginatedListImpl;
9 import com.opensymphony.xwork2.Action;
10
11 @SuppressWarnings("serial")
12 public class PersonAction extends AbstractCRUDAction {
13
14 private PersonService personService;
15 private PaginatedListImpl persons;
16 private Person person;
17
18
19 public PersonAction() {
20
21 }
22
23 public PersonService getPersonService() {
24 return personService;
25 }
26
27 public void setPersonService(PersonService personService) {
28 this.personService = personService;
29 }
30 @Override
31 public String list() {
32 persons=this.propPaginatedList(persons);
33 persons.setFullListSize(personService.count());
34
35 persons.setList(personService.findPartial((page-1)*pageSize,pageSize,sort,dir));
36
37 return Action.SUCCESS;
38 }
39
40 public String save() {
41 this.personService.save(person);
42 this.person = new Person();
43 return Action.SUCCESS;
44 }
45
46 @Override
47 public String delete() throws Exception {
48 if (selectId != null)
49 personService.remove(selectId);
50 return Action.SUCCESS;
51 }
52
53
54
55
56
57
58 public Person getPerson() {
59 return person;
60 }
61
62 public void setPerson(Person person) {
63 this.person = person;
64 }
65
66 @Override
67 public String delete() throws Exception {
68 if (selectId != null)
69 personService.remove(selectId);
70 return Action.SUCCESS;
71 }
72
73 @Override
74 public String get() throws Exception {
75 person=personService.find(selectId);
76 return Action.SUCCESS;
77 }
78
79
80
81
82
83
84
85 public PaginatedListImpl getPersons() {
86 return persons;
87 }
88
89
90 }