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

使用默认目录

假设在以上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 "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() .

常用注解 

SpringBootApplication

包含@Configuration、@EnableAutoConfiguration、@ComponentScan 通常用在主类上。

EnableAutoConfiguration

作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置 这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

Repository:

用于标注数据访问组件,即DAO组件。

Service

用于标注业务层组件。

RestController

加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写Restful接口

要想跳转页面,类上使用的注解应该换成@Controller , @RestController 是用于返回json字符串的,而@Controller 使用于跳转页面

ResponseBody

表示该方法的返回结果直接写入HTTP response body中 一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。

Component

泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

ComponentScan

组件扫描。

Configuration

指出该类是 Bean 配置的信息源,相当于XML中的<beans></beans>,一般加在主类上

EnableAutoConfiguration

让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,一般加在主类上。

RequestMapping

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

该注解有六个属性:

params:指定request中必须包含某些参数值是,才让该方法处理。

headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。

value:指定请求的实际地址,指定的地址可以是URI Template 模式

method:指定请求的method类型, GET、POST、PUT、DELETE等

consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;

produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回

 

posted @ 2019-02-12 12:17  hongxinerke  阅读(1339)  评论(0编辑  收藏  举报