• SpringBoot:jsp、容器对象、CommandLineRunner接口使用(动力)



    目录:

     (1)jsp的使用

    (2)手工获取容器对象

    (3)使用CommandLineRunner


     

    (1)jsp的使用

    在springboot框中中并不推荐使用jsp,默认本身也不支持jsp,经过配置才可以使用,这个技术慢慢就会被淘汰掉了,会用模板技术来替代jsp,用模板(第九章讲)来作为视图做异步交互,显示数据

     

    pom.xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.7.1</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.bjpowernode</groupId>
    12. <artifactId>009-springboot-jsp</artifactId>
    13. <version>1.0.0</version>
    14. <properties>
    15. <java.version>1.8</java.version>
    16. </properties>
    17. <dependencies>
    18. <!--处理jsp的依赖-->
    19. <dependency>
    20. <groupId>org.apache.tomcat.embed</groupId>
    21. <artifactId>tomcat-embed-jasper</artifactId>
    22. </dependency>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-web</artifactId>
    26. </dependency>
    27. <dependency>
    28. <groupId>org.springframework.boot</groupId>
    29. <artifactId>spring-boot-starter-test</artifactId>
    30. <scope>test</scope>
    31. </dependency>
    32. </dependencies>
    33. <build>
    34. <!--指定jsp编译后存放的目录-->
    35. <resources>
    36. <resource>
    37. <!--jsp原来的目录-->
    38. <directory>src/main/webapp</directory>
    39. <!--指定编译后的存放目录-->
    40. <targetPath>META-INF/resources</targetPath>
    41. <!--指定webapp下的目录和文件-->
    42. <includes>
    43. <include>**/*.*</include>
    44. </includes>
    45. </resource>
    46. </resources>
    47. <plugins>
    48. <plugin>
    49. <groupId>org.springframework.boot</groupId>
    50. <artifactId>spring-boot-maven-plugin</artifactId>
    51. </plugin>
    52. </plugins>
    53. </build>
    54. </project>

    在pom.xml,中加入jsp依赖:

    1. <!--处理jsp的依赖-->
    2. <dependency>
    3. <groupId>org.apache.tomcat.embed</groupId>
    4. <artifactId>tomcat-embed-jasper</artifactId>
    5. </dependency>

    在main下创建一个webapp目录,但它是普通的目录需要声明成web文件夹:点击009项目的Web,在点击右下方的+号,选中刚才创建的文件夹爱webapp

     创建一个jsp页面,接收来自控制器的数据:index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/30
    5. Time: 8:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h3>使用jsp,显示Controller中的数据=${data}</h3>
    20. </body>
    21. </html>

    控制器:JspController:

    1. package com.bjpowernode.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.ui.Model;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class JspController {
    8. //可以使用request,或者Model来传输数据
    9. /* public String doJsp(HttpServletRequest request){
    10. request.setAttribute("data","Slpringboot使用Jsp");
    11. //视图的逻辑名称
    12. return "index";
    13. }*/
    14. @RequestMapping("/myjsp")
    15. public String doJsp(Model model){
    16. model.addAttribute("data","Slpringboot使用Jsp");
    17. //视图的逻辑名称
    18. return "index";//返回的是视图,不用在@ResponseBody注解,视图是一个逻辑地址,需要加视图解析器
    19. }
    20. }

    配置文件Application.properties:配置端口号、视图解析器等

    1. #配置端口号
    2. server.port=9090
    3. #配置上下文
    4. server.servlet.context-path=/myboot
    5. #配置视图解析器 :有一个前缀,后缀
    6. #前缀 :/ 表示:src/main/webapp
    7. spring.mvc.view.prefix=/
    8. #后缀
    9. spring.mvc.view.suffix=.jsp

    告诉springboot框架jsp编译完成后放的位置,放在指定位置,告诉springboot框架到哪里使用jsp,在pom.xml的<bulid>中加入:

    <resources>
        <resource>
            <!--jsp原来的目录-->
            <directory>src/main/webapp</directory>
            <!--指定编译后的存放目录-->
            <targetPath>META-INF/resources</targetPath>
            <!--指定webapp下的目录和文件-->
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>

    主启动类:Application:

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

     在浏览器输入:http://localhost:9090/myboot/myjsp

     编译后的目录:可以看到它正常的按照把jsp文件方到META-INF/resources目录下

     (2)手工获取容器对象

     

     UserService接口:

    1. package com.bjpowernode.service;
    2. public interface UserService {
    3. void sayHello(String name);
    4. }

     实现类UserServiceInpl:

    1. package com.bjpowernode.service.impl;
    2. import com.bjpowernode.service.UserService;
    3. import org.springframework.stereotype.Service;
    4. @Service("userService") //注解创建Service对象
    5. public class UserServiceImpl implements UserService {
    6. @Override
    7. public void sayHello(String name) {
    8. System.out.println("执行了业务方法sayHello:"+name);
    9. }
    10. }

    主启动类Application:获取容器创建对象:

    1. package com.bjpowernode;
    2. import com.bjpowernode.service.UserService;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import org.springframework.context.ConfigurableApplicationContext;
    6. @SpringBootApplication
    7. public class Application {
    8. public static void main(String[] args) {
    9. //获取容器对象,用一个返回值接收run方法,获取的即是容器对象 ConfigurableApplicationContext是ApoplicationContext的子类
    10. ConfigurableApplicationContext ctx =SpringApplication.run(Application.class, args);
    11. //从容器中获取对象
    12. UserService userService = (UserService)ctx.getBean("userService");
    13. userService.sayHello("李四");
    14. }
    15. }

    运行项目:调用了方法

     这个操作在什么时候用呢?

     一般在自己做了测试时,想测试某个功能,你不想把整个项目都运行起来,你只想测试这个Service方法能不能用,这样做就比较方便一些

    (3)使用CommandLineRunner

     

    HelloService接口 :

    1. package com.bjpowernode.service;
    2. public interface HelloService {
    3. String sayHello(String name);
    4. }

    实现类HelloServiceImpl:

    1. package com.bjpowernode.service.impl;
    2. import com.bjpowernode.service.HelloService;
    3. import org.springframework.stereotype.Service;
    4. @Service("helloService") //注解创建对象
    5. public class HelloServiceImpl implements HelloService {
    6. @Override
    7. public String sayHello(String name) {
    8. return "你好:"+name;
    9. }
    10. }

    主启动类Application:实现CommandLineRunner接口,实现里面的run方法,他会在容器对象创建号之后执行

    1. package com.bjpowernode;
    2. import com.bjpowernode.service.HelloService;
    3. import org.springframework.boot.CommandLineRunner;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import javax.annotation.Resource;
    7. @SpringBootApplication
    8. public class Application implements CommandLineRunner {
    9. //容器对象创建号之后,会给对象赋值
    10. @Resource //注解自动赋值
    11. private HelloService helloService;
    12. public static void main(String[] args) {
    13. System.out.println("准备创建容器对象");
    14. //创建容器对象
    15. SpringApplication.run(Application.class, args);
    16. System.out.println("创建容器对象之后");
    17. }
    18. @Override
    19. public void run(String... args) throws Exception {
    20. String str=helloService.sayHello("lisi");
    21. System.out.println("调用容器中的对象="+str);
    22. //可做自定义的操作,比如读取文件,数据库等等
    23. System.out.println("在容器对象创建好,执行的方法");
    24. }
    25. }

    运行 

  • 相关阅读:
    java泛型
    MySQL学习(四)
    Ubuntu设设置默认外放和麦克风设备
    AOP——基本概念、底层原理
    MySQL数据库远程访问权限设置
    【每日一题】34. 在排序数组中查找元素的第一个和最后一个位置
    rsync 远程同步
    DNS服务器未响应是什么意思
    压缩pdf文件的大小,pdf档怎么压缩为最小内存
    Hamiton图系列文章 (5) :Hamilton图判定充要条件实现的算法复杂度分析
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125533174