總是說萬事開頭難,還是真的有點難,很多年前就說一定要開始謝謝博客,每次到打開博客的時候總是不知道怎麼下手,每次看到別人寫的博客,有批評有讚揚,看到寫得好的博客的時候,感覺自己跟他一樣,有一種一覽衆山小的感覺。
今天就用一個非常簡答的程序開始我的第一個博客吧,有不足指出還請大家見諒,本文章要分享的是spring boot的入門程序。
第一步:準備環境
1、安裝配置gradle,這個網上有很多介紹的,這裏就不多囉嗦,提供一個下載的網站,Gradle 的官方網站。下載完後,一定要配置好電腦的環境變量,這裏有介紹。
2、配置eclipse的gradle環境,看這裏。
第二步:開始創建工程。
1、創建工程,點擊eclipse的file新建project選擇gradle,下面是我創建的工程目錄圖。
2、 配置build.gradle文件,主要是配置aliyun的私服, build.gradle的配置文件如下所示:
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
//mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
//mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web') //必備
compile('org.springframework.boot:spring-boot-starter-thymeleaf') //必備
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
compile('org.springframework.boot:spring-boot-starter-test')
compile('org.springframework.boot:spring-boot-starter')
}
3、編寫一個啓動程序
@SpringBootApplication
@ComponentScan(basePackages="com.springboot.demo")
public class StartUp {
public static void main(String[] args) {
SpringApplication.run(StartUp.class, args);
}
}
注意@ComponentScan,如果其他程序和啓動類在同一包或者子包,就不需要這個,如果在外包,就需要使用這個指定一下。
4、編寫控制類和業務類
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/")
public String index() {
return "login";
}
@RequestMapping(value = "/login",method=RequestMethod.POST)
public String login(Model model,
@RequestParam(value="username")String username,
@RequestParam(value="password")String password) {
String result = testService.checkUser(username, password);
if(result.equals("success")) return "success";
else {
model.addAttribute("result", "用戶名錯誤或者密碼錯誤!");
return "login";
}
}
}
@Service
public class TestService {
public String checkUser(String username,String password) {
if(username.equals("helloword") && password.equals("123")) {
return "success";
}else return "error";
}
}
5、編寫幾個前端html頁面
login.html
<body>
<div class="login">
<h1>Login</h1>
<form method="post" action="/login">
<input type="text" name="username" placeholder="用戶名" required="required" />
<input type="password" name="password" placeholder="密碼" required="required" />
<button type="submit" onclick="" class="btn btn-primary btn-block btn-large">登錄</button>
</form>
<div th:text="${result}" align="center" style="color: red;"></div>
</div>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<h1 align="center">you login success!</h1>
</body>
</html>
這個前段頁面沒有全部粘貼過來,太長了,我提供了源代碼下載,大家有興趣的可以去看原代碼。
寫到這裏,這個簡單的springboot就差不多完了,只需要啓動startUp的main函數就可以啓動web應用程序了,這是輕量級的tomcat.
第三步:登陸訪問。
登陸http://localhost:8080。源代碼程序地址https://download.csdn.net/download/luoxuepeng/10479807,這個好像必須使用C幣,我在上傳的時候至少要選擇1個C幣,沒辦法免費下載。
第四步:結束。
學射箭,你得去拉弓,整天只擺造型肯定不行;學游泳,你得下水撲騰,整天在岸上做模仿活動不行;學開車,你得坐車上去開,坐沙發上肯定學不會。同樣的道理,學寫博客,你得試着去寫,學習spring boot,你得自己親自寫一次,看看是沒用了。