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

 

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...