1、 void类型作为返回值类型

/**
* 如果方法写成了void就跟原来servlet含义是差不多 的
* json
*/
@RequestMapping("/firstRequest")
public void firstRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
UserInfo info=new UserInfo();
info.setUserid(1);
info.setUserName("李四");
/**
* json格式传递
*/
response.setCharacterEncoding("UTF-8");
String value= JSON.toJSONString(info);
response.getWriter().write(value);
}

代码实现

2 、String类型作为返回值类型

/**
* 返回值类型为String时,一般用于返回视图名称
* 1、当方法返回值为null时,默认将请求路径当做视图 forward:/jsp/index.jsp 如果说没有视图解析器,返回值为null携带数据只能用json
* 2、当方法返回一个String的字符串时,当字符串为逻辑视图名时只返回视图,如果携带数据则使用request,session或者json
* 3、当方法返回值加入forward时代表转发,如果写为redirect:xxxx代表重定向,不是返回视图了,但是不会这样做!!
*/
@RequestMapping("/secondRequest")
public String secondRequest(HttpServletRequest request, HttpServletResponse response){
request.setAttribute("user","张三");
return "forward:/jsp/index.jsp";
}

代码实现

3、 Object类型作为返回值类型

/**
*Object
* 1.当方法返回值为null时,默认将请求路径当作视图 jsp/objectRequest.jsp 如果没有视图解析器,如果返回值为null携带数据只能用json
* 2.当方法返回值为String类型字符串时,就是视图的逻辑名称
* 3.当返回对象或者集合数据时要使用json格式字符串,可选fashjson手动转换 ,也可以使用jackson自动转换
*/
@RequestMapping("/ObjectRequest")
@ResponseBody
public Object ObjectRequest(){
List<UserInfo> userInfoList=new ArrayList<>();
UserInfo userInfo=new UserInfo();
userInfo.setUserid(111);
userInfo.setUserName("鸭头一号");
UserInfo userInfo1=new UserInfo();
userInfo1.setUserid(222);
userInfo1.setUserName("鸭头二号");
userInfoList.add(userInfo);
userInfoList.add(userInfo1);
return userInfoList;
}

代码实现

4、 ModelAndView类型作为返回值类型

/**
* ModelAndView
* @param request 请求对象
* @param response 响应对象
* @return ModelAndView model是用来传递数据用的,view是所需要跳转的页面
*/
@RequestMapping("/model")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){
ModelAndView modelAndView=new ModelAndView();
//携带给页面数据
modelAndView.addObject("user","鸭头");
//指定跳转页面(视图解析器配置前后缀)
modelAndView.setViewName("index");
return modelAndView;
}

代码实现

5、 请求参数的自动类型转换

  5.1.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/oneRequest" method="post">
账户:<input type="text" name="userName"/>
密码:<input type="password" name="userpwd"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.1.2 Controller

(控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致)

@Controller
@RequestMapping("/fourth")
public class FourthController { /**
* 1、请求参数的自动类型转换
* @param userName
* @param userpwd
* @param model
* @return
* 控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致
*/
@RequestMapping(value = "/oneRequest")
public String oneRequest(String userName,String userpwd, Model model){
System.out.println(userName+"\t"+userpwd);
model.addAttribute("userCode",userName);
return "welcome";
} }

代码实现

5.2 RequestMethod.POST

  5.2.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/twoRequest" method="post">
账户:<input type="text" name="userName"/>
密码:<input type="password" name="userpwd"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.2.2 Controller

(此处必须设置请求类型,否则会显示405错误)

/**
* 2、RequestMethod.POST 此处必须设置请求类型 否则将会显示405错误
* @param userName
* @param userpwd
* @param model
* @return
* 控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致
*/
@RequestMapping(value = "/twoRequest",method = RequestMethod.POST)
public String twoRequest(String userName,String userpwd, Model model){
System.out.println(userName+"\t"+userpwd);
model.addAttribute("userCode",userName);
return "welcome";
}

代码实现

5.3 @RequestParam注解

  5.3.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/formRequest" method="post">
账户:<input type="text" name="userName"/>
密码:<input type="password" name="userpwd"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.3.2 Controller

/**
* 3.@RequestParam 注解
* 接收零散参数:装配原则为传递参数名和方法接收参数名一致
* defaultValue默认值 required代表是否必须传递
* @RequestParam 注解
*/
@RequestMapping(value = "/formRequest")
public String formRequest(@RequestParam(name = "userName",defaultValue = "鸭头") String userName, @RequestParam(name = "userpwd")String userpwd, Model model){
System.out.println(userName+"\t"+userpwd);
model.addAttribute("userCode",userName);
return "welcome";
}

代码实现

5.4 RESTFUL风格的参数传递

  5.4.1 Controller

/**
* 4、RESTFUL 风格的参数传递
* get请求时,如果需要传递参数,那么则把不能使用以往的方式?name=xxx&age=yy,但是现在要遵循restful风格,例:xxx/ttt/ddd
* 根据地址栏url匹配拿值 使用@PathVariable(name=地址栏中的参数映射)
*/
@RequestMapping("/restfulRequest/{b}/{d}")
public String restfulRequest(@PathVariable(name = "b") String usercode, @PathVariable(name = "d")String userpwd){
System.out.println(usercode+"\t"+userpwd);
return "welcome";
}

代码实现

5.5 对象参数

  5.5.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/Info" method="post">
