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. Java String Split Method

    Java String.split() method 有如下几种特殊情况: 1. 分隔符出现在首尾 public static void main(String args[]) { String St ...

  2. flask+blueprint路由配置

    1.flask默认的静态文件和html文件在app应用文件夹里的相应文件夹下:app // Flask||--static ||--templates |静态文件默认的url地址为:url_prefi ...

  3. apache配置详解 apache安装路径

    http://www.linuxidc.com/Linux/2015-02/113921.htm 不同apache的安装方式 的安装目录示例 http://www.121down.com/articl ...

  4. HDOJ4768(二分区间)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. [转载]python datetime处理时间

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  6. [cinder] volume type 使用简记

    cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...

  7. 转:InnoDB Page Structure(InnoDB页面结构详解)

    InnoDB Page Structure(InnoDB页面结构详解) 此转载自登博的博客,给大家分享.

  8. 关于Java中集合的讲解~

    http://blog.csdn.net/zccst/article/details/5092816 comparable& Comparator 都是用来实现集合中的排序的,只是Compar ...

  9. python django ORM 性能优化 select_related & prefetch_related

    q = models.UserInfo.objects.all() select * from userinfo select * from userinfo inner join usertype ...

  10. Dataguard ORA-19909 ORA-01110

    在创建ORACLE 10G Dataguard时,报错: Datafile 1 (ckpscn 24967685451) is orphaned on incarnation#=6 MRP0: Bac ...