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 ...
随机推荐
- linux系统编程:read,write与lseek的综合应用
这个实例根据命令行参数进行相应的读学操作: 用法: usage:./io file {r<length>|R<length>|w<string>|s<offs ...
- Linux 安装Python虚拟环境,virtualenvwrapper
virtualenvwrapper工具学习 1.因为virtualenv 工具使用的并不方便 2.安装virtualenvwrapper pip3 install -i https://pypi.do ...
- 原生css 中变量的使用
前两天看到阮大神的一篇在css中使用变量的文章,整理了一下. 这个重要的 CSS 新功能,所有主要浏览器已经都支持了.本文全面介绍如何使用它,你会发现原生 CSS 从此变得异常强大. 一.变量的声明 ...
- 解决The type 'ASP.global_asax' exists in both ASP.global_asax同时存在问题
习惯发布站点的同学有时候可能遇见以下错误 这是发布时[预编译勾选/不勾选]产生的文件冲突导致的 如果不勾选预编译会发布以下代码 如果勾选预编译会发布以下代码 错误就在于此,如果非预编译Global.a ...
- apt-get update 系列作用
sudo apt-get update 更新源 sudo apt-get upgrade 更新已安装的包 sudo apt-get dist-upgrade 升级系统 下面摘自知乎用户回答: apt- ...
- 学习MVC之租房网站(十二)-缓存和静态页面
在上一篇<学习MVC之租房网站(十一)-定时任务和云存储>学习了Quartz的使用.发邮件,并将通过UEditor上传的图片保存到云存储.在项目的最后,再学习优化网站性能的一些技术:缓存和 ...
- art-template模板应用
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 从零自学Java-4.使用字符串来交流
1.使用字符串来存储文本: 2.在程序中显示字符串: 3.在字符串中包含特殊的字符: 4.拼接字符串: 5.在字符串中包含变量: 6.比较字符串: 7.判断字符串的长度: 程序Credits:显示一部 ...
- sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录 FATAL ERROR: Neither host 'kvm' nor 'localhost' could be looked up with ./bin/resolveip Please configure the 'hostname'
初始化数据库报错: sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录FATAL ERROR ...
- 使用代码段遍历,枚举类型Enum
最近项目中定义了一些枚举类型,需要将枚举的键值传给前端,用于制作下拉菜单. 1.首先定义了枚举类型 public enum 请假类型 : int { 病假 = 1, 事假 = 2, 婚假 = 3, 产 ...