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";
}
代码实现