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. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  2. java文本文件读写

    java的IO系统中读写文件使用的是Reader和Writer两个抽象类,Reader中的read()和close()方法是抽象方法,Writer中的write().flush()和close()方法 ...

  3. python3之编码

    这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件 ...

  4. ISE(Iris Server Engine)是一个基于现代C++的跨平台(Linux和Windows)框架

    ISE(Iris Server Engine)是一个基于现代C++的跨平台(Linux和Windows)的高性能多线程并发网络服务器程序框架.它封装了琐碎的socket以及各种操作系统APIs,以面向 ...

  5. java代码继承基础

    总结:继承是java编程的核心,我搞不明白,子类对象调用父类的成员方法时.父类的带参构造方法有什么用,还赋值了 package com.bc; //普通类 public class yt { publ ...

  6. 阿里巴巴开源项目: canal 基于mysql数据库binlog的增量订阅&消费

    背景 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数据库同步业务,主要是基于trigger的方式获取增 量变更,不过从2010年开始,阿里系公司开始逐步的 ...

  7. 查看,修改ceph节点的ceph配置命令

    标签(空格分隔): ceph,ceph运维,ceph配置 查看ceph配置 1. 查看ceph默认配置: # ceph --show-config 2. 查看 type.num 的ceph默认配置: ...

  8. 调试json

    console.log("======================") // 转对象 //var obj = eval('(' + data + ')'); // 转对象 // ...

  9. Flash 零日漏洞复现(CVE-2018-4878)

    项目地址:https://github.com/Sch01ar/CVE-2018-4878.git 影响版本为:Adobe Flash Player <= 28.0.0.137 攻击机器IP:1 ...

  10. 读取XML文件的指定节点的值 并转换为Item

    cmb_State_Send.ItemsSource = null; XmlDocument doc = new XmlDocument(); doc.Load("D:\\模板\\Works ...