1. package cn.edu.hj.controller;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. @Controller
  9. //@RequestMapping(value = "/hello")//表示要访问这个action的时候都要加上这个/hello路径
  10. public class HelloController {
  11. /* 接收参数getParameter()的时候:
  12. * 如果地址栏/springmvc/hello.htm上面没有传递参数,那么当id为int型的时候会报错,当id为Integer的时候值为null
  13. * 当地址栏为/springmvc/hello.htm?id=10的时候,action中有三种接收方式
  14. * 1、String hello(@RequestParam(value = "userid") int id),这样会把地址栏参数名为userid的值赋给参数id,如果用地址栏上的参数名为id,则接收不到
  15. * 2、String hello(@RequestParam int id),这种情况下默认会把id作为参数名来进行接收赋值
  16. * 3、String hello(int id),这种情况下也会默认把id作为参数名来进行接收赋值
  17. * 注:如果参数前面加上@RequestParam注解,如果地址栏上面没有加上该注解的参数,例如:id,那么会报404错误,找不到该路径
  18. */
  19. @RequestMapping(value = "/hello.htm")
  20. public String hello(int id){//getParameter()的方式
  21. System.out.println("hello action:"+id);
  22. //      return "hello";
  23. return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要写上绝对路径
  24. }
  25. //返回页面参数的第一种方式,在形参中放入一个map
  26. @RequestMapping(value = "/hello1.htm")
  27. public String hello(int id,Map<String,Object> map){
  28. System.out.println("hello1 action:"+id);
  29. map.put("name", "huangjie");
  30. return "hello";
  31. }
  32. //返回页面参数的第二种方式,在形参中放入一个Model
  33. @RequestMapping(value = "/hello2.htm")
  34. public String hello2(int id,Model model){
  35. System.out.println("hello2 action:"+id);
  36. model.addAttribute("name", "huangjie");
  37. //这个只有值没有键的情况下,使用Object的类型作为key,String-->string
  38. model.addAttribute("ok");
  39. return "hello";
  40. }
  41. //得到request,response,session等,只要在方法形参中声明参数即可
  42. @RequestMapping(value = "/hello3.htm")
  43. public String hello3(HttpServletRequest request){
  44. String id = request.getParameter("id");
  45. System.out.println("hello3 action:"+id);
  46. return "hello";
  47. }
  48. }

springMvc参数传递的方法的更多相关文章

  1. SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession

    SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession 2017-11-27 16:44:51 douunderstand 阅 ...

  2. 使用SpringMVC参数传递时,解决get请求时中文乱码的问题

    问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码. 解决办法: 一,  我们需要把request.getParameter(“参数名”)获取到的字符串先 ...

  3. SpringMVC参数传递方案

    SpringMVC参数传递方案 登录 @PostMapping("/login") @ResponseBody public Map login(String username, ...

  4. 8.SpringMVC参数传递

    页面参数传递到controller, 可被同名(与页面标签上的name名对应)的参数接收,用request设值,页面再取出来. 注意乱码解决办法: ①如果是get提交,则在tomcat的server. ...

  5. SpringMVC的controller方法中注解方式传List参数使用@RequestBody

    在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...

  6. java 对象的this使用 java方法中参数传递特性 方法的递归

    一.this关键字,使用的情形,以及如何使用. 1.使用的情形 类中的方法体中使用this  --初始化该对象 类的构造器中使用this --引用,调用该方法的对象 2.不写this,调用 只要方法或 ...

  7. SpringMVC基础-controller方法中的参数注解

    @PathVariable  映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在 SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义 通过 ...

  8. SSM-SpringMVC-21:SpringMVC中处理器方法之返回值Object篇

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 今天要记录的是处理方法,返回值为Object的那种,我给它分了一下类: 1.返回值为Object数值(例如1) ...

  9. SSM-SpringMVC-20:SpringMVC中处理器方法之返回值void篇

      ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回 ...

随机推荐

  1. UOJ348. 【WC2018】州区划分

    UOJ348. [WC2018]州区划分 http://uoj.ac/problem/348 分析: 设\(g(S)=(\sum\limits_{x\in S}w_x)^p[合法]\) \(f(S)\ ...

  2. LeetCode Construct the Rectangle

    原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...

  3. TexStudio 非常好用的Latex软件

    先大概写一下,免得忘了,等有时间详细补充. 跨平台.免费. 语法高亮 方便的公式.符号选择界面 可以配置Latex,pdflatex,xelatex等默认编译命令 集成了pdf阅读器,可在阅读器中浏览 ...

  4. redis之 Redis常用数据类型

    Redis最为常用的数据类型主要有以下7种: 一. String (字符) 常用命令:  set,get,decr,incr,mget 等. 应用场景:String是最常用的一种数据类型,普通的key ...

  5. poj 2154 Color——带优化的置换

    题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...

  6. (转)红帽 Red Hat Linux相关产品iso镜像下载【百度云】【更新6.7 Boot Disk】

    不为什么,就为了方便搜索,特把红帽EL 5.EL6 的各版本整理一下,共享出来. RedHat Enterprise Server 6.7 for i386 Boot Disk:rhel-server ...

  7. eclipse中删除tomcat server 导致不能重新创建该server

    定位到:workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings 1 打开org.eclipse.jst.server.tomca ...

  8. 数据库:ubantu下MySQL安装指南

    http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 安装MySQL sudo apt-get install mys ...

  9. mac 下 配置appium +ios真机环境

    mac系统:10.11.6 xcode:7 appium:1.5.3 iphone: 6 p 1.搭建 appium 安卓的环境: 1.jdk 2.sdk 3.appium 4.配置环境变量 mac下 ...

  10. PowerDesigner中的域(Domain)的概念及应用

    一.概念 域:实际上就是一个取值范围,也可扩展为一个数据类型.域可以定义检查约束.取值范围.最大值.最小值.默认值等. 域是通过用户自定义类型实现的,定义一个域的后,可以实多个实体的属性共享,这也模型 ...