SpringMVC-02

2024-01-10 10:26:47

添加@EnableWebMvc //配置json转化器 (使用postman)
可以不用写下面两个方法了
@Bean
public RequestMappingHandlerMapping handlerMapping(){
return new RequestMappingHandlerMapping();
}
@Bean
public RequestMappingHandlerAdapter handlerAdapter(){
return new RequestMappingHandlerAdapter();
}

接收数据

接收cookie

    @RequestMapping("/data")
    public String cookie(@CookieValue(value="cookieName") String value){
        return value;
    }

    @GetMapping("/save")
    public String save(HttpServletResponse response){
        Cookie cookie=new Cookie("cookieName","root");
        response.addCookie(cookie);
        return "OK";
    }

获取请求头

    @RequestMapping("data")
    public String data(@RequestHeader("Host") String host){
        return "host:"+host;
    }

原生对象获取

    public String data(HttpServlet httpServlet, HttpServletRequest httpServletRequest,
                       HttpSession httpSession){
        return "HttpServlet"+httpServlet+" HttpServletRequest"+httpServletRequest+" HttpSession"+
                httpSession;
    }

共享域对象

//1.原生api方式	
    @Autowired
    private ServletContext servletContext;
    public void data(HttpServletRequest httpServletRequest, HttpSession httpSession){
        
    }
    
 //2.
    //spring提供的方法:request提供了几种   model   moaelMap    map    modelAndView

文章来源:https://blog.csdn.net/qq_53568730/article/details/135494614
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。