Meet Spark10:49 with Craig Dennis
Spark is a Micro-Framework that’s sole purpose is to get you up and running and rapidly developing web applications. After we discuss the project for the course, let’s go get it installed. It’s a breeze!
Project Directory Fix
The 'Create directories for empty content roots automatically' option has moved. Once you've finished creating the project, you can set that property by doing the following:
- Use CTRL+SHIFT+A to bring up the Action Search menu and type 'gradle'.
- Choose the option that says 'Gradle' on the left and 'Settings' on the right.
- Check the box for 'Create directories for empty content roots automatically' and hit OK.
I'm super excited to introduce you to the web 0:00 application framework that we're gonna be using for the rest of this course, Spark. 0:03 Now Spark is unfortunately in a bit of name space collision in the Java world. 0:07 The Spark framework is not to be confused with the database called Apache Spark. 0:12 It's unfortunate that they share the same name and 0:18 it makes searching around a little confusing at times. 0:20 So, keep this in mind as you start navigating these parts. 0:23 The idea behind a microframework is to keep things small, simple, and 0:27 clear and Spark does just that. 0:31 It is built for productivity and design to get you up and running and 0:33 ready for rapid application development in no time. 0:37 Part of the complaint of Web frameworks in general, 0:42 is that the more sophisticated they get, the more they bloat and swell. 0:44 There's usually tons of configuration, and 0:48 you have to learn a whole new environment before you can even write a simple page. 0:50 Well microframeworks are not like that. 0:54 In the Ruby programming world, 0:56 a super tiny and easy to use framework was created. 0:58 It's called Sinatra. 1:00 Now Spark and many other microframeworks got it's inspiration from Sinatra so 1:02 you might hear it being called Sinatra based. 1:06 So get ready to start spreading the news because this is awesome. 1:09 Okay, so before we get this framework installed, 1:14 let's talk a bit about the project for this course. 1:17 So the actual idea for 1:20 the creation of this course came from one of your fellow classmates. 1:21 As you probably know, we always ask you for feedback and ideas for future courses. 1:24 I usually get a direct message on Twitter, something over email, and 1:29 we put these suggestions out for you to vote on. 1:32 So one of your classmates shot me an email saying, I'd love to learn Spark so 1:35 I can get a site up and running quickly. 1:38 And I thought that sounded like a good idea. 1:40 And I turned that into a request, and into something you could vote on. 1:42 So, check the teacher's notes for more on the current voting system. 1:45 And you all voted it up and 1:49 we agreed that it made great sense to have in our library, so here it is. 1:50 Well that got me thinking, why don't we remove me, as the middle man and 1:54 just have a place where you students can submit ideas yourself. 1:59 Why don't we just build an app? 2:02 And I thought, that's a pretty simple web app. 2:03 And then I was like, why not just use one of those microframeworks. 2:05 And then I was like, I'm writing a course on Spark and I need a project and then. 2:09 I was like, wow sparkception. 2:15 So the project for this course, let's make a site that takes ideas from students and 2:18 then allows voting. 2:23 Sound good? 2:24 Let's go get Spark installed. 2:25 Alright, so 2:27 let's create a new project in IntelliJ IDEA and we'll make a great old project. 2:28 So do create new project And we'll choose Gradle over here on the left, and 2:33 if Gradle's new to you, check the teacher's notes for a quick intro to it. 2:38 Now we're going to use it because we're going to use Spark as a dependency. 2:41 So if we were going to publish this, we'd want something like this for the Groupid. 2:46 We'd want com Team Treehouse the reverse, and we'll just call this Courses. 2:52 And for the artifact ID we want something like Course Ideas. 3:00 Remember, this is what the published jar would be named. 3:05 Let's go ahead and choose Next. 3:08 And let's choose create directories for 3:11 the empty content roots automatically that way it gonna be there. 3:13 Awesome. 3:18 Okay. 3:23 So now the file, directories all there, let's open up the source. 3:24 Main, Java And let's add a new class here. 3:28 New java class, and the class name is, 3:33 let's do come dot team treehouse .courses and for 3:37 simplicity's sake let's just call it me. 3:41 Okay. 3:47 And in this file we'll make our standard main call. 3:48 I want you to see how simple Spark actually is 3:53 to get the prototype up and running. 3:56 So we'll do the public static void main and press tab, it builds it out for us. 3:57 Okay, so now we're ready. 4:02 So first let's add our dependency. 4:04 So the site's called sparkjava.com, I have it open over here. 4:06 So sparkjava.com, and up here there's this Download tab. 4:10 So over here it tells us how to get it. 4:14 So the group ID is com.sparkjava, and then it's spark-core and 2.3. 4:16 So let's go to build.gradle And in the dependencies section here, 4:22 we'll say compile. 4:27 And we'll use the shorthand, we'll say, 4:28 com.sparkjava, and then spark-core, and then 2.3. 4:32 Let's go ahead and click apply the suggestion there that way when we ship 4:43 this project, if we ever do, Gradle will be shipped with it. 4:48 Okay, so let's flip over to the Gradle tool window, and 4:53 in here we'll press this refresh all Gradle projects. 4:56 And bam, just like that, we have Spark and all of its dependencies. 5:04 So here's Spark and here's Jetty. 5:09 And Jetty is the embedded web server that's used. 5:12 So, what do you say we flip back to the documentation and 5:15 take a quick look at the demo under the documentation? 5:18 So let's switch back. 5:20 Let's go over here to documentation. 5:22 And it says right here this is what we wanna do. 5:25 Let's just grab this line because basically we have that same thing in our 5:27 file, right. 5:30 So I'm just gonna grab this and I'm gonna flip back over and 5:31 in our main Java we'll add that line. 5:35 And let's see what happens. 5:37 Let's close this Gradle project, get it out of the way. 5:40 Okay. 5:46 So what does it say here? 5:50 It says, we need to do the static import of Spark.get. 5:52 See it saw that. 5:56 So again it's statically importing, 5:58 there is a get method here and it's off of the spark class. 5:59 Cool, if it doesn't like your lambda syntax, by default, 6:05 some of the ides are set to 6 by default. 6:10 But go ahead and set it to 8. 6:13 Spark really shines when we're using the lambda syntax. 6:15 And if lambda syntax is new to you, check the teacher's notes. 6:18 But basically what it's saying here is 6:21 called the static method get with the path name /hello. 6:26 Anything that's matching /hello, and then it takes a lambda. 6:29 In the lambda here, there's two parameters that are taken. 6:34 It's the Rec which is request and res which is response. 6:37 And then it returns, 6:42 remember the [INAUDIBLE] the first thing here returns hello world. 6:44 So, this is a function that will return hello world and 6:49 its passed a request and a response. 6:51 This is, what this lambda here is know as a route. 6:54 It's actually a functional interface for the route object. 6:57 So what this means is that when an http get request is made to our web server 7:01 that is looking for the uri or the uniform resource identifier of slash hello. 7:07 Our function will be called and hello world will be returned. 7:14 Now we'll touch a bit more on these requests and 7:17 response variables in upcoming videos, but for now let's just return that text. 7:20 Could that really be all it took to get things up and running? 7:24 Well, let's give it a go, what do you say? 7:28 So, let's choose run and we'll choose Run for the menu here, we will choose Main. 7:30 Okay, so it says that Spark has ignited and 7:40 it says that it's listening on port 0.0.0:4567, 7:43 so 0.0.0 is a code name for all IP addresses on your current machine. 7:48 So you can access that URL in the URL bar of your browser, 7:55 or you could just use another nickname, which is localhost. 8:00 So let's do that. 8:02 So let's switch over to a new window here, and we'll say localhost. 8:03 Well, actually, first let's say that we're gonna talk HTTP to localhost 8:09 and I'm going to do a colon and that's for the port which was port 4567. 8:16 Now by default it listens on a different port but we're specifically saying this. 8:22 By default, you don't need to specify the port. 8:25 The port is 80 but we're specifying here 4567 and that's how you specify the port. 8:28 Okay. So then, let's look for /hello. 8:33 Sound good, right? 8:37 Whoa, look at that. 8:40 A website. 8:41 So, wait a second. 8:43 All I need to do if I want another page is just add a new route, right? 8:45 So I know that we want a page when someone just comes to our site. 8:49 So that path is just /, right? 8:51 So let's do that. 8:54 Let's add Over here we'll just come in and say get. 8:55 If anybody comes to just slash then we add, 8:58 again, we'll add a request and a response. 9:04 And let's make it say welcome students. 9:10 Cool, awesome. 9:17 And I save it, and if I come back over here and I just do the slash, that should. 9:18 What? 9:25 You know what? 9:27 I forgot to restart the server. 9:29 The server is running. 9:33 So there's this button here. 9:34 This restarts. 9:35 This will stop and re-run main. 9:35 So notice that main is still running. 9:37 So let's stop it and re-run it and now let's go see if the slash is there. 9:39 Welcome students. 9:47 There it is. 9:48 Woah. 9:48 Bam. 9:50 You just built a web application. 9:50 It's for sure gonna get more challenging, but 9:52 you just tackled that in like five minutes or something. 9:54 That's pretty amazing right? 9:57 Micro-frameworks for the win. 9:58 Also, did you see how clear that syntax was? 10:00 Doesn't get much more clearer than that, does it? 10:03 But forgetting to restart the server is going to bite you, I promise it will. 10:06 You'll make a change and you won't see it and you change it some more and 10:10 you'll still not see it and then do a facepalm like this. 10:13 And you'll realize that it's because you forgot to reset the server. 10:16 It's actually a good thing to remember that the server's constantly running, 10:19 just waiting for any number of requests to come in from wherever. 10:23 Even though our request response loop for that one request was finished, 10:26 the server's still just sitting there waiting to respond to the next request. 10:29 Well, let's see if we can make this app a little more dynamic. 10:33 Why don't we make a site that's more personal. 10:36 Welcome, students isn't exactly as personable as we can make it. 10:39 Why don't we prompt for the user's name? 10:43 Actually I think we have a little work on making things look a little bit prettier. 10:45
You need to sign up for Treehouse in order to download course files.Sign up