RSS

Dreams of Spring

02 Nov

Episode 21

Going back to my project, which was a bit on hold for some time, I finally some time to to move things forward a bit. Just a bit, as you may recall I made a decision to use Spring as a framework and GWT for view. I’ve worked with both for quite some time, but apparently never tried to do anything from scratch. Let’s fix that and start with Spring.

cropped-logo_springbypivotal_horizontal1-645x300

Inspired by Josh Long’s talk on last Jax conference, I’ve checked the Spring Boot. There is a nice website which asks you for:

  • Few descriptive details of your project
  • If you want to use Maven or Gradle. As I mentioned earlier, I will go with Maven.
  • If you want Jar or War. As usually War is the solution.
  • Java or Groovy. Uhm.
  • Java and Spring Boot version

And then several groups of dependencies you can include in your project, like:

  • AOP and Security
  • Data: JDBC, JPA, MongoDB etc
  • I/O: Batch, JMS etc
  • Web: Web application, Websockets, WebServices…
  • Template engines
  • Social: want Facebook integration out of the box? No problem.

For online hello world you probably only need to check web. After filling in all stuff simply hit Generate Project, download, unzip, and put in your Eclipse workspace. Then import from existing Maven project and here it is. You don’t need to download anything and figuring out where to put this and what to add to some eerie xml files. Maven will handle it for you.

To see something in runtime, just put any html page in src/main/ webapp, Add application to your build-in Tomcat, publish and start. Watch console, If everything went smoothly, you should see server starting in less than a second, and then a lovely Spring Boot ascii artish logo followed by some info outputs over a few seconds. You may now check http://localhost:8080/appname/pagename.html and it should present you the very page you have created earlier. Congratulation, you have now Spring application that does nothing useful but starts twenty times longer than simple dynamic web application! What an achievement. Let’s do something with that.

First let’s see what was generated for us.

  • java, annotated with @Configuration, @ComponentScan and @EnableAutoConfiguration and containing good old main method. You don’t actually need any server to run Spring, you can just run this class as Java Application atop of pure JVM.
  • java – used to bind Spring as http request handler when run on server.
  • Some empty junit test for Application class.
  • pom.xml, Maven project definitions and dependencies

Time to add something. We will create a very simple MVC to display “Hello World!” from the depths of backend.

Our back-end in this case will be class marked with @Component annotation, which allows Spring to manage its injection in the Controller class, annotated with, well, @Controller.

service

Controller has a field which will be initiated with our back-end buddy. It also has a method to handle request, and we will annotate this with @RequestMapping(“/*”) meaning it will catch all http requests to our pretty little application. Now we need to return from the method something that will indicate where the request should land, and that’s the job of ModelAndView object initialized with name of page to display.

To tie back-end and front-end together, we will add a model attribute with value provided from our component.

controler

As quick and dirty view we will create a JSP (sorry for that) page, that will reference model with el expression ${message}. Simple as that.

Publish, restart server, type http://localhost:8080/appname/blah in the browser and here it is, your first Spring MVC web application, sweet and shining.

fb125-how-to-train-your-dragon-2-toothless-cute-art-wallpaper

 
3 Comments

Posted by on November 2, 2014 in BookSentry, Spring, Technology

 

Tags: , , ,

3 responses to “Dreams of Spring

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: