随笔 - 110  文章 - 1  评论 - 0  阅读 - 77445

Spring MVC Controller接收参数方式

复制代码
    /**
     * 方式一:使用servlet原生的方式,request.getParameter("key")获取参数;
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/get1")
    ModelAndView get1(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        return null;
    }

    /**
     * 方式二:同名规则接收传参
     * @param username
     * @return
     * @throws Exception
     */
    @RequestMapping("/get2")
    ModelAndView get2(String username)throws Exception{
        return null;
    }
    
    /**
     * 方式二的另一种形式:前后台参数名不同,使用@RequestParam("前台参数名称")注解
* 如前台传参:username1,Controller中使用username *
@param username * @return * @throws Exception */ @RequestMapping("/get3") ModelAndView get3(@RequestParam("username1")String username) throws Exception{ return null; } /** * 方式三:模型传参 * @param user * @return * @throws Exception */ @RequestMapping("/get4") ModelAndView get4(User user)throws Exception{ return null; }
/** * 方式四:使用地址栏传参,需要使用注解@PathVariable("parameter_name") * 如请求地址为:/get5/zhangsan * 此时地址栏的zhangsan会被当作username的参数值 * @param username * @return * @throws Exception */ @RequestMapping("/get5/{username}") ModelAndView get5(@PathVariable("username")String username)throws Exception{ return null; }
复制代码

 

posted on   骑着母猪去打猎  阅读(201)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示