1、@PathVariable

@PathVariable绑定URI模板变量值

@PathVariable是用来获得请求url中的动态参数的

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。//配置url和方法的一个关系@RequestMapping("item/{itemId}")

/* @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求
* @responsebody表示该方法的返回结果直接写入HTTP response body中
*一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response *body中。
*比如异步获取json数据,加上@responsebody后,会直接返回json数据。*
*@Pathvariable注解绑定它传过来的值到方法的参数上
*用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
*/
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId){

@RequestMapping("/zyh/{type}")
public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException {
String url = "http://wx.diyfintech.com/zyhMain/" + type;
if (type != 1 && type != 2) {
throw new IllegalArgumentException("参数错误");
}
String encodeUrl = URLEncoder.encode(url, "utf-8");
String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", ""));
return "redirect:" + redirectUrl;
}

2、@RequestParam

在SpringMVC后台控制层获取参数的方式主要有两种:

一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取

这里主要讲这个注解 @RequestParam

接下来我们看一下@RequestParam注解主要有哪些参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

@Controller
@RequestMapping("/wx")
public class WxController { @Autowired
private WxService wxService;
private static final Log log= LogFactory.getLog(WxController.class); @RequestMapping(value = "/service",method = RequestMethod.GET)
public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce,
@RequestParam String echostr, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}else
out.print("fail");
out.flush();
out.close();
}

参考文章:https://www.cnblogs.com/zlw-xf/p/8035215.html

springMVC中的注解@RequestParam与@PathVariable的区别的更多相关文章

  1. SpringMVC 中的注解@RequestParam与@PathVariable的区别

    @PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上.//配置 ...

  2. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  3. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

  4. SpringMVC 中 @ControllerAdvice 注解

    SpringMVC 中 @ControllerAdvice 注解 1.@ControllerAdvice 1.1 全局异常处理 1.2 全局数据绑定 1.3 全局数据预处理 原文地址: 江南一点雨:S ...

  5. @RequestParam 和 @ PathVariable 的区别

    @RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...

  6. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  7. Spring|SpringMVC中的注解

    文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...

  8. springmvc中ModelAttribute注解应用在参数中

    可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...

  9. SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别

    @Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...

随机推荐

  1. android-基础编程-ViewPager

    ViewPager android 提供的基础V4包,android studio 导入gradle compile 'com.android.support:support-v4:25.0.0' 1 ...

  2. qscoj 喵哈哈村的打印机游戏 区间dp

    点这里去看题 区间dp ,dp[l][r][d]代表从l到r的区间底色为d,具体看代码 第一次见到区间dp...两个小时对着敲了五遍终于自己敲懂了一遍ac #include<bits/stdc+ ...

  3. 网络编程——I/O复用

    int select( int nfds, fd_set FAR* readfds, fd_set * writefds, fd_set * exceptfds, const struct timev ...

  4. 通过URL触发Jenkins构建

    用Jenkins做持续集成时,一般会使用webhooks触发构建,或者定时构建,这里记录用URL的方式触发Jenkins构建. Note: This assumes you're using Jenk ...

  5. Windows 10 IoT Core 17101 for Insider 版本更新

    除夕夜,微软发布了Windows 10 IoT Core 17101 for Insider 版本更新,本次更新只修正了一些Bug,没有发布新的特性. 已知的问题: F5 driver deploym ...

  6. 包建强的培训课程(15):Android App热修复技术

    @import url(/css/cuteeditor.css); Normal 0 10 pt 0 2 false false false EN-US ZH-CN X-NONE $([{£¥·‘“〈 ...

  7. Chapter 1: Plug-in programing from past to the future

    It is the best time. Although the internal API of Android not allowed to be modified by google play, ...

  8. 自学自用 = B站(操作系统_清华大学(向勇、陈渝)) 未完待续。。

    视频地址 https://www.bilibili.com/video/av6538245 介绍 本篇博客,旨在记录视频学习的要点,所以格式随意, 方便本人日后自考和回忆,有兴趣的朋友可以评论讨论. ...

  9. 阿里巴巴Java开发规约及插件安装

    [上海尚学堂编辑整理]10.14日,阿里巴巴在杭州云栖大会上,正式发布了由阿里巴巴 P3C 项目组,经过 近一年的持续研发,正式发布众所期待的 <阿里巴巴 Java 开发规约>的扫描插件. ...

  10. JavaScript 基础排序的实现(二)

    继上一篇O(n^2)的排序算法后,这一篇主要记录O(n*logn)的排序算法 1.快排(快速排序) 这一算法的核心思想为,先随机选一个数作为标兵或者说是标记(这个数一般来说选择该无序数组的中间那个元素 ...