zoukankan      html  css  js  c++  java
  • 关于springMVC的一些常用注解

    ①:@RequestMapping("/helloworld")、@RequestMapping(value="/emp", method=RequestMethod.GET)

    写在类上可用于区分模块

    写在方法上可指定请求的方法

    带method=RequestMethod.GET:可以指定请求的方法,有四种情况:get(用于获取),post(用于添加),delete(用于删除),put(用于更新),用于rest风格网站

    ②:

    @RequestMapping("/testView")
    public String testView(){
      System.out.println("testView");
      return "index";
    }

    返回的index会被视图解析器自动解释为index.jsp页面(后缀可自己设置)

    ③:return "redirect:/index.jsp";

    这样写,方法执行后会重定向到index.jsp页面。

    ④:@RequestParam(value="id",required=false) Integer id

    指定请求参数,参数名为id,required=false指定参数为不必要,如果设置为true的话就为必要参数

    例如:

    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false)String page,
    @RequestParam(value="rows",required=false)String rows,
    User s_user,HttpServletResponse response) throws Exception{
    PageBean pageBean=new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("userName", StringUtil.formatLike(s_user.getUserName()));
    map.put("start", pageBean.getStart());
    map.put("size", pageBean.getPageSize());
    List<User> userList=userService.find(map);
    long total=userService.getTotal(map);
    JSONObject result=new JSONObject();
    JSONArray jsonArray=JSONArray.fromObject(userList);
    result.put("rows", jsonArray);
    result.put("total", total);
    ResponseUtil.write(response, result);
    return null;
    }

    ⑤:@PathVariable("id") Integer id

    用法:

    @RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
    public String input(@PathVariable("id") Integer id, Map<String, Object> map){
      map.put("employee", employeeDao.get(id));
      map.put("departments", departmentDao.getDepartments());
      return "input";
    }

    把请求地址后面的数字解析为id参数,用于rest风格的网站

    @PathVariable 绑定 URL 占位符到入参
    带占位符的 URL 是 Spring3.0 新增的功能,该功能在 •
    SpringMVC 向 REST 目标挺进发展过程中具有里程碑的
    意义
    通过 @PathVariable 可以将 URL 中占位符参数绑定到控 •
    制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
    @PathVariable("xxx") 绑定到操作方法的入参中。

    ⑥:

    使用 @CookieValue 绑定请求中的 Cookie 值
    @CookieValue 可• 让处理方法入参绑定某个 Cookie 值

    ⑦:使用 POJO 对象绑定请求参数值(就比如:写上参数User)

    Spring MVC 会按• 请求参数名和 POJO 属性名进行自动匹
    配,自动为该对象填充属性值。支持级联属性。
    如:dept.deptId、dept.address.tel 等

    ⑧:使用 Servlet API 作为入参

    可以写上参数:HttpServletRequest,HttpServletResponse,HttpSession,等等

  • 相关阅读:
    语音
    elasticsearch-HQ 安装与使用
    身份证号归属地数据库
    mysql 检查一个字符串是不是身份证号
    导出微信聊天记录并生成词云
    云平台Linux主机安装流程
    7za的压缩与解压
    把linux文件夹压缩成tar.gz的命令
    Python中包(package)的调用方式
    golang学习笔记 ----读写文件
  • 原文地址:https://www.cnblogs.com/dmchzp/p/5195929.html
Copyright © 2011-2022 走看看