3、LoginAction――getJson():
public String getJson() throws IOException{
List
userList = new ArrayList
(); userList.add(new UserDTO("1","chy","123",getCurrentTime(),getCurrentTime())); userList.add(new UserDTO("2","mxx","123",getCurrentTime(),getCurrentTime())); userList.add(new UserDTO("3","chk","123",getCurrentTime(),getCurrentTime())); if("chy".equals(username)){ userList.add(new UserDTO("4","chy","123",getCurrentTime(),getCurrentTime())); } //当与后台交互时、可以根据前台回传的页号与每页记录数查询数据 //List
userList = userService.getUsers(page, rows); 这样就可以动态获取每页记录数、前台动态展示。 //但是现在前台的总页数却变成每页的记录数、这样就要求还要有个service查询总记录数、然后重新构造Json传回到前台 //int count = userService.getUersCount(); /* * 想要按照某个列进行排序的时候、就使用前台传过来的关于排序的字段和排序方式 sort、order * List
userList = userService.getUsers(page, rows, sort, order) */ System.out.println("sort" + sort + " order" + order); Map
m = new HashMap
(); m.put("total", 12); m.put("rows", userList); System.out.println(getJson(m)); PrintWriter pw = ServletActionContext.getResponse().getWriter(); pw.write(getJson(m)); pw.flush(); pw.close(); return null; } private String getCurrentTime(){ SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return sDateFormat.format(new java.util.Date()); } private String getJson(Object o) throws IOException, JsonGenerationException, JsonMappingException { ObjectMapper om = new ObjectMapper(); StringWriter sw = new StringWriter(); JsonGenerator jg = new JsonFactory().createJsonGenerator(sw); om.writeva lue(jg, o); jg.close(); return sw.toString(); }
4、UserDTO:
package com.chy.ssh.business.bean;
import java.io.Serializable;
@SuppressWarnings("serial")
public class UserDTO implements Serializable{
private String id;
private String userName;
private String passWord;
private String createTime;
private String u