在上一随笔记录的基础上,现记录编写处理带有参数的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"

spring mvc 编写处理带参数的Controller的更多相关文章

  1. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  2. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  3. Spring MVC基础知识整理➣View与Controller数据交互

    概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...

  4. Spring MVC 接受的请求参数

    目录 1. 概述 2. 详解 2.1 处理查询参数 2.2 处理路径参数接受输入 2.3 处理表单 3. 补充内容 3.1 Ajax/JSON 输入 3.2 multipart参数 3.3 接收 he ...

  5. Spring MVC 基础注解之@RequestMapping、@Controller、(二)

    我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...

  6. Spring MVC之Action输入参数

    第一部分:Action输入参数Spring MVC 通过@RequestMapping注解映射请求,最终的真正执行代码为处理器方法,即@RequestMapping注解的方法.Spring MVC方法 ...

  7. SpringMVC(八):使用Servlet原生API作为Spring MVC hanlder方法的参数

    在SpringMVC开发中,是有场景需要在Handler方法中直接使用ServletAPI. 在Spring MVC Handler的方法中都支持哪些Servlet API作为参数呢? --Respo ...

  8. Spring MVC 不能正常获取参数的值

    最近在开发时遇到一个非常奇怪的问题,在tomcat8中使用Spring MVC框架,在Controller中的方法参数无法正常获取到相应的值,将tomcat版本换成7.0就解决了. 记录以下解决过程, ...

  9. Spring MVC 接收Json格式参数

    今天做了一个关于表格排序的功能,可以通过右边的箭头做排序操作,每次操作需要通过Ajax将每条记录的Id数组作为参数去发送请求, 后台Spring MVC接到参数后作更改序号操作. 前端页面发送请求的代 ...

随机推荐

  1. 百度地图和高德地图的API视频教程

    学习地址: http://www.houdunren.com/houdunren18_lesson_152?vid=10228 素材地址: https://gitee.com/houdunwang/v ...

  2. Python基础——时间

    导入时间模块 import time 时间戳 print(time.time()) 获取本地时间 print(time.localtime(time.time())) 时间显示格式化 print(ti ...

  3. python入门学习笔记1:Python与C的简单区别

    转载于:https://www.cnblogs.com/mlgjb/p/7892130.html 并做适当修改 一:简单比较   C语言 python 执行速度 快 慢 跨平台 不可以 可以 用途 操 ...

  4. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  5. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  6. while循环输出的表格

    方法一: <?php echo '<table border="1" width="800" align="center"> ...

  7. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  8. Python虚拟机之异常控制流(四)

    Python虚拟机中的异常控制流 先前,我们分别介绍了Python虚拟机之if控制流(一).Python虚拟机之for循环控制流(二)和Python虚拟机之while循环控制结构(三).这一章,我们来 ...

  9. luogu1169 [ZJOI2007]棋盘制作

    悬线法 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  10. 转:模式窗口showModalDialog的用法总结

    模式窗口showModalDialog的用法总结   最近几天一直在处理模式窗口的问题,索性写了这篇总结,以供参考: 1.打开窗口:var handle = window.showModalDialo ...