自学内容网 自学内容网

Spring Boot的Web开发

目录

Spring Boot的Web开发

1.静态资源映射规则

第一种静态资源映射规则

2.enjoy模板引擎

3.springMVC

3.1请求处理

@RequestMapping

@DeleteMapping 删除

@PutMapping 修改

@GetMapping 查询

@PostMapping 新增

3.2参数绑定

一.支持数据类型:

3.3常用注解

一.@RequestParam

二.@RequestBody

三.@PathVaribale

四.@RequestHeader

五.@CookieValue

3.4数据传递

3.5文件上传

4.注册Servlet三大组件 Servlet/Filter/Listener

5.切换为其他嵌入式Servlet容器

6.restFul


Spring Boot的Web开发

1.静态资源映射规则
第一种静态资源映射规则

总结:

只要静态资源放在类路径下: called /static (or INF/resources

访问 : 当前项目根路径/ + 静态资源名

静态资源访问前缀 spring: mvc: static-path-pattern: /apesource/**

2.enjoy模板引擎

1.将页面保存在templates目录下

2.添加坐标

        <dependency>
            <groupId>com.jfinal</groupId>
            <artifactId>enjoy</artifactId>
            <version>5.0.3</version>
        </dependency>

3.开启配置(创建配置类)

@Configuration
public class SpringBootConfig {
    @Bean(name = "jfinalViewResolver")
    public JFinalViewResolver getJFinalViewResolver() {
​
        // 创建用于整合 spring boot 的 ViewResolver 扩展对象
        JFinalViewResolver jfr = new JFinalViewResolver();
​
        // 对 spring boot 进行配置
        jfr.setSuffix(".html");
        jfr.setContentType("text/html;charset=UTF-8");
        jfr.setOrder(0);
​
        // 设置在模板中可通过 #(session.value) 访问 session 中的数据
        jfr.setSessionInView(true);
​
        // 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
        Engine engine  = JFinalViewResolver.engine;
​
        // 热加载配置能对后续配置产生影响,需要放在最前面
        engine.setDevMode(true);
​
        // 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
        engine.setToClassPathSourceFactory();
​
        // 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
        // 代替 jfr.setPrefix("/view/")
        engine.setBaseTemplatePath("/templates/");
​
​
        // 更多配置与前面章节完全一样
        // engine.addDirective(...)
        // engine.addSharedMethod(...);
​
        return jfr;
    }
}

4.编写代码

@Controller
@RequestMapping("user")
public class RequestController {
    @RequestMapping("/wwt")
    public String successFound(){
        return "success";
    }
}

3.springMVC

3.1请求处理
@RequestMapping

意义:处理用户的请求,相似于doget与dopost

位置: 类上:一级目录 方法:二级目录 例如:user/save user/delete student/save student/delete 属性: value = "",path = "" 表示请求路径

method=常量,此请求的类型(get,post),若不设置则此请求适配所有的请求方式

params = "" 限制请求参数,例如:params={"msg1","msg2"}表示请求路径中必须携带参数名为msg1与msg2的参数

注意:1.超链接默认发送的是get请求 2.所有请求所携带的参数格式均为:key = value

@DeleteMapping 删除
@PutMapping 修改
@GetMapping 查询
@PostMapping 新增

作用与@RequestMapping一样,只是请求方式不同,语法相同。

@RequestMapping可以点击查看源码 @Target({ElementType.METHOD, ElementType.TYPE}) METHOD==代表修饰方法,TYPE==代表修饰类

3.2参数绑定

绑定的机制:SpringMVC 绑定请求参数的过程是通过把表单提交请求参数,作为控制器中方法参数进行绑定的

一.支持数据类型:

1.基本类型参数:

包括基本类型和 String 类型

1.发送请求中携带数据的key与方法参数的name必须一致 2.数据类型合法

 @RequestMapping("/show1")
    public String one(String msg1){
        System.out.println("msg1="+msg1);
        return "success";
    }

2.POJO类型参数: 包括实体类,以及关联的实体类

    @RequestMapping("/show3")
    public String sendEmp(Emp emp){  //发送请求中携带数据的key与实体类的属性名必须一致
        System.out.println(emp);
        return "success";
    }

嵌套实体类:

@RequestMapping("/show3")
    public String sendEmp(Emp emp){  //发送请求中携带数据的key与实体类的属性名必须一致
        System.out.println(emp);
        return "success";
    }
    <!--被嵌套的实体类对象的name使用: 对象名.属性名 的方式 该属性名与发送请求中携带数据key必须一致-->
    <form action="/one/show4" method="post">
        员工编号:<input type="text" name="eid"><br/>
        员工姓名:<input type="text" name="ename"><br/>
        员工性别:<input type="text" name="esex"><br/>
        部门编号:<input type="text" name="dept.did"><br/>
        部门名称:<input type="text" name="dept.dname"><br/>
        <input type="submit" value="发送请求">
    </form>

3.数组和集合类型参数: 包括 List 结构和 Map 结构的集合(包括数组)

    @RequestMapping("/show5")
    public String sendMap(@RequestParam Map map){//参数类型Map前需要添加@RequestParam注解
        System.out.println(map);
        return "success";
    }
<form action="/one/show6" method="post">
        部门编号:<input type="text" name="did"><br/>
        部门名称:<input type="text" name="dname"><br/>
        员工编号1:<input type="text" name="emps[0].eid"><br/>
        员工姓名1:<input type="text" name="emps[0].ename"><br/>
        员工性别1:<input type="text" name="emps[0].esex"><br/>
        员工编号3:<input type="text" name="empMap['one'].eid"><br/>
        员工姓名3:<input type="text" name="empMap['one'].ename"><br/>
        员工性别3:<input type="text" name="empMap['one'].esex"><br/>
        <input type="submit" value="提交">
        <a href="/one/show7?nums=1233&nums=234&nums=23432">发送请求7</a>
        <h3>使用 ServletAPI 对象作为方法参数</h3>
        <a href="/one/show7">发送请求8</a>
    </form>
 

4.使用 ServletAPI 对象作为方法参数 HttpServletRequest HttpServletResponse HttpSession java.security.Principal Locale InputStream OutputStream Reader Writer

@RequestMapping("/show8")
    public String show7(HttpServletRequest request, HttpServletResponse response){
​
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        System.out.println(request);
        System.out.println(response);
        request.getParameter("msg1");
​
        HttpSession session =     request.getSession();
        System.out.println(session);
        session.setAttribute("","");
        try {
            response.sendRedirect("重定向");
        } catch (IOException e) {
            e.printStackTrace();
        }
        ServletContext applaction =  session.getServletContext();
​
        return "success";
    }

二.使用要求 1.发送请求中携带数据的key与方法参数的name必须一致 2.数据类型合法

3.3常用注解

springMVC常用注解

一.@RequestParam

作用: 把请求中指定名称的参数给控制器中的形参赋值。 如果页面标签名称和方法参数名称不一致,可以使用此注解实现 属性: name属性:设置参数名称 defaultValue属性:设置默认值 required属性:设置是否为必传

  @RequestMapping("show1")
    public String show1(@RequestParam(value = "msg") String msg1){
        System.out.println("msg1="+msg1);
        return "success";
    }
​
    @RequestMapping("/show2")
    public String show2(@RequestParam("msg") String msg1,@RequestParam("msg2") String msg2){
        System.out.println("msg1="+msg1+"msg2="+msg2);
        return "success";
    }
二.@RequestBody

作用: 用于获取"请求体"内容。直接使用得到是 key=value&key=value... 结构的数据,并可以转换为对象 属性: required:是否必须有请求体。默认值是:true。

  @RequestMapping("/show3")
    public String show3(@RequestBody Emp emp){
        System.out.println(emp);
        return "success";
    }
三.@PathVaribale

作用: 用于绑定 url 中的占位符。例如:请求 url 中 /delete/{id}, 这个{id}就是 url 占位符。url 支持占位符是 spring3.0 之 后加入的。是 springmvc 支持 rest 风格 URL 的一个重要标志 属性: value:用于指定 url 中占位符名称。 required:是否必须提供占位符。

Restful是一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。 主要用于客户端和服务器交互类的软件,基于这个风格设计的软件可以更简洁,更有层次, 更易于实现缓存机制等。

Restful风格的请求是使用“url+请求方式”表示一次请求目的的,HTTP 协议里面四个表示操作方式的动词如下:  GET:用于获取资源  POST:用于新建资源  PUT:用于更新资源  DELETE:用于删除资源 例如:  /users/1 GET : 得到 id = 1 的 user  /users/1 DELETE: 删除 id = 1 的 user  /users/1/新名/新性 PUT: 更新 id = 1 的 user  /users/新名/新性 POST: 新增 user

 @RequestMapping("/show4/{ename}/{eid}")
    public String show4(@PathVariable String ename,@PathVariable String eid){
        System.out.println(ename+"ename"+eid+"eid");
        return "success";
    }
​
    @RequestMapping("/show5/{name}/{id}")
    public String show5(@PathVariable(value = "name") String ename,@PathVariable(value = "id") int eid){
        System.out.println(ename+"ename"+eid+"eid");
        return "success";
    }
四.@RequestHeader

作用: 用于获取请求消息头。

属性: value:提供消息头名称 required:是否必须有此消息头

    @RequestMapping("/show1")
    public String show1(@RequestHeader(value = "name")String name){
        System.out.println(name);
        return "success";
    }
五.@CookieValue

作用: 用于把指定 cookie 名称的值传入控制器方法参数。

属性: value:指定 cookie 的名称。 required:是否必须有此 cookie。

    @RequestMapping("/show2")
    public String show2(@CookieValue(value = "JSESSIONID",required = false)String id){
        System.out.println(id);
        return "success";
    }
3.4数据传递

controller的返回值类型有两种,Json和String

String类型返回值作用:

  1. 充当视图的逻辑名称,默认页面跳转为请求转发方式

    @RequestMapping("/show01")
    public String show01(){
        System.out.println("成功响应show01");
        return "success";
    }

     2. 充当一次请求转发或重定向(发送请求)

@RequestMapping("/show02")
    public String show02(){
        System.out.println("成功响应show02");
        return "redirect:show01";
    }
    @RequestMapping("/show03")
    public String show03(){
        System.out.println("成功响应show03");
        return "forward:show01";
    }

     3. 给session中存数据并返回前端

    @RequestMapping("/show04")
    public String show04(HttpServletRequest request){
        System.out.println("成功响应show04");
        Emp emp = new Emp(11,"wwt","男");
        request.getSession().setAttribute("emp",emp);
        return "success";
    }

Json类型返回值作用:

将对象转换为json格式返回给前端:

    @RequestMapping("/show02")
    @ResponseBody
    public List<Emp> shoe02(){
        Emp emp1 = new Emp(12,"wwt","男");
        Emp emp2 = new Emp(13,"wwt","男");
        Emp emp3 = new Emp(14,"wwt","男");
        List<Emp> list = new ArrayList<>();
        list.add(emp1);
        list.add(emp2);
        list.add(emp3);
        return list;
    }
3.5文件上传

该案例以七牛云为例:

  1. 在springboot项目中导入七牛云需要坐标

        <!--导入七牛云需要坐标-->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.25</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>happy-dns-java</artifactId>
            <version>0.1.6</version>
            <scope>test</scope>
        </dependency>

         2.配置七牛云相关信息:

@Configuration
public class SpringBootConfig {
    @Bean(name = "jfinalViewResolver")
    public JFinalViewResolver getJFinalViewResolver() {

        // 创建用于整合 spring boot 的 ViewResolver 扩展对象
        JFinalViewResolver jfr = new JFinalViewResolver();

        // 对 spring boot 进行配置
        jfr.setSuffix(".html");
        jfr.setContentType("text/html;charset=UTF-8");
        jfr.setOrder(0);

        // 设置在模板中可通过 #(session.value) 访问 session 中的数据
        jfr.setSessionInView(true);

        // 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
        Engine engine  = JFinalViewResolver.engine;

        // 热加载配置能对后续配置产生影响,需要放在最前面
        engine.setDevMode(true);

        // 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
        engine.setToClassPathSourceFactory();

        // 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
        // 代替 jfr.setPrefix("/view/")
        engine.setBaseTemplatePath("/templates/");


        // 更多配置与前面章节完全一样
        // engine.addDirective(...)
        // engine.addSharedMethod(...);

        return jfr;
    }
}

      3.写一个简单的文件上传页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
  <form action="/file/show1" method="post" enctype="multipart/form-data">
    用户名:<input name="uname"/><br>
    图片:<input name="upic" type="file"/><br>
    <input type="submit" value="上传">
  </form>
</body>
</html>

     4.编写控制文件上传controller

@Controller
@RequestMapping("/file")
public class fileController {
    @RequestMapping("/show")
    public String show(){
        return "index";
    }
    @RequestMapping("/show1")
    public String file(String uname, MultipartFile upic, HttpServletRequest request){
        System.out.println(uname);
        System.out.println(upic);
        System.out.println(upic.getOriginalFilename());
        System.out.println(upic.getName());


        //方式1.将文件upic以流的方式写入当前服务器磁盘(应用服务器)
        //方式2.文件服务器(七牛云)
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
        String accessKey = "FvHzx7X-XV_EJwLWin-8-AkozGAu8tR0_ELaSCcB";
        String secretKey = "DQSLg30LmmrlWuFGiRUKMxrw6l2169A0i-miKJZ7";
        String bucket = "db202407";
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;
        String name = null;

        try {
            byte[] uploadBytes = upic.getBytes();
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            try {
                Response response = uploadManager.put(uploadBytes, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);//获取文件名
                System.out.println(putRet.hash);//获取文件hash值
                name = putRet.key;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception ex) {
            //ignore
        }
        request.getSession().setAttribute("picname",name);

        return "success";
    }
}

     5.最后将它显示在页面上(通过enjoy模版引擎传递数据)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
    成功上传
    <div>#(session.picname)</div>
    <img src="http://sgwh6dzed.hd-bkt.clouddn.com/#(session.picname)">
</body>
</html>

4.注册Servlet三大组件 Servlet/Filter/Listener

而由于 Spring Boot 默认是以 jar 包的方式运行嵌入式Servlet容器来启动应用,没有web.xml文件, Spring提供以下Bean来注册三大组件

  1. 配置类配置三大组件:

@Configuration
public class MyMvcConfig {
    //注册Serlvet
    //替换:@WebServlet(urlPatterns = "/mySerlvet")
    @Bean
    public ServletRegistrationBean doServlet(){
        ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>();
        bean.setServlet(new MyServlet());
        bean.setUrlMappings(Arrays.asList("/myServlet"));
        bean.setLoadOnStartup(1);
        return bean;
    }

    //注册Filter
    @Bean
    public FilterRegistrationBean doFilter(){
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new MyFilter());
        filter.addUrlPatterns("/*");
        return filter;
    }

    //注册Listener
    @Bean
    public ServletListenerRegistrationBean doListener(){
        //关闭监听器切记不要点击红色按钮,太暴力,点击控制台左侧exist
        ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean();
        listener.setListener(new MyListener());
        return listener;
    }
}

      (1) ServletRegistrationBean 注册自定义Servlet

//@WebServlet("/servlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入servlet");
        resp.getWriter().println("<h1>Hello World!</h1>");
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

     (2) FilterRegistrationBean 注册自定义Filter

//@WebFilter(urlPatterns = {"/*"})
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("========请求过滤");
        servletRequest.setCharacterEncoding("UTF-8");

        //分水岭
        filterChain.doFilter(servletRequest,servletResponse);

        servletRequest.setCharacterEncoding("UTF-8");
        System.out.println("========响应过滤");
    }
}

     (3) ServletListenerRegistrationBean 注册自定义Listener

//@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("执行listener初始化方法");
        ServletContextListener.super.contextInitialized(sce);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("执行listener销毁方法");
        ServletContextListener.super.contextDestroyed(sce);
    }
}

     2. 如果使用传统 @WebFilter...实现注册也可以

        条件:1.一定是自定义组件

                   2.启动类添加@ServletComponentScan

5.切换为其他嵌入式Servlet容器

SpringBoot 默认针对Servlet容器提供以下支持:

  • Tomcat(默认使用)

  • Jetty :支持长连接项目(如:聊天页面)

  • Undertow : 不支持 JSP , 但是并发性能高,是高性能非阻塞的容器

切换 Jetty 容器

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除tomcat容器 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--引入其他的Servlet容器-->
        <dependency>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <groupId>org.springframework.boot</groupId>
        </dependency>

嵌入式Servlet容器:运行启动类就可启动,或将项目打成可执行的 jar 包 优点:简单、快捷; 缺点:默认不支持JSP、优化定制比较复杂使用定制器, 还需要知道 每个功能 的底层原理 外置Servlet容器:配置 Tomcat, 将项目部署到Tomcat中运行

6.restFul

REST 指的是一组架构约束条件和原则,rest原则有部分组成: URL定位资源,HTTP动词操作(GET, POST, PUT,DELETE) 描述操作。

 设计RESTful风格的API:

  1. 在RESTful风格的架构中, 每个网址代表一种资源,所以网址中不能有动词,只能有名词。而且所 用的名词往往与数据库的表名对应。
  2. HTTP动词设计: GET (获取资源) POST (新建资源) PUT (更新资源,客户端提供改变后的完整资源) DELETE (删除资源)

请求方式 含义

GET /zoos                                                    列出所有动物园

POST /zoos                                                 新建一个动物园

GET /zoos/ID                                               获取某个指定动物园的信息

PUT /zoos/ID                                               更新某个指定动物园的信息(提供该动物园的全部信息)

DELETE /zoos/ID                                        删除某个动物园

GET /zoos/lD/animals                                  列出某个指定动物园的所有动物

DELETE /zoos/lD/animals/ID                       删除某个指定动物园的指定动物


原文地址:https://blog.csdn.net/qq_63688997/article/details/140555634

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!