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
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值