在上一随笔记录的基础上,现记录编写处理带有参数的Controller。

@Controller //这个注解会告知<context:component:scan> 将HomeController自动检测为一个Bean
@RequestMapping("/home")  //这是根Url
public class HomeController {
    
    private UserService userService;
    
    @Autowired
    public HomeController(UserService userService){
        this.userService = userService;
    }
    
    //指定请求的路径,处理get请求的路径为 xxx/home/home.html的请求
    @RequestMapping(value="home.html",method=RequestMethod.GET)
    public String showHomePage(@RequestParam("name")String username,Model model){
        model.addAttribute("username",username);
        model.addAttribute("count", userService.getUsersCount());
        return "home";
    }
}

注意到,在showHomePage方法的输入参数中,有@RequestParam("name")String username这个参数,表示接收请求路径的参数name,并赋值给username.

如果请求的路径的参数名,与方法的入参的变量名一致,就可以不需要@RequestParam这个注解。

以上showHomePage这个方法处理的请求路径为:项目根路径/home/home.html?name="who"

最新文章

  1. Java可变参数讲解
  2. 【Mysql】权限管理
  3. 07.19 Linux命令 cd
  4. 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
  5. Git 和 Github的关系
  6. Python-接口自动化(七)
  7. pg_dump 数据处理
  8. html标签必备
  9. 遇到问题---hosts不起作用问题的解决方法
  10. MonkeyRunner进坑——errors importing other modules
  11. 获取spring的ApplicationContext几种方式【转】
  12. Three Pieces CodeForces - 1065D (BFS)
  13. selenium测试(Java)-- 一组元素操作(十一)
  14. web开发之微信公众号---微信公众好开发
  15. HttpHelper类及调用
  16. 算法笔记_151:算法提高 01背包(Java)
  17. 9.spring:事务管理(下):声明式事务管理
  18. python学习笔记:第7天 深浅拷贝
  19. 设置eclipse中jsp/html文件好看的自动排版
  20. 使用Git操作码云

热门文章

  1. 10个MCU常用的基础知识
  2. 关于stm32优先级大小的理解
  3. Educational Codeforces Round 41 E. Tufurama (961E)
  4. ubuntu12.04 ppa安装pidgin
  5. linux学习-使用者身份切换
  6. nmap命令扫描存活主机
  7. jubeeeeeat(网络流)
  8. BZOJ 4425: [Nwerc2015]Assigning Workstations分配工作站
  9. 光学字符识别OCR-7语言模型
  10. 光学字符识别OCR-4