SpringBoot+gradle的第一个例子

        总是说万事开头难,还是真的有点难,很多年前就说一定要开始谢谢博客,每次到打开博客的时候总是不知道怎么下手,每次看到别人写的博客,有批评有赞扬,看到写得好的博客的时候,感觉自己跟他一样,有一种一览众山小的感觉。

        今天就用一个非常简答的程序开始我的第一个博客吧,有不足指出还请大家见谅,本文章要分享的是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" οnclick="" 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,你得自己亲自写一次,看看是没用了。 


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十二月的雪7

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值