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 ...
随机推荐
- 初识DOM
初识DOM 一. 节点的获取.插入.创建.删除.替换.修改文本节点 获取 1.document.getElementById 2.(element)document.getElementsByTagN ...
- java的XML解析(DOM4J技术)
DOM4J技术解析XML文件 一,XML简介 xml (可扩展标记语言) 全称: Extended Markup Language 可扩展的含义:允许程序员按照自己的想法去扩展新的标签 注意:但是扩 ...
- vue 笔记
<div id="root"> <div> <input v-model="inputValue" /> <butto ...
- Linux打包、压缩与解压详解
介绍:在Windows下最常见的压缩文件就只有两种,另一个是.rar,它有.gz..tar.gz.tgz.bz2..Z..tar等众多的压缩文件名,本文就来对这些常见的压缩文件进行总结,在具体总结各类 ...
- eclipse中svn插件的工程不能与svn资源库同步的解决方法
eclipse中svn插件的工程不能与svn资源库同步的解决办法 最近几天自己的工程与资源库同步总是出现问题,重启机器后发现资源库丢失了,无法进行同步. 解决办法如下: 1.右键工程---->选 ...
- C#安全加密类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- scott/tiger is locked 解决办法
在plsql developer中要是以scott/tiger登录时提示ora-28000 the account is locked. 解决办法: 新装完Oracle10g后,用scott/tige ...
- 如何加密 Windows VM 上的虚拟磁盘
为了增强虚拟机 (VM) 的安全性以及符合性,可以加密 Azure 中的虚拟磁盘. 磁盘是使用 Azure 密钥保管库中受保护的加密密钥加密的. 可以控制这些加密密钥,以及审核对它们的使用. 本文详细 ...
- 8086CPU的出栈(pop)和入栈(push) 都是以字为单位进行的
8086CPU的出栈(pop)和入栈(push) 都是以字为单位进行的
- Finding the source of signals on Linux with strace, auditd, or systemtap
inux and UNIX® like operating systems commonly use signals to communicate between processes. The use ...