注解@RequestParam与@PathVariable的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/System_out_print_Boy/article/details/80759626

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

@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;
    }

猜你喜欢

转载自blog.csdn.net/System_out_print_Boy/article/details/80759626