springBoot2 基础语法
请求响应
request对象
request 对象其实是httpservletrequest 类型, 通过它可以获取请求相关的一切信息, 包含请求信息 、 以及请求参数 ,甚至可以当成往里面存储数据【暂定】
@requestmapping("/aa") @responsebody public string aa(httpservletrequest request , httpservletresponse response){ //1. 获取请求行 string method = request.getmethod(); //请求方式 string uri = request.getrequesturi(); //请求地址 string protocol = request.getprotocol(); //获取协议 system.out.println(method + " :" + uri + " : "+ protocol); //2. 获取请求头信息 enumeration<string> headernames = request.getheadernames(); while (headernames.hasmoreelements()){ string headername = headernames.nextelement(); string headervalue = request.getheader(headername); system.out.println(headername + " = " + headervalue); } //3. 获取请求参数 string username = request.getparameter("username"); return "请求成功"; }
response对象
response的类型为httpservletresponse类型 , 在客户端发出每个请求时,服务器都会创建一个response对象,目的是为了对客户端的请求做出响应。
@requestmapping("testresponse") public void testresponse(httpservletresponse response)throws ioexception{ //1. 设置响应行 response.setstatus(200); //2. 设置响应头 response.setheader("myheader" , "myheadervalue"); //3. 设置响应体 response.setcontenttype("text/html;charset=utf-8"); response.getwriter().write("你好~!??"); system.out.println("执行了testresponse"); }
资源
静态资源
在gradle资源目录中,有个resource的目录,该目录主要是用来存放项目的资源,一般是html 、css 、js、图片 … . 默认情况下,resource下的资源是不能随便乱放的。因为spring boot 在处理资源匹配上,有自己默认的配置。 其中匹配的是 /static ,/public, /resources, /meta-inf/resources 目录 。 比如我们有一个html页面,那么这个html页面,默认可以放在以上4个目录中。
使用默认目录
自定义目录
一般来说,官方给定的默认目录已经足够我们开发用了。我们如果想要声明 html文件, 可以在static下新建目录html , 如果要表示 图片,可以再static下新建image目录。如果自定义目录,需要在resource下新建application.properties 文件,在文件中指定路径。
#表示静态资源位置 直到public 都是默认的位置。 后面的是我们自己添加的。 spring.resources.static-locations=classpath:/meta-inf/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/html,classpath:/image
转发 & 重定向
跳转页面
要想跳转页面,类上使用的注解应该换成@controller , @restcontroller 是用于返回json字符串的,而@controller 使用于跳转页面
@controller public class usercontroller { private static final string tag = "usercontroller"; @requestmapping("/user_register") public string register(user user){ //此处使用对象来封装数据 try { filewriter filewriter = new filewriter("stu.txt", true); filewriter.write(user.tostring() + "\r\n"); filewriter.close(); system.out.println("注册成功"); } catch (ioexception e) { e.printstacktrace(); } return "index.html"; } }
请求转发
请求转发的写法有以下几种。
使用request对象
request.getrequestdispatcher("index.html").forward(request,response);
直接返回跳转页面
return "index.html"; 或者 return "forward:index.html";
重定向
返回值使用 redirect: 作为前缀,表示使用重定向来跳转页面 。
使用response对象
response.sendredirect("index.html");
直接返回跳转页面
@requestmapping("/save") public string save09() throws servletexception, ioexception {return "redirect:index.html"; }
会话
cookie
基本使用
cookie cookie = new cookie("key" ,"value"); response.addcookie(cookie);
设置时长
cookie cookie = new cookie("name","aobama"); //设置过期时间 cookie.setmaxage(60 * 60 * 24 * 7); response.addcookie(cookie);
session
获取session
httpsession session = request.getsession()
存值
session.setattribute(name ,value);
取值
session.getattribute(name);
移除值
session.removeattribute(name);
让session失效 作废
session.invalidate();
获取id值
session的id值就是这一块内存空间的唯一标识符。 session.getid() .