首页 技术 正文
技术 2022年11月12日
0 收藏 716 点赞 4,761 浏览 1979 个字

①:@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,等等

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857