SpringMVC页面向Controller传参
关于SpringMVC页面向Controller传参的问题,看了网上不少帖子,大多总结为以下几类:
1、直接把页面表单中相关元素的name属性对应的值作为Controller方法中的形参。
这个应该是最直接的,我看的那本书从百度的编辑器中取内容content时就直接用的这个方法:
<!--页面-->
<form action="<%=basePath%>saveUeditorContent" method="post">
<!-- 加载编辑器的容器 -->
<div style="padding: 0px;margin: 0px;width: 100%;height: 100%;" >
<script id="container" name="content" type="text/plain">
</script> <!--why dose this use script tag here???-->
</div>
<input name="test_input" value="hengha">
<button type="submit"> 保存</button>
</form>
//Controller
@RequestMapping(value="/saveUeditorContent")
public ModelAndView saveUeditor(String content, String test_input){
ModelAndView mav = new ModelAndView("myJSP/test03");
//addObject方法设置了要传递给视图的对象
mav.addObject("content", content);
mav.addObject("input_content", test_input);
//返回ModelAndView对象会跳转至对应的视图文件。也将设置的参数同时传递至视图
return mav;
}
2、通过@RequestParam把页面表单中相关元素的name属性对应的值绑定Controller方法中的形参。
用于URL带?场景,/saveUeditorContent?content=123&input_content=456,参数可以设置是否必须required,默认值defaultvalue
@RequestMapping(value="/saveUeditorContent")
public ModelAndView saveUeditor(@RequestParam(value="content",required=true,defaultValue="123") String content, @RequestParam("test_input") String input_content){
ModelAndView mav = new ModelAndView("myJSP/test03");
mav.addObject("content", content);
mav.addObject("input_content", input_content);
return mav;
}
3、通过@PathVariable获取@RequestMapping中URL路径带入的{变量}绑定Controller方法中的形参。
用于URL直接传参场景,/saveUeditorContent/123/456
@RequestMapping(value="/saveUeditorContent/{content}/{test_input}")
public ModelAndView saveUeditor(@PathVariable("content") String content, @PathVariable("test_input") String input_content){
ModelAndView mav = new ModelAndView("myJSP/test03");
mav.addObject("content", content);
mav.addObject("input_content", input_content);
return mav;
}
4、创建属性名对应页面表单中相关元素带setter和getter方法的POJO对象作为Controller方法中的形参。
//太晚了不是特别熟不整了
5、把HttpServletRequest对象作为Controller方法中的形参。
@RequestMapping(value="/saveUeditorContent")
public ModelAndView saveUeditor(HttpServletRequest request){
ModelAndView mav = new ModelAndView("myJSP/test03");
mav.addObject("content", request.getParameter("content"));
mav.addObject("input_content", request.getParameter("test_input"));
return mav;
}
约束说明:
a) 1中的形参,2中的RequestParam("参数"),3中的URL中的{参数}以及PathVariable("参数"),4中的POJO对象属性,5中的request.getParameter("参数")均需要和前台页面中相关元素name属性对应的值匹配。
b) 2中的RequestParam("参数"),3中的PathVariable("参数")绑定到后面跟的形参,后台在处理时根据实际需要可以改变参数名称。
SpringMVC页面向Controller传参的更多相关文章
- struts2 页面向Action传参方式
1.基本属性注入 我们可以直接将表单数据项传递给Action,而Action只需要提供基本的属性来接收参数即可,这种传参方式称为基本属性注入.例如 jsp页面: <s:form method=& ...
- springmvc jsp向controller传参,一直为null
怎么检查都无解 重启电脑好了
- angularjs不同页面间controller传参方式,使用service封装sessionStorage
这里分享一个我在实际项目中,使用service封装的一个依赖sessionStorage的传参服务. 这里先说下大背景,在我们的实际开发中,登陆之后一般会存在一个token,这个token将会贯穿全场 ...
- JS form跳转到新标签页并用post传参
通过js实现跳转到一个新的标签页,并且传递参数.(使用post传参方式) 1 超链接<a>标签 (get传参) <a href="http://www.cnblogs. ...
- jnhs-SpringMVC jsp页面向controller传递参数的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- C# 页面向controller中跳转匹配方法的时候,当controller中有两个重载方法时候,不发生跳转
在ajax中的URL跳向controller一个方法时候,controller中有两个重载的方法,ajax不发生跳转,当删除另外一个方法之后,正常跳转. 不知道,是我自己写的有问题,还是control ...
- springboot controller传参,对象映射
Post请求,对象映射时,在参数 加 @RequestBody: 传入对象内字段的json才能映射 {"legendData": [100,90,80,70,60,50,40,30 ...
- angular 跳转页面时传参
首先,你需要已经配置过你的rout,比如: $stateProvider .state('firstPage',{ url:'/Page/firstPage', templateUrl: 'Page/ ...
- Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块
路由跳转 三种方式: $router.push / $router.go / router-link to this.$router.push('/course'); this.$router.pus ...
随机推荐
- 权限系统设计-day01
数据库表的设计: 关键流程思考: 权限在SSH系统中应该表现为什么东西? 小胖这个用户登陆:1,检查用户名和密码;2,检查通过; 1),得到小胖这个用户的对应的所有的角色:R1 2),根据所有的角 ...
- victory-native的使用
Victory用于构建交互数据可视化的可组合React组件的生态系统 想写又不想写,真尴尬...
- .NET Core 微服务架构 Steeltoe 使用(基于 Spring Cloud)
阅读目录: 1. Spring Cloud Eureka 注册服务及调用 2. Spring Cloud Hystrix 断路器 3. Spring Cloud Hystrix 指标监控 4. Spr ...
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...
- 依赖注入[2]: 基于IoC的设计模式
正如我们在<控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC自身不仅与面向对象没有必然的联系,它也算不上是一种设计模式.一般来讲,设计模 ...
- SEO需要掌握的基础知识
什么是SEO? 官方解释: SEO是指通过对网站内部调整优化及站外优化,使网站满足搜索引擎收录排名需求,在搜索引擎中提高关键词排名, 从而把精准用户带到网站,获得免费流量,产生直接销售或品牌推广 ...
- “百度杯”CTF比赛(二月场)-web-writeup
爆破一: 打开网页看到源代码: 根据提示这题就是找变量的值,本想爆破,但不太现实.百度 php获取变量的值 有个超全局数组 $GLOBALS 爆破二: 打开网页看到源代码: 看到了eval() 函数, ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- 『最小表示法 Necklace』
最小表示法 这是一个简单的字符串算法,其解决的问题如下: 给定一个字符串\(S\),长度为\(n\),如果把它的最后一个字符不断放到最前面,会得到\(n\)个不同的字符串,那么我们称这\(n\)个字符 ...
- Python合并多个Excel数据
安装模块 1.找到对应的模块 http://www.python-excel.org/ 2.用pip install 安装 pip install xlrdpip install XlsxWrite ...