首页 > 图灵资讯 > 技术篇>正文

springboot的配置环境和自定义配置以及jsp的使用

2023-05-31 09:19:58

1.Springboot核心配置文件16

SpringBoot的核心配置文件用于配置SpringBoot程序,名称必须从application开始

扩展名有:properties(k=v);yml(k:v)

使用application.properties,application.yml

公式properties文件的默认使用

2.properties文件(默认使用此文件)17

例1:application.properties设置端口和上下文

修改application.properties配置文件,修改默认tomcat端口号和项目上下文件根

properties属性文件配置方法

application.properties

#设置端口号serverer.port=8082#设置访问应用上下文路径, contextpathserver.servlet.context-path=/myboot

BootController

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;// properties 文件(默认使用文件)   16@Controllerpublic class BootController {    @RequestMapping("/hello")    @ResponseBody////将dosome方法的返回值作为数据    public String doSome(){        return "hello SpringBoot应用程序 ,端口8082上下文路径设置 /myboot ";    }}

MyApplication

package com.bjpowernode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MyApplication {    public static void main(String[] args) {        SpringApplication.run(MyApplication.class,args);    }}

变成了访问路径http://localhost:8082/myboot/hello

springboot的配置环境和自定义配置以及jsp的使用_jsp

3.yml配置文件18

yml是yaml格式的配置文件,主要采用一定的空格、换行等格式进行配置。

yaml是一种直观的数据序列格式,可以被计算机识别,容易被人类阅读。yaml类

类似xml,但语法比xml简单得多,值必须与之前的冒号配置项有一个空间,yml

也可以使用yaml后缀

注:当两种格式配置文件同时存在时,yml配置文件从SpringBoot2.4开始.

所有修改的配置名称都是application。

建议使用yml格式配置文件

server:  port: 8083  servlet:    context-path: /myboot2

BootController

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;// properties 文件(默认使用文件)   16@Controllerpublic class BootController {    @RequestMapping("/hello")    @ResponseBody////将dosome方法的返回值作为数据    public String doSome(){        return "hello SpringBoot应用程序 ,端口8082上下文路径设置 /myboot ";    }}

MyApplication

package com.bjpowernode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MyApplication {    public static void main(String[] args) {        SpringApplication.run(MyApplication.class,args);    }}

重新运行Application,查看启动端口和上下文根

访问路径http://localhost:8083/mybooot2/hello

springboot的配置环境和自定义配置以及jsp的使用_配置文件_02

4.多环境配置19

代码写在course3_1中

在实际开发过程中,我们的项目将经历许多阶段(开发)->测试->在线),每个阶段的配置都会有所不同,如端口、上下文根、数据库等。此时,SpringBoot为不同环境之间的切换提供了多环境配置

为每个环境创建配置文件,必须以application-环境名称命名.properties|yml

例如

创建开发环境的配置文件:application-dev.properties(application-dev.yml)

创建测试人员使用的配置:application-test.properties

4.1具体如下19-21

springboot的配置环境和自定义配置以及jsp的使用_spring_03

application-dev.yml

#开发环境配置文件  19server:  port: 8081  servlet:    context-path: /mydev

application-online.yml

#项目在线使用的配置文件   19server:  port: 9002  servlet:    context-path: /myonline

application-test.yml

#配置文件用于测试   19server:  port: 9001  servlet:    context-path: /mytest

application.在yml文件中调用指定的环境

application.yml

#激活dev环境  20#spring:#  profiles:#    active: dev#激活test环境  20spring:  profiles:    active: test

BootController

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;//多环境配置  19@Controllerpublic class BootController {    @RequestMapping("/hello")    @ResponseBody    public String doSome(){        return “Springbot多环境配置”;    }}

Application

package com.bjpowernode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//多环境配置  19@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

springboot的配置环境和自定义配置以及jsp的使用_spring_04

5.Springboot自定义配置22

在SpringBoot的核心配置文件中,除了使用内置配置项外,我们还可以自定义配置

然后使用以下注释来读取配置的属性值

6.@Value注释226.1阅读配置文件数据22

application.properties

#配置端口号   22server.port=8082#context-pathserver.servlet.context-path=/myboot#定制key-valueschool.name=动力节点http://school.website=www.bjpowernode.comschool.address=http,北京大兴区://site=www.bjpowernode.com

HelloController

