SpringBoot(二):属性值在配置文件中+SpringBoot使用jsp步骤

1、属性值在配置文件中

在springboot的配置文件中加入:

变量名1=值

在java代码中赋值时候在属性名上面加入 @Values标签,如下所示:

@Value("${变量名1}")
    private String schoolName;

如果要给一个类中的各个属性赋值,则需要在配置文件中加入
名称.属性1,名称.属性2等等,例如在配置文件中加入如下值:

school.name=peking university
school.website=https:www.peking university.com

在类的声明上面加入@ConfigurationProperties注解

@Component//将此类交给spring的声明周期进行管理
@ConfigurationProperties(prefix="school")
public class School {
    private String name;
    private String website;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }
}

2.SpringBoot使用jsp步骤

第一步:在pom文件中加入依赖包

 <!--引入springboot内嵌toncat对jsp的解析包,不添加不中-->
        <!--仅仅只是展示jsp界面,只添加一下一个依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

第二步:在pom文件中指定jsp编译路径

 <!--springboot项目默认使用的前端引擎是thymeleaf
        现在我们要使用springboot集成jsp,手动指定jsp最后编译的路径
        而且,springboot集成jsp编译的路径是springboot规定好的位置
        META-INF/resources-->
        <resources>
            <resource>
                <!--源文件夹-->
                <directory>src/main/webapp</directory>
                <!--指定编译到META-INF/resources-->
                <targetPath>META-INF/resources</targetPath>
                <!--指定源文件夹中的哪个资源要编译进去-->
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>

第三步:手动创建并指定webapp目录,将jsp文件放入webapp下面
第四步:创建视图解析器
在SpringBoot配置文件中加入:

#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

猜你喜欢

转载自blog.csdn.net/qq_41984117/article/details/110941619
今日推荐