1)ModelAndView

@RequestMapping(value="/itemEdit")
public ModelAndView itemEdit(){
//创建模型视图对象
ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("username", "张三");//指定页面返回的数据
modelAndView.setViewName("test");//设置返回的视图名称 return modelAndView;
}

2)String(推荐使用)

1) 返回普通字符串,就是页面去掉扩展名的名称, 返回给页面数据通过Model来完成

@RequestMapping(value="/test1")
public String test1(Model model){

//添加数据
model.addAttribute("username", "李四");
model.addAttribute("address", "福州晋安区");
return "test";
}

2) forward: 请求转发,存入到model中的数据, 转发的方法响应的页面可以直接取出${username}--${address}

@RequestMapping(value="/test2")
public String test2(Model model){
//添加数据,请求转发携带的数据
model.addAttribute("username", "李四");
model.addAttribute("address", "福州晋安区"); //请求转发到另一个方法
return "forward:index";//相对路径
//return "forward:/test/index"; //绝对路径,"/"代表从项目名开始
} @RequestMapping(value="/index")
public String index(){
return "test";
}

页面

<body>
<!--页面显示: 李四--福州晋安区-->
${username}--${address}
</body>

3) redirect: 重定向

@RequestMapping("/testRedirect")
public String testRedirect(Model model){ //添加数据
model.addAttribute("username", "jack");
model.addAttribute("address", "美国纽约"); //重定向
return "redirect:index";
}

//http://localhost:8080/crm0618/test/index?username=jack&address=美国纽约 @RequestMapping(value="/index")
public String index(HttpServletRequest request) throws UnsupportedEncodingException{
String username = request.getParameter("username");
String address = new String(request.getParameter("address").getBytes("iso8859-1"), "utf-8"); return "test";
}

3)返回void(使用它破坏了springMvc的结构,所以不建议使用)

可以使用request.setAttribut 来给页面返回数据
可以使用request.getRquestDispatcher().forward()来指定返回的页面
如果controller返回值为void则不走springMvc的组件,所以要写页面的完整路径名称

@RequestMapping("/testRequest")
public void testRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//将数据存入到request域中
request.setAttribute("username", "make");
request.setAttribute("address", "芝加哥"); request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request, response);;
}

SpringMVC中 controller方法返回值的更多相关文章

  1. SpringMVC中Controller的返回值类型

    Controller方法的返回值可以有以下几种: 1.返回ModelAndView 返回ModelAndView时最常见的一种返回结果.需要在方法结束的时候定义一个ModelAndView对象,并对M ...

  2. Spring MVC中 controller方法返回值

    1.返回ModelAndView 定义ModelAndView对象并返回,对象中可添加model数据.指定view 2.返回String 1.表示返回逻辑视图名 model对象通过 model.add ...

  3. SpringMVC的@RequestMapping和Controller方法返回值

    本节内容: @RequestMapping Controller方法返回值 一.@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映 ...

  4. SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器

    一.参数的传递 1.简单的参数传递 /* @RequestParam用法:入参名字与方法名参数名不一致时使用{ * value:传入的参数名,required:是否必填,defaultValue:默认 ...

  5. springmvc中Controller方法的返回值

    1.1 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 1.2 返回void 在controller方法形参 ...

  6. SpringMVC的Controller的返回值与接收的参数

    内容参考自博客: http://blog.csdn.net/u011001084/article/details/52846791 http://blog.csdn.net/xuxiaoyinliu/ ...

  7. SprimgMVC学习笔记(五)—— Controller方法返回值

    一.返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. /** * 查询商品列表 * @return */ @R ...

  8. Controller方法返回值

    1. 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. //入门程序 第一 包类 + 类包 + 方法名 @Re ...

  9. Asp.net mvc中Controller的返回值

    (1)EmptyResult:当用户有误操作或者是图片防盗链的时候,这个EmptyResult就可以派上用场,返回它可以让用户啥也看不到内容,通过访问浏览器端的源代码,发现是一个空内容: public ...

随机推荐

  1. 每日算法 - day 15

    每日算法 those times when you get up early and you work hard; those times when you stay up late and you ...

  2. Iterator作用

    前言 下面的内容是我从百度知道拷贝出来的,也就不在贴出链接了.我总结下就是迭代器在集合中使用,用户不需要关心具体集合实现的是如何遍历(不暴露细节),按照迭代器的方式遍历. 作用 Iterator模式是 ...

  3. uni app中使用自定义图标库

    项目中难免会用到自定义图标,那在uni app中应该怎么使用呢? 首先, 将图标目录放在static资源目录下: 在main.js中引入就可以全局使用了 import '@/static/icon-o ...

  4. eclipse不能启动,An internal error occurred during: "reload maven project".

    An internal error occurred during: "reload maven project". 这个错误是因为项目已经关闭,导致 导致此问题的原因是Sprin ...

  5. Day2-E-Catch That Cow-POJ3278

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  6. Day3:关于地形生成

    ---恢复内容开始--- 今天桃子好像还是没什么动静,不过媳妇倒是有一点见红~ 希望这是马上要出来的前兆了~ 桃子都已经晃点我俩好多回了~ 已经都快习惯来她这个狼来了的征兆了~ ----------- ...

  7. jdk1.7推出的Fork/Join提高业务代码处理性能

    jdk1.7推出的Fork/Join提高业务代码处理性能 jdk1.7之后推出了Fork/Join框架,其原理个人理解为:递归多线程并发处理业务代码,以下为我模拟我们公司业务代码做的一个案例,性能可提 ...

  8. 设置zabbix (3.4.2)添加监控项,触发器,让CPU使用超过85%就报警:

    zabbix (3.4.2)添加监控项,触发器,让CPU使用超过85%就报警: zabbix自带模板有一个 Template OS Linux模板.这个模板有监控CPU的监控项,如果没有添加一个监控项 ...

  9. P1093 字符串A+B

    1093 字符串A+B (20分)   给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出  ...

  10. C++编程学习(十二) STL

    一.简介 标准模板库STL,是一组模板类和函数.提供了: 1.容器.用于存储信息. 2.迭代器.用于访问容器中的信息. 3.算法.操作容器内容. 1.容器 STL有两种类型的容器类: (1)顺序容器 ...