一、在src/spring-mvc.xml里添加接口文件夹路径
<!-- 定义控制器注解扫描包路径,控制器注解为 @Controller -->
<context:component-scan base-package="com.linyun.game.controller" />
二、在controller路径下新建一个class,DateController
package com.linyun.game.controller;
import com.linyun.game.util.BaseController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
@Controller
@RequestMapping("/date")
public class DateController extends BaseController {
/** BaseController 里写公共的返回数据 */
/** 分页*/
private Integer total= 0;
@Resource
private IUserService userService;
}
/**直接返回map的方法一 */
@RequestMapping("/getDataList")
@ResponseBody
public Map<String, Object> getDataList(HttpServletRequest request) throws ParseException, UnsupportedEncodingException {
Map<String, Object> map = new HashMap<String, Object>();
Integer startRownumStr = Integer.valueOf(request.getParameter("currentPages"));//第几页
Integer onePageNum = Integer.valueOf(request.getParameter("pageSizes"));//数据数
Integer start = (startRownumStr*onePageNum)-onePageNum;
String startRownum = start.toString();
String endRownum = request.getParameter("pageSizes");
String account = request.getParameter("account");
String beginTime = request.getParameter("startTime");
String overTime = request.getParameter("endTime");
try {
List<Object> returnList = new ArrayList<Object>();
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("id", 1);
dataMap.put("name", "xixi");
returnList.add(dataMap);
total = returnList.size();
if (returnList!= null) {
map.put("list", returnList);
map.put("total", total);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**方法2 */
@RequestMapping("/getData")
@ResponseBody
public ResultInfo getData(HttpServletRequest request) throws Exception {
ResultInfo resultInfo = new ResultInfo();
Map<String, Object> map = new HashMap<String, Object>();
try {
List<User> opList =userService.getUserList();
if (opList != null) {
map.put("opList", opList);
}
/**调用BaseController里的success*/
success(resultInfo, "成功",200);
resultInfo.setData(map);
} catch (Exception e) {
e.printStackTrace();
}
return resultInfo;
}
BaseController,公共的返回成功失败调用信息
public class BaseController{
public static String SUCCEE="succee";
public static String FAIL="fail";
/**
* 请求成功
* @param resultInfo
*/
protected void success(ResultInfo resultInfo,Object data,String token)
{
resultInfo.setCode(200);
resultInfo.setMsg("成功");
resultInfo.setToken(token);
System.out.println(resultInfo);
resultInfo.setData(data);
}
/**
* 请求失败
* @param resultInfo 结果信息
* @param errorMsg 错误信息
* @param e
*/
protected void error(ResultInfo resultInfo,String errorMsg,String code)
{
resultInfo.setCode(code);
resultInfo.setMsg(errorMsg);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
|