关于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传参的更多相关文章

  1. struts2 页面向Action传参方式

    1.基本属性注入 我们可以直接将表单数据项传递给Action,而Action只需要提供基本的属性来接收参数即可,这种传参方式称为基本属性注入.例如 jsp页面: <s:form method=& ...

  2. springmvc jsp向controller传参,一直为null

    怎么检查都无解 重启电脑好了

  3. angularjs不同页面间controller传参方式,使用service封装sessionStorage

    这里分享一个我在实际项目中,使用service封装的一个依赖sessionStorage的传参服务. 这里先说下大背景,在我们的实际开发中,登陆之后一般会存在一个token,这个token将会贯穿全场 ...

  4. JS form跳转到新标签页并用post传参

    通过js实现跳转到一个新的标签页,并且传递参数.(使用post传参方式) 1 超链接<a>标签  (get传参)  <a href="http://www.cnblogs. ...

  5. jnhs-SpringMVC jsp页面向controller传递参数的五种方式

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  6. C# 页面向controller中跳转匹配方法的时候,当controller中有两个重载方法时候,不发生跳转

    在ajax中的URL跳向controller一个方法时候,controller中有两个重载的方法,ajax不发生跳转,当删除另外一个方法之后,正常跳转. 不知道,是我自己写的有问题,还是control ...

  7. springboot controller传参,对象映射

    Post请求,对象映射时,在参数 加 @RequestBody: 传入对象内字段的json才能映射 {"legendData": [100,90,80,70,60,50,40,30 ...

  8. angular 跳转页面时传参

    首先,你需要已经配置过你的rout,比如: $stateProvider .state('firstPage',{ url:'/Page/firstPage', templateUrl: 'Page/ ...

  9. Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块

    路由跳转 三种方式: $router.push / $router.go / router-link to this.$router.push('/course'); this.$router.pus ...

随机推荐

  1. oracle的知识点总结

    体系结构:数据库的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制.体系结构包括:实例(instence),数据库文件(database),用户进程(user proces ...

  2. 上传文件格式,及headers设置

    file[]:(binary)文件格式,传过去的参数自然是query string parameters  形式,当然也有纯的formData格式 formData格式就是将所有的参数append到p ...

  3. js-day06-jQuery事件和DOM操作-练习题

    jQuery事件绑定 js中绑定事件,三种方式: 方式1: 直接在元素上,增加onXxx事件属性. <button onclick="alert(1);">点我< ...

  4. 使用RSA加密在Python中逆向shell

    i春秋翻译小组-Neo(李皓伟) 使用RSA加密在Python中逆向shell 这是一个关于使用RSA加密编程逆向shell的python教程. 我想提一下,这篇文章更多的是关于理解shell中涉及的 ...

  5. Java线程和进程相关面试题与答案总结

    有几天没有写一写博客了,今天就带给大家一些面试题和参考答案吧! 这些都是上海尚学堂Java培训的学员去面试时遇到的问题,今天总结出来的是Java线程相关类的面试题.把参考答案和解析也发布出来,供大家学 ...

  6. [Swift]LeetCode638. 大礼包 | Shopping Offers

    In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, there are ...

  7. [Swift]LeetCode764. 最大加号标志 | Largest Plus Sign

    In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...

  8. Identity Server 4 中文文档(v1.0.0) 目录

    欢迎来到IdentityServer4 第一部分 简介 第1章 背景 第2章 术语 第3章 支持和规范 第4章 打包和构建 第5章 支持和咨询选项 第6章 演示服务器和测试 第7章 贡献 第二部分 快 ...

  9. postgresql 删除库的时候报错database "temp_test_yang" is being accessed by other users

    删除库的时候报错 ERROR: database "temp_test_yang" is being accessed by other usersDETAIL: There ar ...

  10. Python Django(WEB电商项目构建)

    (坚持每一天,就是成功) Python Django Web框架,Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,即模型M,模板T和视图V组成. 安装Pyth ...