JAVA 框架 springmvc controller的返回值
一、返回值: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的返回值的更多相关文章
- springmvc controller方法返回值
- SpringMVC的@RequestMapping和Controller方法返回值
本节内容: @RequestMapping Controller方法返回值 一.@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映 ...
- SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器
一.参数的传递 1.简单的参数传递 /* @RequestParam用法:入参名字与方法名参数名不一致时使用{ * value:传入的参数名,required:是否必填,defaultValue:默认 ...
- SpringMVC的Controller的返回值与接收的参数
内容参考自博客: http://blog.csdn.net/u011001084/article/details/52846791 http://blog.csdn.net/xuxiaoyinliu/ ...
- Web API-如何将Controller的返回值转换成HTTP response消息
https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization https://co ...
- Java的Object.hashCode()的返回值到底是不是对象内存地址?
关于这个问题,查阅了网上的资料,发现证明过程太繁琐,这里我用了反证法. java.lang.Object.hashCode()的返回值到底是不是对象内存地址? hashCode契约 说到这个问题,大家 ...
- SpringMVC学习笔记三:Controller的返回值
springMVC的返回值有ModelAndView,String,void,Object类型 项目目录树: 该项目是在前面项目的基础上修改的,这里的pom.xml文件需要加入使用到的包,应为@Res ...
- SpringMVC中 controller方法返回值
1)ModelAndView @RequestMapping(value="/itemEdit") public ModelAndView itemEdit(){ //创建模型视图 ...
- SpringMVC中Controller的返回值类型
Controller方法的返回值可以有以下几种: 1.返回ModelAndView 返回ModelAndView时最常见的一种返回结果.需要在方法结束的时候定义一个ModelAndView对象,并对M ...
随机推荐
- POJ1661(KB12-M DP)
Help Jimmy Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无 ...
- inline-block元素间隙处理
要使多个块级元素并行显示,可使用float或者inline-block进行处理 使用inline-block会出现元素之间的间隙 <div class="demo"> ...
- node 静态伺服(搭建服务)
基本功能 不急着写下第一行代码,而是先梳理一下就基本功能而言有哪些步骤. 在本地根据指定端口启动一个http server,等待着来自客户端的请求 当请求抵达时,根据请求的url,以设置的静态文件目录 ...
- 【代码笔记】iOS-字体从右向左滚动
一,效果图. 二,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup ...
- Zookeeper简单初应用
一.Zookeeper 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. Zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务 ...
- java发起HTTP请求的共用类
一定要注意编码,请求时,content-type里的编码,仅仅是流的编码,而结果的编码类型,则是流转化为字符串是需要设定的. 以下是3种使用get/post的方式: import java.io.Bu ...
- ViewPager中切换界面Fragment被销毁的问题
ViewPager中切换界面Fragment被销毁的问题分析 使用ViewPager+Fragment实现界面切换,当界面数量大于3时,出现二次滑动后数据消失的情况,下面由Fragment生命周期进行 ...
- Oracle EBS OPM 取消生产批
--取消生产批 --created by jenrry SET serveroutput on; DECLARE p_batch_header_rec gme_batch_header%ROWTYPE ...
- [Visual Studio] [Config] [Transformation] [SlowCheetah] 在非Web工程中使用Transformation
1. 为VS安装SlowCheetah插件 https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.Sl ...
- css基础内容
css基础内容 CSS 指层叠样式表 (Cascading Style Sheets)样式定义如何显示 HTML 元素样式通常存储在样式表中把样式添加到 HTML 4.0 中,是为了解决内容与表现分离 ...