SSM(1)ajax的入门使用

来源:互联网 发布:电脑音频剪辑软件 编辑:程序博客网 时间:2024/06/06 07:00

**# 注册板块的信息获取 #
  ## 前端jqery发送ajax获取json信息 ##**

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

  1. 设置code状态码 表示ajax请求成功失败 (通过success和fail存放
  2. 设置message 设置信息 (不重要
  3. 设置infor的Map 存放ajax的请求信息 (最重要
  4. function介绍 (返回类型都是Message 可以连续请求
  5. success 和fail表明返回的是成功还是失败类型的信息 (static类型
  6. add存放请求和返回信息

`

public class JsonMessage {//成功为200private 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)@ResponseBodypublic 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:

原创粉丝点击