Spring MVC3在controller和视图之间传递参数的方法:

 
一, 从controller往视图传递值, controller---->视图
 
1)简单类型,如int, String,直接写在controller方法的参数里,是无法传递到视图页面上的(经测试)。
 
(而用@RequestParam("name")注解,可以从视图上,通过url的方式?name=***传递到controller方法里)
 
2)可以用Map<String, Object>,其键值可以在页面上用EL表达式${键值名}得到,
 
3)也可以用Model类对象来传递,有addAttribute(key, value)方法,其键值可以在页面上用EL表达式${键值名}得到,
 
如果用addAttribute(value)这个方法,会将类型名的首字母改成小写后,作为键值名传递过去,例如"ok"在页面上用${string}得到,而一个复合类对象,如User类对象,页面上用${user}得到该对象,用${user.propertyName}得到其属性,这是用Model的一大优势。
例如,model.addAttribute(new User("my姓名","我的爱好有游泳打球"));
这样页面上就能用${user.name}和${user.hobby}打印对应属性
@RequestMapping(value={"/","/hello"})
public String hello(int id,Map<String,Object> map) {
System.out.println(id);
System.out.println("hello");
map.put("hello", "world");
return "hello";
}
@RequestMapping(value="/say")
public String say(@RequestParam int id,Model model) {
System.out.println("say");
model.addAttribute("hello", "value");
//使用Object的类型作为key,String-->string
model.addAttribute("ok");
return "hello";
}
 
二,从视图向controller传递值,  controller <--- 视图
 
1)简单类型,如int, String, 应在变量名前加@RequestParam注解,
例如:
@RequestMapping("hello3")
public String hello3( @RequestParam("name" ) String name,
@RequestParam("hobby" ) String hobby){
System. out.println("name=" +name);
System. out.println("hobby=" +hobby);
return "hello" ;
}
但这样就要求输入里面必须有这两个参数了,可以用required=false来取消,例如:
@RequestParam(value="name",required=false) String name
但经测试也可以完全不写这些注解,即方法的参数写String name,效果与上面相同。
 
2)对象类型:
@RequestMapping("/hello4" )
public String hello4(User user){
System.out.println("user.getName()=" +user.getName());
System.out.println("user.getHobby()=" +user.getHobby());
return "hello";
}
Spring MVC会按: “HTTP请求参数名 =  命令/表单对象的属性名”的规则自动绑定请求数据,支持“级联属性名”,自动进行基本类型数据转换。
即有一个User类,如下
public class User {
private String name ;
private String hobby ;
public User(){ }
public User(String name, String hobby) {
this.name = name;
this.hobby = hobby;
}
//...get/set方法略

则页面上可以用

<form name="form1" action="hello4" method="post">
<input type="text" name="name"/>
<input type="text" name="hobby"/>
...

提交后,把值直接绑定到user对象上。

此外,还可以限定提交方法为POST,即修改方法的@RequestMapping注解为
@RequestMapping(value="/hello4",method=RequestMethod.POST)
最后,注意,如果这里提交过来的字符出现乱码,应该在web.xml里加入如下filter:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name >
<url-pattern>/*</url-pattern>
</filter-mapping>

内容转载自:http://blog.csdn.net/yanqlv/article/details/7655645

[转]SpringMVC Controller&View数据传递的更多相关文章

  1. Controller与View数据传递 多Model传递

    1)ViewBag变量方式 使用4个ViewBag变量进行数据传递,Data1.Data2.Data3.Data4的数据直接从数据库里调. Control中伪代码如下所示: 1 public Acti ...

  2. springMVC:将controller中数据传递到jsp页面

    1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...

  3. ASP.NET controller TO view 数据传递

    https://stackify.com/viewbag/ In the case of ASP.NET MVC, you have three ways to pass data from the ...

  4. SpringMVC之json数据传递

    json是一种常见的传递格式,是一种键值对应的格式.并且数据大小会比较小,方便传递.所以在开发中经常会用到json. 首先看一下json的格式: {key1:value1,key2:value2} 每 ...

  5. springmvc中的数据传递

    import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; impo ...

  6. SpringMVC Controller 接收页面传递的中文参数出现乱码

    在Controller中接收到的POST参数如果是中文的话,显示为乱码.已知客户端传过来时编码为UTF-8. 问题产生分析: spring MVC中默认的编码格式为“ISO-8859-1”,因此造成乱 ...

  7. ASP.NET MVC3中Controller与View之间的数据传递总结

    一.  Controller向View传递数据 1.       使用ViewData传递数据 我们在Controller中定义如下: ViewData["Message_ViewData& ...

  8. ASP.NET MVC3中Controller与View之间的数据传递

    在ASP.NET MVC中,经常会在Controller与View之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨: 一.  Controller向Vie ...

  9. Asp.net MVC中 Controller 与 View之间的数据传递

    在ASP.NET MVC中,经常会在Controller与View之间传递数据 1.Controller向View中传递数据 (1)使用ViewData["user"] (2)使用 ...

随机推荐

  1. spring 配置定时任务

    spring的定时任务配置分为三个步骤:1.定义任务2.任务执行策略配置3.启动任务1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob ...

  2. python类的特性

    #encoding=utf-8 class Province: #静态字段 memo = '这里是静态变量' def __init__(self,name,capital,leader,flag): ...

  3. Java反序列化漏洞执行命令回显实现及Exploit下载

    原文地址:http://www.freebuf.com/tools/88908.html 本文原创作者:rebeyond 文中提及的部分技术.工具可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使 ...

  4. memset与malloc性能测试

    memset与malloc性能测试 测试环境:2.2GHZ.2G内存 memset一段大小为1K的buf,每秒有1200万次:10K的buf,每秒有260万次:100K的buf,每秒有13万次. ma ...

  5. 2014中国软件开发者调查(一):Java最受欢迎 第二语言JS使用比例最高

    2014 年 3 月 20 日到 4 月 25 日期间,CSDN 通过在线问卷渠道进行了中国软件开发者调查,本次调查问卷得到了近万名开发者踊跃支持.日前这份调查报告已经出炉,CSDN 将就调查结果连续 ...

  6. 安装redis监控

    在修改登录中心的时候,数据存储在redis里面,需要对redis进行监控,使用的是Redis-Live 参考文章: http://www.nkrode.com/article/real-time-da ...

  7. count有关

    1.count有两个作用:统计某个字段有值的记录数:统计结果集的记录数.2.count括号内的表达式不为null,就是统计结果集的记录数.也就是说,count(1),count(*),count(10 ...

  8. web项目中加入struts2、spring的支持,并整合两者

    Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...

  9. arcgis flexviewer中由Application向widget传值

    arcgis flexviewer所有的小部件类均继承自com.esri.viewer.BaseWidget基类,而BaseWidget又继承了com.esri.viewer.IBaseWidget接 ...

  10. Java 部分排序算法

    . import java.io.*;import java.math.*;import java.util.*;public class Algr{ public static int array[ ...