一、返回值: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. UI-12组结对编程作业总结

    UI-12组结对编程作业总结 源码Github地址 https://github.com/tilmto/TILMTO/tree/master/Arithmetic 作业摘要 本次结对编程作业分为以下两 ...

  2. python-策略模式

    源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 策略指的就是为了达到某一目的而采取的手段或者方法.为了实现软件设计咪表,对象可能 ...

  3. SpringMVC注解集合

    @RequestMapper注解 绑定请求路径与处理方法例如: @RequestMapping("login.do") public String showLogin() { .. ...

  4. 葡萄城报表介绍:矩表(Tablix)

    一.矩表(Tablix)定义 Tablix 即表(Table)+矩阵(Matrix)+列表(List),它是报表中展示数据的一种工具,由表.矩阵.列表这三个具有相似性的报表项组成. 二.矩表(Tabl ...

  5. DrawerLayout建立侧滑时,显示侧滑页面,底层页面仍可以有点击响应,解决办法。

    第一感觉是下层仍有焦点,解决办法应该是侧方页面出现后,下层页面的焦点改为false,应该是动态去改变焦点的状态,但是不知道如何去实现. 然后再网上找到实现方法,感谢:http://blog.csdn. ...

  6. 安卓基础之Activity的生命周期

    Activity的生命周期 onCreate 在Activity被创建时调用 onDesdroty 在Activity销毁时调用 onRestart 在Activity重新打开时调用 onStart ...

  7. Hive创建指向HBase表的表

    create [external] table t1(id int, value string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorag ...

  8. 耐克的定制页用canvas如何实现....跪求前端大神指点。

    选择鞋子的鞋底 鞋底会变色,也可以添加自己定制的id,这个东西看的是用canvas做的,但是小弟确实不知道怎么去做,求大神指点一二,不胜感激! nike的定制页地址:http://store.nike ...

  9. 学习git踩坑之路

    第一次使用git: 1.使用CentOS Linux release 7.3.1611 (Core),内核版本3.10.0-514.el7.x86_64 2.安装git: root权限运行:yum   ...

  10. hibernate数据库操作基础

    1.根据主键查询 2.getSession().createSQLQuery(sql)和session.createQuery(sql) 3.Seeion的其他方法  4.Hibernate Crit ...