溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Spring Boot如何配置maven文件

發(fā)布時(shí)間:2020-11-24 15:29:06 來(lái)源:億速云 閱讀:204 作者:Leah 欄目:編程語(yǔ)言

使用Spring Boot如何配置maven文件?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

1.配置maven文件pom.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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>com.hdwang</groupId>
  <artifactId>spring-boot-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>spring-boot-test</name>
  <description>project for test Spring Boot</description>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>
  <!-- Inherit defaults from Spring Boot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
  </parent>
  <dependencies>
    <!-- Add typical dependencies for a web application -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <!-- auto redeploy -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  <!-- Package as an executable jar -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

2.文件結(jié)構(gòu)(static/templates/application.properties/logback.xml的名稱都是約定好了的,只可以使用某幾個(gè)名稱,具體參考spring boot官方文檔,下面的名稱是其中一種配置方式)

使用Spring Boot如何配置maven文件

3.建立啟動(dòng)類(放在頂層,子層(下級(jí)文件夾)的類方可被掃描注入)

@SpringBootApplication
public class Application {
  /**
   * main function
   * @param args params
   */
  public static void main(String[] args){
    SpringApplication.run(Application.class,args);
  }
}

 4.建立controller(在Application類的下級(jí)目錄中)

@Controller
@RequestMapping("/common")
public class Common {
  @Value("${msg:Welcome!}")
  private String msg;
  /**
   * get a page
   * @return a page with name called return value
   */
  @RequestMapping("login")
  public String getLoginPage(ModelMap map){
    map.put("welcomeMsg",this.msg);
    return "login";
  }
}

5.建立網(wǎng)頁(yè)模板login.ftl(freemarker必須使用ftl后綴,被這個(gè)坑了好久!js/css啥的都放在相應(yīng)文件夾下,注意訪問(wèn)路徑中不帶/static,也被這個(gè)坑了好久!)

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>login</title>
  <link href="/css/home.css" rel="external nofollow" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="/js/jquery-2.0.3.min.js"></script>
  <script type="text/javascript" src="/js/home.js"></script>
</head>
<body>
<h2>login page</h2>
<h3>${welcomeMsg}</h3>
<form>
  <div>
   <label>用戶名:<input type="text" id="username"/></label>
  </div>
  <div>
   <label>密碼:<input type="password"/></label>
  </div>
  <div>
    <input type="submit" value="提交"/>
    <input type="reset" value="重置" />
  </div>
</form>
</body>
</html>

6.應(yīng)用配置文件編寫(xiě)

新建application.properties文件并添加以下內(nèi)容

msg=Ladies and gentleman,Welcome!

7.啟動(dòng)運(yùn)行

瀏覽器中訪問(wèn):http://localhost:8080/common/login

使用Spring Boot如何配置maven文件

8.部署

mvn package 打個(gè)包

java -jar xxx.jar 運(yùn)行這個(gè)包即可  

看完上述內(nèi)容,你們掌握使用Spring Boot如何配置maven文件的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI