springMVC中controller的几种返回类型
==网文1,还不错,感觉比较老旧
springMVC中controller的几种返回类型 - CSDN博客
http://blog.csdn.net/qq_16071145/article/details/51222372 Controller方法的返回值可以有以下几种:
1、返回ModelAndView
返回ModelAndView时最常见的一种返回结果。需要在方法结束的时候定义一个ModelAndView对象,并对Model和View分别进行设置。
2、返回String
1):字符串代表逻辑视图名
真实的访问路径=“前缀”+逻辑视图名+“后缀”
注意:如果返回的String代表逻辑视图名的话,那么Model的返回方式如下:
public String testController(Model model){
model.addAttribute(attrName,attrValue);//相当于ModelAndView的addObject方法
return "逻辑视图名";
}
2):代表redirect重定向
redirect的特点和servlet一样,使用redirect进行重定向那么地址栏中的URL会发生变化,同时不会携带上一次的request
案例:
public String testController(Model model){
return "redirect:path";//path代表重定向的地址
}
3):代表forward转发
通过forward进行转发,地址栏中的URL不会发生改变,同时会将上一次的request携带到写一次请求中去
案例:
public String testController(Model model){
return "forward:path";//path代表转发的地址
}
3、返回void
返回这种结果的时候可以在Controller方法的形参中定义HTTPServletRequest和HTTPServletResponse对象进行请求的接收和响应
1)使用request转发页面
request.getRequestDispatcher("转发路径").forward(request,response);
2)使用response进行页面重定向
response.sendRedirect("重定向路径");
3)也可以使用response指定响应结果
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter.write("json串");
以上三种返回值没有什么重要和不重要的分别,一般来说都会使用到, 只不过有的时候使用的方式会有一些细微的差别
==网文2 此文有代码,对上方的拓展
SpringMVC学习(七)——Controller类的方法返回值 - CSDN博客
http://blog.csdn.net/yerenyuan_pku/article/details/72511844
== 很多返回类型很陌生
springMVC教程(八)controller中方法的返回值类型 - CSDN博客
http://blog.csdn.net/baidu_16702581/article/details/32695519
== 很不错,但是里面多了很多<span> 多余的标签了,应该是博主从其他地方拷贝过来的
SpringMVC的Controller层参数绑定以及返回值 - CSDN博客
http://blog.csdn.net/nuowei_senlin/article/details/53695956
当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定
Controller方法的返回值1:返回void类型 @RequestMapping("/test_void.action")
public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");//通过HttpServletRequest获得请求参数
System.out.println("用户名:"+username);
request.setAttribute("username",username);
User u = new User();
u.setUsername(username);
userService.insertUser(u);//插入数据库
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发
}
Controller方法的返回值2:返回ModelAndView @RequestMapping("/test_modelandview.action")
public ModelAndView controller02(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1
String username = request.getParameter("username");
System.out.println("用户名:"+username);
ModelAndView modelAndView = new ModelAndView();//new一个ModelAndView
modelAndView.addObject("username",username);//相当于request.setAttribute(attrName,attrValue);
modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转
return modelAndView;
}
Controller方法的返回值3:返回String类型(逻辑视图) @RequestMapping("/test_string.action")
public String controller03(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");//转码
String username = request.getParameter("username");
request.setAttribute("username",username);//设置请求参数
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp";//返回String类型,代表逻辑视图
}
Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)
[html] view plain copy
@RequestMapping("/test_model.action")
public String controller04(HttpServletRequest request,Model model) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
model.addAttribute("username", username);//等价于request.setAttribute(attrName,attrValue);
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp";//返回String类型,跳转到逻辑视图
}
Controller方法的返回值5:返回重定向redirect后的逻辑视图名 @RequestMapping("/test_redirect.action")
public String controller05(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
request.setAttribute("username",username);//由于是redirect,所以请求参数失效
return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里
}
Controller方法的返回值6:返回farward转发后的逻辑视图名 @RequestMapping("/test_forword.action")
public String controller06(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
System.out.println("用户名:"+username);
request.setAttribute("username",username);//由于是转发,所以请求参数有效
return "forward:/controller/test_model.action";//转发,跳转到一个Controller里
} 参数绑定
参数绑定的第一种方法:绑定普通类型 //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
@RequestMapping("/test_parambinding01.action")
public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
//必须进行转码
username = new String(username.getBytes("iso8859-1"),"UTF-8");
request.setAttribute("username",username);
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
} 参数绑定的第二种方法:绑定pojo类 //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
@RequestMapping("/test_parambinding02.action")
public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
//必须进行转码
user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
userService.insertUser(user);
ModelAndView modelAndView = new ModelAndView();
request.setCharacterEncoding("utf-8");
modelAndView.addObject("username",user.getUsername());
modelAndView.addObject("password",user.getPassword());
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
} 参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解 @RequestMapping("test_RequestParam.action")
//将客户端的请求参数"username"与"uname"绑定
public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
uname = new String(uname.getBytes("iso-8859-1"),"utf-8");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username",uname);
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
原文
===此文 里面的小结感觉很实用,但本人理解不完全
SpringMVC Controller 返回值的可选类型 - xiepeixing - 博客园
http://www.cnblogs.com/xiepeixing/p/4243801.html
小结:
1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。
2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。
3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。
springMVC中controller的几种返回类型的更多相关文章
- SpringMVC中controller的几种返回值
String :跳转到对应的返回值中. return “/index”: ModelAndView: 控制页面跳转方式: 1. ModelAndView modelAndView = new Mode ...
- SpringMVC中Controller类的方法返回String不跳转,而是将字符串显示到页面
问题描述: 在spring中,控制层的注解一般都是使用@Controller,如果哪个请求参数需要返回数据的话,我们可以在该方法上配合@ResponseBody注解使用,这也是比较常见的方式了. 今天 ...
- SpringMVC中controller返回图片(转)
本文转自:http://blog.csdn.net/u011637069/article/details/51112187 SpringMVC中controller通过返回ModelAndView然后 ...
- 详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumen ...
- SpringMVC中Controller
详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] 目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodR ...
- 详解SpringMVC中Controller的方法中参数的工作原理——基于maven
转自:http://www.tuicool.com/articles/F7byQn 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:ht ...
- 【MVC - 参数原理】详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
- springmvc 中controller与jsp传值
参考:springmvc 中controller与jsp传值 springMVC:将controller中数据传递到jsp页面 jsp中,死活拿不到controller中的变量. 花了半天,网上列出各 ...
- Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool。后面三种布尔类型是为了与其他语言兼容而引入的
bool是LongBool类型. Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool.后面三种布尔类型是为了与其他语言兼容而引入的,一般情况下建议使 ...
随机推荐
- 【css3】使用filter属性实现改变svg图标颜色
1.兼容性: 2.应用场景:新增页面上传svg图标后,更改图标颜色后,在列表页面展示色值改后的svg图标. 3.解决方案:使用filter属性中的 drop-shadow,drop-shadow滤镜可 ...
- Html 改变原有标签属性
内容简要: 当标签内内容 达到某以条件的时候改变当前标签属性 例如原标签为<tr> 当tr内的值符合某一条件时把<tr>变成<a>标签 例:当订单状体编程已支付的时 ...
- LeetCode 613. Shortest Distance in a Line
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...
- TypeError: sequence item 1: expected str instance, int found
Error Msg Traceback (most recent call last): File "E:/code/adva_code/my_orm.py", line 108, ...
- python scapy dns 包字段解析
qr: 0表示查询报文,1表示响应报文opcode: 通常值为0(标准查询),其他值为1(反向查询)和2(服务器状态请求).aa: 表示授权回答(authoritative answer)tc: ...
- 关于toncat的Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986
首先这个错误通常的产生原因, 是tomcat的确收到了格式不正确的请求参数,根据tomcat的版本支持的解析,tomcat抛出这个错误. 但是还有一种就是前台发送的请求方式由 get发送导致本应pos ...
- 简单的JQuery完美拖拽
首先导入jq库,最好是1.0版本的.调用函数时,传入要拖拽元素的id值. function drag(sel){ $div = $("#"+sel); $div.mousedown ...
- ReentrantLock重入锁详解
1.定义 重入锁:能够支持一个线程对资源的重复加锁,也就是当一个线程获取到锁后,再次获取该锁时而不会被阻塞. 2.可重入锁的应用场景 2.1 如果已经加锁,则不再重复加锁,比如:交互界面点击后响应时间 ...
- Netty 5 io.netty.util.IllegalReferenceCountException 异常
异常信息 io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1 原因 handler 继承了 SimpleChan ...
- css3 box-shadow阴影(外阴影与外发光)讲解
基础说明: 外阴影:box-shadow: X轴 Y轴 Rpx color; 属性说明(顺序依次对应): 阴影的X轴(可以使用负值) 阴影的Y轴(可以使用负值) 阴影 ...