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

猜你喜欢

转载自www.cnblogs.com/zhenghongxin/p/10364553.html