注解@RequestParam与@PathVariable的区别

看下这段代码,显示图片的

@Controller
@RequestMapping(value = "/imgs")
public class ImgsController {

    @Autowired
    private HdfsFileService hdfsFileService;
    /**
     * 显示图片
     * @return
     */
    @RequestMapping("/showc/{id}")
    @ResponseBody
    public String showc(@PathVariable String id, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FileSystem fs = hdfsFileService.getFileSystem();

        InputStream in = null;
        try {
            in = fs.open(new Path("/kddata/food/finishprotectfingure/imgs/" + id));
            IOUtils.copyBytes(in, response.getOutputStream(), 4096, false);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
        }
        return null;
    }

在浏览器上你只需输localhost:8080/mananger/imgs/showc/uewaas12nwdqwd.img.就能访问到图片,其中,每个图片名字是不同的,所以{id}是动态的,这个时候要获取url中的这个动态参数,就用到了注解@PathVariable

@PathVariable 将请求URL中的模板变量映射到功能处理方法的参数。

在SpringMVC后台控制层获取参数的方式主要有两种:
一种是request.getParameter(“name”),另外一种是用注解@RequestParam直接获取
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

    @RequestMapping("/test")
    @ResponseBody
    public String test3(@RequestParam(value="username") String username,HttpServletRequest request){
        String age = request.getParameter("age");
        return username;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值