欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springboot整合(1)——springboot基础配置

程序员文章站 2022-03-01 20:34:39
...

Springboot整合(1)——springboot基础配置

1. pom文件中添加依赖,此处贴出pom文件,注释对每个节点做了解释

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>tech.luoyu.product</groupId>

    <artifactId>KnowledgeIsland</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>war</packaging>

    <name>KnowledgeIsland</name>

    <description>KnowledgeIsland</description>

 

    <!-- 引用parent,则后面用到的所有springboot相关的依赖不需要写版本号 -->

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.8.RELEASE</version>

    </parent>

    <dependencies>

        <!-- springboot包,web -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

       

        <!-- jasper -->

        <dependency>

            <groupId>org.apache.tomcat.embed</groupId>

            <artifactId>tomcat-embed-jasper</artifactId>

            <scope>provided</scope>

        </dependency>

 

        <!-- jsp中使用jstl标签 -->

        <dependency>

            <groupId>jstl</groupId>

            <artifactId>jstl</artifactId>

            <version>1.2</version>

        </dependency>

    </dependencies>

 

    <build>

        <finalName>KnowledgeIsland</finalName>

        <plugins>

            <!--springloaded hot deploy使用此插件,可让项目运行时支持热部署,运行方法:run as —— maven build —— spring-boot:run -->

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <dependencies>

                    <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>springloaded</artifactId>

                        <version>1.2.4.RELEASE</version>

                    </dependency>

                </dependencies>

                <executions>

                    <execution>

                        <goals>

                            <goal>repackage</goal>

                        </goals>

                        <configuration>

                            <classifier>exec</classifier>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

            <!-- 配置maven远吗编译版本 -->

            <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                    <source>1.8</source>

                    <target>1.8</target>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>

2. resources目录下创建springboot配置文件application.yml,其内容如下

server:

  port: 8088

  context-path: /KnowledgeIsland

  ##不加下面的配置的话jsp修改了无法立刻生效

  jsp-servlet:

    init-parameters:

      development: true

注:视图控制器也可在配置文件里配置,配置方法为:

spring:

  view:

    prefix: /WEB-INF/jsp/

    suffix: .jsp

但不推荐,因为在这里配置会有一个小问题: controller中的requestMapping里配置的路径不能和返回的view路径相同,否则就会报Circular view path 错误;推荐在@Configuration配置类里进行配置

3. 创建Configuration配置类,springboot最大的好处就是精简配置,写java代码就可以完成配置。当前配置类只需要配置视图处理器即可。

@Configuration

publicclass MyWebAppConfigurer extends WebMvcConfigurerAdapter {

 

    /**

     * 视图处理器

     */

    @Bean

    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/jsp/");

        viewResolver.setSuffix(".jsp");

        returnviewResolver;

    }

}

注:springboot在启动的时候会自动扫描所有带有Configuration注解的类,完成对容器的配置。

4. Springboot启动类

@SpringBootApplication// @SpringBootApplication=@Configuration+@ComponentScan+@EnableAutoConfiguration

publicclass Application {

 

    publicstaticvoid main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

5. 创建IndexController

@Controller

publicclass IndexController {

 

    @RequestMapping(value = { "", "index" })

    public ModelAndView index() {

        Map<String, Object> model = new HashMap<String, Object>();

        model.put("message", "Welcome!");

        returnnew ModelAndView("index");

    }

}

6. 创建index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>index</title>

</head>

<body>

${message}

</body>

</html>

注:这几个文件的目录结构可参考下图


Springboot整合(1)——springboot基础配置
            
    
    博客分类: 框架整合
 

 

 

7. 运行application.java, 打开浏览器访问http://localhost:8088/KnowledgeIsland/,完成


Springboot整合(1)——springboot基础配置
            
    
    博客分类: 框架整合
 

  • Springboot整合(1)——springboot基础配置
            
    
    博客分类: 框架整合
  • 大小: 25.5 KB
  • Springboot整合(1)——springboot基础配置
            
    
    博客分类: 框架整合
  • 大小: 17.6 KB