当前位置: 移动技术网 > IT编程>开发语言>Java > springBoot2 基础语法

springBoot2 基础语法

2019年02月13日  | 移动技术网IT编程  | 我要评论

请求响应

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个目录中。

使用默认目录

假设在以上4个目录有一个 login.html , 那么访问该网页的路径应该是 localhost:8080/login.html 。
上面的/** 表示不管多少重的路径,都是在这默认的4个路径下查询资源。例如: 我们访问路径为:
localhost:8080/image/aa.jpg 或者 localhost:8080/image/jpg/01/aa.jpg 。 从8080 后面就表示要在咱们的项目里面找东西了,那么如何找呢。 在那默认的4个目录中找子目录image , 然后在子目录iamge中查找aa.jpg ,后面的例子是在4个目录中查找 image/jpg/01这个子目录,然后在这个子目录中查找aa.jpg

自定义目录

一般来说,官方给定的默认目录已经足够我们开发用了。我们如果想要声明 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 "";
    }
}

请求转发

请求转发的写法有以下几种。

使用request对象

request.getrequestdispatcher("").forward(request,response);

直接返回跳转页面

return "";

或者

return "forward:";

重定向

返回值使用 redirect: 作为前缀,表示使用重定向来跳转页面 。

使用response对象

response.sendredirect("");

直接返回跳转页面

 @requestmapping("/save")
public string save09() throws servletexception, ioexception {return "redirect:";
}

 会话

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() .

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网