1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| package com.course.controller;
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject; import com.course.util.DataGripObject; import com.course.vo.User; import com.course.vo.UserInfo;
@Controller @RequestMapping("user") public class UserController { static List<String> usernameList = new ArrayList<>(); static { usernameList.add("admin"); usernameList.add("user1"); usernameList.add("user2"); }
@RequestMapping(value = "checkUserName1", method = RequestMethod.POST) public void checkUserName1(String username, HttpServletResponse httpServletResponse) { String message = "该用户不存在"; for (String nameItem : usernameList) { if (nameItem.equals(username)) { message = "该用户存在"; } } try { httpServletResponse.setCharacterEncoding("UTF-8"); httpServletResponse.setContentType("text/html;charset=utf-8"); PrintWriter out = httpServletResponse.getWriter(); out.write(message); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } @RequestMapping(value = "checkUserName2", method = RequestMethod.POST) @ResponseBody public String checkUserName2(String username) { String code = "0"; for (String nameItem : usernameList) { if (nameItem.equals(username)) { code = "1"; } } return code; } @RequestMapping(value = "getOneUser1", method = RequestMethod.GET, produces = "application/json;charset=utf-8") @ResponseBody public String getOneUser1() { User user = new User(); user.setId(1); user.setName("小明"); user.setAge(10); String json = JSONObject.toJSONString(user); return json; } @RequestMapping(value = "getUserList1", method = RequestMethod.GET, produces = "application/json;charset=utf-8") @ResponseBody public String getUserList1() { List<User> userList = new ArrayList<>(); userList.add(new User(1, "小明", 10)); userList.add(new User(2, "小李", 11)); userList.add(new User(3, "小张", 12)); String json = JSONObject.toJSONString(userList); return json; }
@RequestMapping(value = "getOneUser2", method = RequestMethod.GET) @ResponseBody public User getOneUser2() { User user = new User(); user.setId(1); user.setName("小明"); user.setAge(10); return user; } @RequestMapping(value = "getUserList2", method = RequestMethod.GET) @ResponseBody public List<User> getUserList2() { List<User> userList = new ArrayList<>(); userList.add(new User(1, "小明", 10)); userList.add(new User(2, "小李", 11)); userList.add(new User(3, "小张", 12)); return userList; }
@RequestMapping(value = "getUserList3") @ResponseBody public Map<String, Object> getUserList3() { Map<String, Object> resultMap = new HashMap<>(); List<UserInfo> userList = new ArrayList<>(); userList.add(new UserInfo(1, "小明", 10, "address1", new Date())); userList.add(new UserInfo(2, "小李", 11, "address2", new Date())); userList.add(new UserInfo(3, "小张", 12, "address3", new Date())); resultMap.put("total", userList.size()); resultMap.put("rows", userList); return resultMap; }
@RequestMapping(value = "getUserList4") @ResponseBody public DataGripObject getUserList4() { DataGripObject dataGripObject = new DataGripObject(); List<UserInfo> userList = new ArrayList<>(); userList.add(new UserInfo(1, "user1", 10, "address1", new Date())); userList.add(new UserInfo(2, "user2", 11, "address2", new Date())); userList.add(new UserInfo(3, "user3", 12, "address3", new Date())); dataGripObject.setTotal(userList.size()); dataGripObject.setRows(userList); return dataGripObject; } }
|