@Controller
@RequestMapping("/param")
public class ParamController {
//直接接收
//形参列表填写对应名称即可
//1.名称需要相同 2.可以不传递,不报错
@RequestMapping("/data")
@ResponseBody
public String data(String name,int age){
System.out.println("name = " + name+"age = " + age);
return "name = " + name+"age = " + age;
}
//注解指定
//指定任意参数名,要求必须传递 要求不必传递 给与一个默认值
/**
* @RequestParam -- > 形参列表 指定请求参数名 或者是否必须传递 或者非必须传递,设置默认值
* 用法:@RequestParam(value = "account") 指定请求名称
* @RequestParam(required = true,defaultValue = "1") 设置是否必须传参,是否有默认值
*/
@GetMapping("data1")
@ResponseBody
public String data1(@RequestParam(value = "account") String account,
@RequestParam(required = true,defaultValue = "1") int page){
return "name = " + account+"age = " + page;
}
//特殊值
//一名多值 爱好 hbs等等
//param/data2?hbs=chi&hbs=wan&hbs=tangtou
@GetMapping("data2")
@ResponseBody
//必须加入 @RequestParam@RequestParam 注解
//不加注解会将 hbs的字符串直接赋值给List集合 报类型异常错误
public String data2(@RequestParam List<String> hbs){
return hbs.toString();
}
//使用实体对象接值 用户注册信息 ----> 对应的实体类 ----> 插入到数据库
//param/data3?name="二狗子"&age=18 准备一个对应实体类 有get set方法
@RequestMapping("/data3")
@ResponseBody
public String data3(User user){
System.out.println(user);
return user.toString();
}
}
/**
* 路径参数演示
*/
@Controller
@RequestMapping("/path")
@ResponseBody
public class PathController {
// path/账号/密码
/**
* 动态路径的设计 {key} = * {key}可以在形参列表获取传入的参数
* 接收路径参数 String account,String password-> 接收Param格式参数
*/
@RequestMapping("{account}/{password}")
public String login(@PathVariable(value = "account") String username, @PathVariable String password){
System.out.println("username = " + username +"===="+ " password = " + password);
return "username = " + username +"===="+ " password = " + password;
}
}
@RequestMapping("/json")
@Controller
@ResponseBody
public class JsonController {
//data --> 请求体 post {name,age,gender}
//前端 --> 415 不支持媒体类型 ?
// java原生API只支持 Param和路径传参 而不支持Json
// 1. 导入相关依赖 jackson-bind
// 2. handlerAdapt配置Json转换器 配置类上加 @EnableWebMvc 注解
// 相当于添加了经理 handlerAdapter 添加秘书 handlerMapping 添加jackson处理器
@RequestMapping("/data")
public String data(@RequestBody Person person){
System.out.println(person);
return person.toString();
}
}
@Controller
@ResponseBody
@RequestMapping("/cookie")
public class CookieController {
@RequestMapping("/data")
public String data(@CookieValue("cookieName") String value){
System.out.println("value = " + value);
return "value = " + value;
}
@GetMapping("/save")
public String save(HttpServletResponse response){
Cookie cookie = new Cookie("cookieName", "root");
response.addCookie(cookie);
return "ok";
}
}
@Controller
@ResponseBody
@RequestMapping("/header")
public class HeadController {
@RequestMapping("/data")
public String data(@RequestHeader String host){
System.out.println("host = " + host);
return "host = " + host;
}
}
@Controller
@ResponseBody
@RequestMapping("/api")
public class ApiController {
@Autowired
private ServletContext servletContext;
public String data(HttpServletRequest request,
HttpServletResponse response,
HttpSession session
){
//ServletContext [1.最大的配置文件 2.最大的共享域 3.核心API拥有者 ]
//方案一:request、session获取
ServletContext servletContext = request.getServletContext();
//方案二:全局声明一个ServletContext
return null;
}
}
request 一次请求或者转发
session 一个浏览器的多次请求
ServletContext 全应用程序存储
存 setAtribute
取 getAtribute
/**
* 共享域数据获取
*/
@RequestMapping("/share")
@ResponseBody
@Controller
public class ShareController {
@Autowired
private ServletContext servletContext;
//方法一:原生获取
public void data(HttpServletRequest request,
HttpServletResponse response,
HttpSession session
){
}
//方法二:SpringMVC提供的方式获取,针对request提供了几种方案(了解即可)
//model modelMap map modelAndView
public void data1(Model model) {
model.addAttribute("key","value"); //传到request共享域
}
public void data2(ModelMap modelMap) {
modelMap.addAttribute("key","value"); //传到request共享域
}
public void data3(Map map) {
map.put("key","value"); //传到request共享域
}
public ModelAndView data4() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("key","value"); //传到request共享域
modelAndView.setViewName("视图名称/页面名称");
return modelAndView;
}
}
推荐阅读: