您的位置:首页 > 其它

SSM(1)ajax的入门使用

2017-12-01 21:05 225 查看
**# 注册板块的信息获取 #

  ## 前端jqery发送ajax获取json信息 ##**

1.于com.bean建立Message.java 存放ajax页面的信息

设置code状态码 表示ajax请求成功失败 (通过success和fail存放

设置message 设置信息 (不重要

设置infor的Map 存放ajax的请求信息 (最重要

function介绍 (返回类型都是Message 可以连续请求

success 和fail表明返回的是成功还是失败类型的信息 (static类型

add存放请求和返回信息

`

public class JsonMessage {

//成功为200
private int code;
private  String message;
private Map<String, Object> map=new HashMap<String, Object>();

//成功
public static JsonMessage success() {
JsonMessage jsonMessage=new JsonMessage();
jsonMessage.setCode(200);
jsonMessage.setMessage("处理成功");

return jsonMessage;
}
//失败
public static JsonMessage fail() {
JsonMessage jsonMessage=new JsonMessage();
jsonMessage.setCode(500);
jsonMessage.setMessage("处理失败");

return jsonMessage;
}
public JsonMessage add(String key,Object value) {
this.getMap().put(key, value);
return this;
}
……
public  ……………… get&&put(){
……
}


}

`

2.配置servlet层和controller层

1.servlet层

查询所有人的信息

(example是空 可以直接selectByExample(null)

public List<Infor> getInfors() {
List<Infor> infors=null;
InforExample inforExample =new InforExample();
Criteria criteria=inforExample.createCriteria();
infors=inforMapper.selectByExample(inforExample);
return infors;
}


2.controller层

1. 注解@ResponseBody返回的页面不是html是json

2. 注解@RequestParam(value = “pageNumber”, defaultValue = “1”)请求的页面参数,默认为1 (map没用

3. PageHelper.startPage(pageNumber, 5); //开始的页数和每页数据的个数

4. infors = inforServlet.getInfors(); 获取信息

5. PageInfo page = new PageInfo(infors, 6); 将分页信息传入到Message的map中 并将Message作为ajax信息返回

6. 3,4,5具有顺序性

@RequestMapping(value = "/getInforsByJson", method = RequestMethod.GET)
@ResponseBody
public JsonMessage getInforsByJson(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
Map<String, Object> map) {

// 开始的页面和 一夜显示的信息数量
PageHelper.startPage(pageNumber, 5);
System.out.println("传递的页面:" + pageNumber);

// 获取查询的信息
List<Infor> infors;
infors = inforServlet.getInfors();

// 将查询的内容用pageinfo包装,连续显示6页
PageInfo page = new PageInfo(infors, 6);

// 分装进model中
return JsonMessage.success().add("pageInfo", page);
}

@RequestMapping(value = "/getJsonInfor")
public String getJsonInfor() {
return "getJsonInfor";
}


配置jsp页面

url:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: