博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring3 MVC请求参数获取的几种方法
阅读量:4607 次
发布时间:2019-06-09

本文共 1373 字,大约阅读时间需要 4 分钟。

Spring3 MVC请求参数获取的几种方法

一、      通过@PathVariabl获取路径中的参数

@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)    public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {                System.out.println(id);        System.out.println(name);        model.addAttribute("message", "111111");        return "users";    }

例如,访问user/123/lei路径时,执行以上方法,其中,参数id=123,name=lei

二、      @ModelAttribute获取POST请求的FORM表单数据

JSP表单如下

a:
b:

Java  Pojo如下

public class Pojo{        private String a;        private int b;    }

Java Controller如下

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {         return "helloWorld"; }

三、      直接用HttpServletRequest获取

@RequestMapping(method = RequestMethod.GET) public String get(HttpServletRequest request, HttpServletResponse response) {    System.out.println(request.getParameter("a"));     return "helloWorld"; }

四、      用注解@RequestParam绑定请求参数

用注解@RequestParam绑定请求参数a到变量a

当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,

例如: @RequestParam(value="a", required=false)

Controller如下

@RequestMapping(value = "/requestParam", method = RequestMethod.GET) public String setupForm(@RequestParam("a") String a, ModelMap model) {    System.out.println(a); return "helloWorld";}

 

转载于:https://www.cnblogs.com/zhujiabin/p/4897901.html

你可能感兴趣的文章
快速入门
查看>>
Mybatis通过ID查询 && 通过name模糊查询
查看>>
SQL查询日志 查看数据库历史查询记录的方法
查看>>
4、重建二叉树------------>剑指offer系列
查看>>
Firefly
查看>>
客户/服务器程序设计范式
查看>>
Docker常用命令
查看>>
[POJ 1390] Blocking
查看>>
Python中数据类型
查看>>
JS常用的设计模式(17)—— 状态模式
查看>>
完全背包
查看>>
jmeter分布式压测
查看>>
android listview 动态布局 网格显示数据
查看>>
nodejs项目总结
查看>>
Windows Server 8 开发预览版
查看>>
使用微软的TFS云服务
查看>>
Minikube之Win10单机部署
查看>>
Nginx配置文件分析
查看>>
大数据测试之hadoop命令大全
查看>>
高可用的MongoDB集群
查看>>