package com.bjpowernode.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;////读取配置文件中的数据  22@Controllerpublic class HelloController {    @Value("${server.port}")    private Integer port;    @Value("${server.servlet.context-path}")    private String contextPath;    @Value("${school.name}")    private String name;    @Value("${site}")    private String site;    @RequestMapping("/data")    @ResponseBody    public String  queryData(){        return name+",site="+site+", 项目访问地址=“+contextPath+使用的端口="+port;    }}

Application

package com.bjpowernode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

启动Application应用,访问浏览器

springboot的配置环境和自定义配置以及jsp的使用_java_05

7.@configurationProperties23

将整个文件映射成自定义配置项较多的对象

7.1案例演示

在com.bjpowernode.vo包下创建Scholinfo类,并为此类添加Component和ConfigurationProperties注释。prefix不能指定。如果没有,它将被指定在配置文件中找到与此类属性名一致的配置,prefix的作用可以区分同名配置

application.properties

#定制key-valueschool.name=动力节点http://school.website=www.bjpowernode.comschool.address=北京大兴区

package com.bjpowernode.controller;import com.bjpowernode.vo.SchoolInfo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;////读取配置文件中的数据  22@Controllerpublic class HelloController {    @Resource///自动注入  23    private SchoolInfo info;    @RequestMapping("/info")    @ResponseBody    public String  queryInfo(){        return “ScholInfo对象=”+info.toString();    }}

springboot的配置环境和自定义配置以及jsp的使用_jsp_06

7.2解决警告23

ConfigurationProperties注释用于Scholinfo类别,IDEA会发出警告,不会影响程序的执行

springboot的配置环境和自定义配置以及jsp的使用_jsp_07

点击opendocumentnation跳转到网页,提示需要在网页上添加依赖。我们将这种依赖复制并粘贴到pomention.xml文件中

springboot的配置环境和自定义配置以及jsp的使用_配置文件_08

pom.以下依赖于xml文件中的以下依赖

springboot的配置环境和自定义配置以及jsp的使用_java_09

<!--        处理 @configurationProperties注释 元数据  23-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <optional>true</optional>        </dependency>

3.3中文乱码

SpringBoot核心配置文件中有中文信息的,会出现乱码:

◼一般不建议在配置文件中出现中文(注释除外)

◼如果有,可以先转换为ASCII码

springboot的配置环境和自定义配置以及jsp的使用_jsp_10

4.jsp244在springbot中使用

SpringBoot不建议使用jsp,而是使用模板技术代替jsp

4.1使用jsp需要配置:25

代码写在course4_1

加入对jsp的依赖。负责编译jsp文件

<!--处理jsp依赖--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency>

假如需要使用servlet,jsp,jstl的功能

<dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version></dependency>

创建一个存储jsp的目录,通常称为webapp

如果右键在webapp目录中,没有创建jsp的选项,可以在projectstructure中指定webapp

Webresourcedirectory

springboot的配置环境和自定义配置以及jsp的使用_jsp_11

index.jsp

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

pom需要.编译jsp文件后,xml指定存储目录。

<!--指定jsp编译的存储目录  25-->        <resources>            <resource>                <!--jsp原始目录-->                <directory>src/main/webapp</directory>                <!--指定编译的存储目录-->                <targetPath>META-INF/resources</targetPath>                <!--指定处理的目录和文件-->                <includes>                    <include>**/*.*</include>                </includes>            </resource>

META-INF/resources

创建Controller,访问jsp

package com.bjpowernode.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;///在springboot中使用jsp  25@Controllerpublic class JspController {    /*public String doJsp(HttpServletRequest request){        request.setAttribute("data",“Springboot使用Jsp”;        //视图的逻辑名称        return "index";    }*/    ///上面的dojsp也可以写成以下内容  25    /**     * ModelAndView:     * @param model     * @return     */    @RequestMapping("/myjsp")    public String doJsp(Model model){        ///将数据放入request作用域        //以下两种写法等价        model.addAttribute("data",“Springboot使用Jsp”;        //request.setAttribute("data",“Springboot使用Jsp”;        //视图的逻辑名称        return "index";    }}

application.视图解析器配置在propertis文件中

#配置端口号  25server.port=9090server.servlet.context-path=/myboot#视图分析器的配置   25# prefix=/ 中的 / = src/main/webapphttp://spring.mvc.view.prefix=/spring.mvc.view.suffix=.jsp

测试

springboot的配置环境和自定义配置以及jsp的使用_java_12

上一篇 java编程基础之抽象类
下一篇 springmvc注解式开发

文章素材均来源于网络,如有侵权,请联系管理员删除。