一、返回值:ModleView对象。

使用modelAndView.setViewName设置返回的页面。使用modelAndView.addObject设置返回的数据。

     @RequestMapping("/edit")
public ModelAndView editTable(HttpServletRequest request){
ModelAndView modelAndView=new ModelAndView();
String id=request.getParameter("id");
goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
modelAndView.setViewName("edit");
modelAndView.addObject("goods",goodser);
return modelAndView;
}

二、返回值:字符串

字符串为页面名称除去扩展名

     @RequestMapping("/edit")
public String editTable(HttpServletRequest request,Model model){
String id=request.getParameter("id");
goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
model.addAttribute("goods",goodser);
return "edit";
}

三、返回值:void

通过参数httpservletrequest来确定设置返回值,通过request 设置值和转发。

     @RequestMapping("/edit")
public void editTable(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
goods goodser= this.goodsService.findByIdSer(Integer.parseInt(id));
request.setAttribute("gooods",goodser);
request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request,response);
}

四:转发

通过return  forward:*.action 方式进行转发,通过Model设置数据。绝对路径是带有/

     @RequestMapping("/update")
public String upddateById(goods goods,Model model){
this.goodsService.updateById(goods);
model.addAttribute("id",goods.getId());
return "forward:edit.action";
}

绝对路径写法:根据需求来适用绝对路径和相对路径。

    @RequestMapping("/update")
public String upddateById(goods goods,Model model){
this.goodsService.updateById(goods);
model.addAttribute("id",goods.getId());
return "redirect:/goods/edit.action";
}

注意这里需要带上扩展名.action

url没发生变化。

五、重定向:

通过return "redirect:*.action"  通过Model设置数据。绝对路径是带有/。

     @RequestMapping("/update")
public String upddateById(goods goods,Model model){
this.goodsService.updateById(goods);
model.addAttribute("id",goods.getId());
return "redirect:edit.action";
}

url发生变化。

Modle 底层也是通过request.setAttribute来设置属性,但是对request上做了更好的封装,无论是转发还是重定向都可以设置值。

JAVA 框架 springmvc controller的返回值的更多相关文章

  1. springmvc controller方法返回值

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

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

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

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

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

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

  5. Web API-如何将Controller的返回值转换成HTTP response消息

    https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...

  6. Java的Object.hashCode()的返回值到底是不是对象内存地址?

    关于这个问题,查阅了网上的资料,发现证明过程太繁琐,这里我用了反证法. java.lang.Object.hashCode()的返回值到底是不是对象内存地址? hashCode契约 说到这个问题,大家 ...

  7. SpringMVC学习笔记三:Controller的返回值

    springMVC的返回值有ModelAndView,String,void,Object类型 项目目录树: 该项目是在前面项目的基础上修改的,这里的pom.xml文件需要加入使用到的包,应为@Res ...

  8. SpringMVC中 controller方法返回值

    1)ModelAndView @RequestMapping(value="/itemEdit") public ModelAndView itemEdit(){ //创建模型视图 ...

  9. SpringMVC中Controller的返回值类型

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

随机推荐

  1. 漫画 | Java多线程与并发(二)

    1.什么是线程池? 为什么要使用它? 2.Java中invokeAndWait 和 invokeLater有什么区别? 3.多线程中的忙循环是什么? 4.Java内存模型是什么? 线程内的代码能够按先 ...

  2. python基础训练题2-元组,字典

    1,判断值在元组中 >>> a = ( 1, 2, 3, 4, 10 ) >>> 10 in a True >>> ' in a False 2, ...

  3. php两个多维数组组合遍历

    $res = $this->LoanRecord->searchloan($conditions,$columns,$page,$this->num,$user_id); forea ...

  4. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  5. Onsen UI for React文档

    注:采用ES6+JSX语法 1.开始一个项目 在React中使用Onsen UI 需要首先安装onsenui和react-onsenui模块. 可以使用monaca CLI工具包快速初始化一个应用: ...

  6. 【VS2015】Win7 X64上面安装VS2015

    环境: 1.Win7 x64 SP1旗舰版 2.VS2015专业版Update3 3.IE11 4.WDK10 5.SDK10   安装步骤: 1.安装IE11,需要如下补丁:     a.Windo ...

  7. ActiveReports 报表控件V12新特性 -- RPX报表转换为RDL报表

    ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...

  8. springcloud 入门 2 (Enreka的服务和注册)

    spring cloud eureka: eureka 用以服务发现.服务注册,比较流行的有consul(后面再介绍) eureka介绍: eureka为netflix开源软件,分为三个部分: eur ...

  9. 【three.js练习程序】创建简单物理地形

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. NXlog配置

    NXlog文档: http://nxlog.org/docs/nxlog-ce/nxlog-reference-manual.html https://nxlog.org/documentation/ ...