Springboot入門使用實(shí)例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹“Springboot入門使用實(shí)例分析”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Springboot入門使用實(shí)例分析”文章能幫助大家解決問題。

為成縣等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及成縣網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站建設(shè)、成縣網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

入門Springboot

項(xiàng)目創(chuàng)建在IDEA中創(chuàng)建即可。

注意點(diǎn):

1、所有文件都需要放在 :

Application文件的同級(jí)或下級(jí)目錄中

2、application.properties 為 spring-boot 項(xiàng)目主核心配置文件,且只能有一個(gè)核心配置文件。

Springboot入門使用實(shí)例分析

3、多環(huán)境下的核心配置文件的使用, 文件名必須以 application- 開頭!
  application-xxx.properties

Springboot入門使用實(shí)例分析

(1)開發(fā)環(huán)境

# 開發(fā)環(huán)境配置文件
server.port=9000
server.servlet.context-path=/

(2)測試

# 測試環(huán)境配置文件

(3)生產(chǎn)環(huán)境

# 生產(chǎn)環(huán)境配置文件
server.port=7000

在主核心配置文件中激活我們自定義的配置文件:

#激活我們編寫的application-xxx.properties配置文件
spring.profiles.active=dev

4、@Value 注解

spring-boot核心配置文件 自定義的配置屬性,如何獲取
下邊方式只能一個(gè)一個(gè)屬性獲??!
比如:在application.properties文件中自定義了一個(gè)配置 website=http://www.baidu.com
在項(xiàng)目中獲取到這個(gè)自定義的配置:

使用注解 @Value("${website}")

也可以寫一個(gè)默認(rèn)值,如果配置項(xiàng)沒有,會(huì)使用默認(rèn)值@Value("${website: 默認(rèn)值}")

package com.lxc.sprint_boot_01.web;
 
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.management.ValueExp;
import javax.print.DocFlavor;
 
// 聲明控制層
@Controller
public class IndexController {
    @Value("${website:values}")
    private String name; // 此時(shí)website值會(huì)賦給name屬性
 
    @RequestMapping(value = "/self")
    @ResponseBody
    public String self() {
        return name;
    }
}

5、@Component 和 @ConfigurationProperties(prefix="xxx") 注解

spring-boot核心配置文件 將我們自定義的配置屬性,映射為一個(gè)對(duì)象(獲取的是一個(gè)對(duì)象),使用這種方式的前提:配置文件中的屬性必須要寫前綴!

application.properties文件

# 屬性前邊必須要有前綴,我這里前綴是user
user.name=lxc
user.password=123456

 config -> user.java文件

package com.lxc.sprint_boot_01.config;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component // 將此類交給spring容器管理
@ConfigurationProperties(prefix = "user") // 配置屬性注解,參數(shù)前綴必須有值,值為我們定義的前綴
// 配置完上邊的兩個(gè)注解,下邊把配置文件中的屬性映射到下邊類中去
public class User {
    private String username;
    private String password;
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}

調(diào)用屬性

package com.lxc.sprint_boot_01.web;
 
import com.lxc.sprint_boot_01.config.User;
import org.springframework.beans.factory.annotation.Autowired;
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.management.ValueExp;
import javax.print.DocFlavor;
import java.util.HashMap;
import java.util.Map;
 
// 聲明控制層
@Controller
public class IndexController {
    @Autowired // @Autowired 把User類注入進(jìn)來
    private User user;
 
    @RequestMapping(value = "/many")
    @ResponseBody
    public String many() {
        return "user為:"+user.getUsername() + ",密碼為:"+user.getPassword();
    }
 
}

Springboot入門使用實(shí)例分析

6、加上@ConfigurationProperties注解,會(huì)出現(xiàn)上邊紅色警告,想解決此問題需要加一個(gè)依賴包:

Springboot入門使用實(shí)例分析

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

7、如果在application.properties中有中文,會(huì)出現(xiàn)亂碼,在IDEA中解決中文亂碼的問題:

Springboot入門使用實(shí)例分析

8、在配置文件中屬性的鍵值對(duì)不能有空格,否則解析會(huì)有問題!

9、spring-boo集成JSP

首先在main文件夾下創(chuàng)建 webapp文件夾,然后 點(diǎn)擊 file -> project structure-> Modules 如下圖:

Springboot入門使用實(shí)例分析

然后在彈出的對(duì)話框中點(diǎn)擊右邊文件,找到我們剛才創(chuàng)建的webapp文件夾,確定即可,具體如下:

Springboot入門使用實(shí)例分析

 此時(shí),webapp會(huì)變?yōu)槿缦聵幼印?/p>

Springboot入門使用實(shí)例分析

配置pom.xml文件

(1)首先引入spring-boot內(nèi)嵌的tomcat對(duì)jsp的解析依賴,不添加解析不了jsp

<!--引入spring-boot內(nèi)嵌的tomcat對(duì)jsp的解析依賴,不添加解析不了jsp-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

(2)spring-boot默認(rèn)使用的是前端引擎thymeleaf,現(xiàn)在我們要使用springboot繼承jsp,需要手動(dòng) 指定jsp最后編譯的路徑,而且springboot繼承jsp的路徑是springboot規(guī)定好的位置: META-INF/resources

<build>
    <!--spring-boot默認(rèn)使用的是前端引擎thymeleaf,現(xiàn)在我們要使用springboot繼承jsp,需要手動(dòng)指定jsp最后編譯的路徑,而且springboot繼承jsp的路徑是springboot規(guī)定好的位置:META-INF/resources-->
    <resources>
        <resource>
            <!--源文件-->
            <directory>src/main/webapp</directory>
            <!--指定編譯路徑:-->
            <targetPath>META-INF/resources</targetPath>
            <!--指定源文件夾中的哪些資源需要被編譯-->
            <includes>
                <include>*.*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <!-- ··· -->
    </plugins>
</build>

最后一步:在 application.properties 中配置視圖解析器

# 配置視圖解析器
spring.mvc.view.prefix=/ # 前綴
spring.mvc.view.suffix=.jsp # 后綴

創(chuàng)建.jsp頁面,測試:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <h2>${msg}</h2>
</body>
</html>
package com.lxc.boot_02;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class controller {
    // 寫法一:
    @RequestMapping(value="/say")
    public ModelAndView say() {
        ModelAndView mv = new ModelAndView();
        // 給視圖傳值
        mv.addObject("msg", "hello");
        // 設(shè)置 最終視圖的名稱
        mv.setViewName("say");
        return mv;
    }
 
    // 寫法二:把視圖和模型拆分開,返回一個(gè)視圖(return的是視圖的名字)
    @RequestMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("msg", "lxc;");
        return "say";
    }
}

 寫法一:

Springboot入門使用實(shí)例分析

寫法二:

Springboot入門使用實(shí)例分析

關(guān)于“Springboot入門使用實(shí)例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

當(dāng)前文章:Springboot入門使用實(shí)例分析-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.muchs.cn/article26/coigjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)虛擬主機(jī)、企業(yè)網(wǎng)站制作、網(wǎng)站改版、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化