java接口的几种写法

本文介绍了如何在Spring MVC中配置控制器扫描路径,并详细展示了DateController的创建,包括分页操作和数据获取方法。重点讲解了如何使用@Controller注解和@Autowired注解进行资源注入。
摘要由CSDN通过智能技术生成

一、在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;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值