springMvc参数传递的方法
- package cn.edu.hj.controller;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- @Controller
- //@RequestMapping(value = "/hello")//表示要访问这个action的时候都要加上这个/hello路径
- public class HelloController {
- /* 接收参数getParameter()的时候:
- * 如果地址栏/springmvc/hello.htm上面没有传递参数,那么当id为int型的时候会报错,当id为Integer的时候值为null
- * 当地址栏为/springmvc/hello.htm?id=10的时候,action中有三种接收方式
- * 1、String hello(@RequestParam(value = "userid") int id),这样会把地址栏参数名为userid的值赋给参数id,如果用地址栏上的参数名为id,则接收不到
- * 2、String hello(@RequestParam int id),这种情况下默认会把id作为参数名来进行接收赋值
- * 3、String hello(int id),这种情况下也会默认把id作为参数名来进行接收赋值
- * 注:如果参数前面加上@RequestParam注解,如果地址栏上面没有加上该注解的参数,例如:id,那么会报404错误,找不到该路径
- */
- @RequestMapping(value = "/hello.htm")
- public String hello(int id){//getParameter()的方式
- System.out.println("hello action:"+id);
- // return "hello";
- return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要写上绝对路径
- }
- //返回页面参数的第一种方式,在形参中放入一个map
- @RequestMapping(value = "/hello1.htm")
- public String hello(int id,Map<String,Object> map){
- System.out.println("hello1 action:"+id);
- map.put("name", "huangjie");
- return "hello";
- }
- //返回页面参数的第二种方式,在形参中放入一个Model
- @RequestMapping(value = "/hello2.htm")
- public String hello2(int id,Model model){
- System.out.println("hello2 action:"+id);
- model.addAttribute("name", "huangjie");
- //这个只有值没有键的情况下,使用Object的类型作为key,String-->string
- model.addAttribute("ok");
- return "hello";
- }
- //得到request,response,session等,只要在方法形参中声明参数即可
- @RequestMapping(value = "/hello3.htm")
- public String hello3(HttpServletRequest request){
- String id = request.getParameter("id");
- System.out.println("hello3 action:"+id);
- return "hello";
- }
- }
springMvc参数传递的方法的更多相关文章
- SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession
SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession 2017-11-27 16:44:51 douunderstand 阅 ...
- 使用SpringMVC参数传递时,解决get请求时中文乱码的问题
问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码. 解决办法: 一, 我们需要把request.getParameter(“参数名”)获取到的字符串先 ...
- SpringMVC参数传递方案
SpringMVC参数传递方案 登录 @PostMapping("/login") @ResponseBody public Map login(String username, ...
- 8.SpringMVC参数传递
页面参数传递到controller, 可被同名(与页面标签上的name名对应)的参数接收,用request设值,页面再取出来. 注意乱码解决办法: ①如果是get提交,则在tomcat的server. ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- java 对象的this使用 java方法中参数传递特性 方法的递归
一.this关键字,使用的情形,以及如何使用. 1.使用的情形 类中的方法体中使用this --初始化该对象 类的构造器中使用this --引用,调用该方法的对象 2.不写this,调用 只要方法或 ...
- SpringMVC基础-controller方法中的参数注解
@PathVariable 映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在 SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义 通过 ...
- SSM-SpringMVC-21:SpringMVC中处理器方法之返回值Object篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 今天要记录的是处理方法,返回值为Object的那种,我给它分了一下类: 1.返回值为Object数值(例如1) ...
- SSM-SpringMVC-20:SpringMVC中处理器方法之返回值void篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回 ...
随机推荐
- UOJ348. 【WC2018】州区划分
UOJ348. [WC2018]州区划分 http://uoj.ac/problem/348 分析: 设\(g(S)=(\sum\limits_{x\in S}w_x)^p[合法]\) \(f(S)\ ...
- LeetCode Construct the Rectangle
原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...
- TexStudio 非常好用的Latex软件
先大概写一下,免得忘了,等有时间详细补充. 跨平台.免费. 语法高亮 方便的公式.符号选择界面 可以配置Latex,pdflatex,xelatex等默认编译命令 集成了pdf阅读器,可在阅读器中浏览 ...
- redis之 Redis常用数据类型
Redis最为常用的数据类型主要有以下7种: 一. String (字符) 常用命令: set,get,decr,incr,mget 等. 应用场景:String是最常用的一种数据类型,普通的key ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...
- (转)红帽 Red Hat Linux相关产品iso镜像下载【百度云】【更新6.7 Boot Disk】
不为什么,就为了方便搜索,特把红帽EL 5.EL6 的各版本整理一下,共享出来. RedHat Enterprise Server 6.7 for i386 Boot Disk:rhel-server ...
- eclipse中删除tomcat server 导致不能重新创建该server
定位到:workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings 1 打开org.eclipse.jst.server.tomca ...
- 数据库: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 ...
- 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下 ...
- PowerDesigner中的域(Domain)的概念及应用
一.概念 域:实际上就是一个取值范围,也可扩展为一个数据类型.域可以定义检查约束.取值范围.最大值.最小值.默认值等. 域是通过用户自定义类型实现的,定义一个域的后,可以实多个实体的属性共享,这也模型 ...