账户:<input type="text" name="userName"/>
密码:<input type="password" name="userpwd"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.5.2 Controller

/**
* 5、对象参数
*/
@RequestMapping(value = "/Info")
public String UserRequest(UserInfo info){
System.out.println(info.getUserName());
return "welcome";
}

代码实现

5.6 域属性对象参数

  5.6.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/userInfoRequest" method="post">
老师一号:<input type="text" name="teacher.teacherName"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.6.2 Controller

/**
* 6、域属性对象参数
*/
@RequestMapping(value = "/userInfoRequest")
public String UserInfoRequest(UserInfo info){
System.out.println(info.getTeacher().getTeacherName());
return "welcome";
}

代码实现

5.7 域属性集合参数

  5.7.1 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="/fourth/userInfoRequest" method="post">
老师二号:<input type="text" name="teacherList[0].teacherName"/>
老师三号:<input type="text" name="teacherList[1].teacherName"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>

代码实现

  5.7.2 Controller

/**
* 7、域属性集合参数
*/
@RequestMapping(value = "/userListRequest")
public String UserListRequest(UserInfo info){
System.out.println(info.getTeacherList());
return "welcome";
}

代码实现

 

SpringMVC方法的返回值类型和自动装配的更多相关文章

  1. MVC方法的返回值类型

    MVC方法返回值类型 ModelAndView返回值类型: 1.当返回为null时,页面不跳转. 2.当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转. 3.当返回值指定了视图名,程序会按照 ...

  2. springMVC入门(四)------参数绑定与返回值类型

    简介 从之前的介绍,已经可以使用springMVC完成完整的请求.返回数据的功能. 待解决的问题:如何将数据传入springMVC的控制器进行后续的处理,完成在原生servlet/jsp开发中Http ...

  3. MyBatis中Mapper的返回值类型

    insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...

  4. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  5. 11.SpringMVC注解式开发-处理器方法的返回值

    处理器方法的返回值 使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型 1.ModelAndView 2.String 3.void 4.自定义类型对象 1.返回Model ...

  6. SSM-SpringMVC-21:SpringMVC中处理器方法之返回值Object篇

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 今天要记录的是处理方法,返回值为Object的那种,我给它分了一下类: 1.返回值为Object数值(例如1) ...

  7. SSM框架之SpringMVC(4)返回值类型及响应数据类型

    SpringMVC(4)返回值类型及响应数据类型 1. 返回值分类 1.1. 返回字符串 Controller方法返回字符串可以指定逻辑视图的名称,根据视图解析器为物理视图的地址. @RequestM ...

  8. SpringMVC——返回值类型

    1.void作为返回值类型 如果你的方法写成了Void就跟原来Servlet含义是差不多的 @RequestMapping("/index*") public void first ...

  9. Java学习笔记13---如何理解“子类重写父类方法时,返回值若为类类型,则必须与父类返回值类型相同或为其子类”

    子类重新实现父类的方法称重写:重写时可以修改访问权限修饰符和返回值,方法名和参数类型及个数都不可以修改:仅当返回值为类类型时,重写的方法才可以修改返回值类型,且必须是父类方法返回值的子类:要么就不修改 ...

随机推荐

  1. 数据库三,exec内置函数

    数据库三,exec内置函数 一.数据库查询与执行顺序 必备知识 查询语句的基本操作 - select - from - where - group by - having - distinct - o ...

  2. Mac中 pip3 install mysqlclient 报错

    主要错误提示如下: ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use ...

  3. python(一) jupyter 安裝

    copy from https://jupyter.org/install Getting started with JupyterLab Installation JupyterLab can be ...

  4. Reinforcement Learning by Sutton 第三章习题答案

    好不容易写完了 想看全部的欢迎点击下面的github https://github.com/LyWangPX/Solutions-of-Reinforcement-Learning-An-Introd ...

  5. Appium 1.15.1版本的appium-doctor不是内部或者外部命令的问题

    先讲一下整个app自动化环境的部署过程: 1.安装appium 2.安装nodejs 3.查看appium的环境是否完成 问题:安装appium和nodejs都没啥问题,直接到对应的官网下载然后安装即 ...

  6. Appium(二):Node.js下载与安装、非GUI版本appium下载与安装、GUI版本appium下载与安装

    1. 下载并安装Node.JS 进入官网:https://nodejs.org/en/. 由于我们是新手嘛,所以肯定是越稳定越好啦,所以选择下载LTS版本. 进入文件下点击文件就进入安装界面了,点击n ...

  7. linux-在指定路径下查询文件夹是否存在

    我们常常在Linux下去查找文件 find / -name 'test.py' # 在根目录下查找名为test.py的文件 但是如果用查找文件的方式去查找文件夹的话,是查不到的 find / -max ...

  8. go语言的错误处理

    1.系统自己抛异常 //go语言抛异常 func test3_1() { l := [5] int {0,1,2,3,4} var index int = 6 fmt.Println(l) l[ind ...

  9. IMP-00009: abnormal end of export file解决方案

    一.概述 最近在测试环境的一个oracle数据库上面,使用exp将表导出没有问题,而将导出的文件使用imp导入时却出现了如下错误. IMP-00009: abnormal end of export ...

  10. Netty服务端Channel的创建与初始化

    Netty创建服务端Channel时,从服务端 ServerBootstrap 类的 bind 方法进入,下图是创建服务端Channel的函数调用链.在后续代码中通过反射的方式创建服务端Channel ...