0
点赞
收藏
分享

微信扫一扫

初学SpringBoot--ch03-SpringBoot入门

毅会 2022-02-09 阅读 99

ch03-SpringBoot入门

1.1 SpringBoot 使用 JSP

SpringBoot不推荐使用jsp ,而是使用模板技术代替jsp。

1.1.1 添加Maven依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--引入 Spring Boot 内嵌的 Tomcat 对 JSP 的解析包,不加解析不了 jsp 页面-->
        <!--如果只是使用 JSP 页面,可以只添加该依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--如果要使用 servlet 必须添加该以下两个依赖-->
        <!-- servlet 依赖的 jar 包-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- jsp 依赖 jar 包-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!--如果使用 JSTL 必须添加该依赖-->
        <!--jstl 标签依赖的 jar 包 start-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

1.1.2 添加编译路径

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- SpringBoot 要求 jsp 文件必须编译到指定的META-INF/resources 目录下才能访问,否则访问不到。
        其实官方已经建议使用模版技术(后面会课程会单独讲解模版技术)-->
        <resources>
            <resource>
                <!--源文件位置-->
                <directory>src/main/webapp</directory>
                <!--指定编译到META-INF/resource,该目录不能随便写-->
                <targetPath>META-INF/resources</targetPath>
                <!--指定要把哪些文件编译进去,**表示 webapp 目录及子
                目录,*.*表示所有文件-->
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

1.1.3 配置视图解析器

在这里插入图片描述

1.1.4 创建控制器:

JspController

package com.suyv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class JspConreoller {

    @RequestMapping("/jsp")
    public String doJsp(HttpServletRequest request){
        request.setAttribute("data","SpringBoot使用Jsp");
        //视图的逻辑名称
        return "index";
    }

}

1.1.5 新建webapp文件夹

在这里插入图片描述

1.1.6 新建index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp文件</title>
</head>
<body>
    <h3>使用jsp,显示Controller中的数据 ${data}</h3>
</body>
</html>

1.1.7 运行测试

在这里插入图片描述

1.2 SpringBoot 使用 ApplicationContext

在 main 方法中 SpringApplication.run()方法获取返回的 Spring 容器对象,再获取业务 bean 进行调用。

1.2.1 创建UserService

package com.suyv.service;

public interface UserService {

    void sayHello(String name);
}

1.2.2 创建UserService实现类

package com.suyv.service.impl;

import com.suyv.service.UserService;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Override
    public void sayHello(String name) {
        System.out.println("执行了业务方法sayHello"+name);
    }
}

1.2.3 获取容器对象

package com.suyv;

import com.suyv.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        // 获取容器对象
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        // 从容器中获取对象
        UserService userService = (UserService) ctx.getBean("userService");
        userService.sayHello("李四");
    }
}

1.3 CommandLineRunner 接口

开发中可能会有这样的情景。需要在容器启动后执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot 给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为 CommandLineRunner 和 ApplicationRunner。他们的执行时机为容器启动完成的时候。

这两个接口中有一个 run 方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner 中 run 方法的参数为 ApplicationArguments ,而 CommandLineRunner 接口中 run 方法的参数为 String 数组。

1.3.1 创建HelloService

package com.suyv.service;

public interface HelloService {
    String sayHello(String name);
}

1.3.2 创建HelloService实现类

package com.suyv.service.impl;

import com.suyv.service.HelloService;
import org.springframework.stereotype.Service;

@Service("helloService")
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "你好" + name;
    }
}

1.3.3 获取容器对象

package com.suyv;

import com.suyv.service.HelloService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import javax.annotation.Resource;

@SpringBootApplication
public class Ch07SpringbootContainerApplication implements CommandLineRunner {

    @Resource
    private HelloService helloService;

    public static void main(String[] args) {
        //run 方法返回值是容器对象
        //1.创建容器对象
        ConfigurableApplicationContext ctx = SpringApplication. run (Ch07SpringbootContainerApplication.class, args);
        HelloService helloService = (HelloService) ctx.getBean("helloService");
        helloService.sayHello("李四");
    }

    @Override
    public void run(String... args)  throws Exception {
        //2. 容器对象创建好,执行 run
        System. out .println("输出, 在容器对象创建好后执行的代码");
    }

}
举报

相关推荐

0 条评论