关于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,等等
关于springMVC的一些常用注解的更多相关文章
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- SpringMVC入门和常用注解
SpringMVC的基本概念 关于 三层架构和 和 MVC 三层架构 我们的开发架构一般都是基于两种形式,一种是 C/S 架构,也就是客户端/服务器,另一种是 B/S 架构,也就 是浏览器服务器.在 ...
- SpringMVC中的常用注解
RequestParam 作用: 用于 将请求参数区数据 映射到 功能处理方法的参数上. 属性: value 请求参数中的名称 required 请求参数中是否必须提供此参数. 默认值: ...
- <SpringMvc>入门二 常用注解
1.@RequestMapping @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME ...
- SpringMVC基础03——常用注解之@RequestMapping
1.用法 SpringMVC使用@RequestMapping注解,为控制器指定可以处理哪些URL请求,并且可以指定处理请求的类型(POST/GET),如果@RequestMapping没有指定请求的 ...
- 【springmvc学习】常用注解总结
@Controller 在springmvc中,我们用它来告诉前端控制器,他这个类是controller,也就是springmvc的一个对象了,我们在spring.xml配置文件中用<conte ...
- springMvc几个常用注解
浏览器本省就是get ,post 可以用form表单 @RequestMapping: 作用:用来映射请求的url @RequestMapping注解的多个属性是与(且)的关系,必须同时满足 位置:可 ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
随机推荐
- Python -- 网络编程 -- 抓取网页图片 -- 图虫网
字符串(str)编码成字节码(bytes),字节码解码为字符串 获取当前环境编码:sys.stdin.encoding url编码urllib.parse.quote() url解码urllib.pa ...
- WPF开发的彩票程序(练手好例子)
前言 WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大. 应朋友之邀,编写了一个小程序.程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑. 对于新手学习有很好的借鉴意义, ...
- kubernetes ingress到pod的数据流
假设现在有一个ingress暴露的服务 example.com.cn,查看一下流量是怎么传输到后端的 使用kubectl get ingress可以查看到如下内容,example.com.cn对应的i ...
- 机器学习--boosting家族之XGBoost算法
一.概念 XGBoost全名叫(eXtreme Gradient Boosting)极端梯度提升,经常被用在一些比赛中,其效果显著.它是大规模并行boosted tree的工具,它是目前最快最好的开源 ...
- 【Xmail】使用Xmail搭建局域网邮件服务器
下载地址: http://www.xmailserver.org/xmail-1.27.win32bin.zip,当前最新版本 1.27. 解压文件:xmail-1.27.win32bin.zip ...
- Java中的数据验证
原文链接:https://www.cuba-platform.com/blog/2018-10-09/945 翻译:CUBA China CUBA-Platform 官网 : https://www. ...
- Mysql备份工具xtraback全量和增量测试
Mysql备份工具xtraback全量和增量测试 xtrabackup 是 percona 的一个开源项目,可以热备份innodb ,XtraDB,和MyISAM(会锁表) 官方网址http:// ...
- FacebookFriendAdderPro
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Fe ...
- Ubuntu下NAT模式配置静态IP
编辑文件/etc/network/interfaces: 并用下面的行来替换有关eno16777736的行: # The primary network interfaceauto eno167777 ...
- iOS--支付宝环境集成
1.下载支付宝SDK以及Demo https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType= ...