Saturday, September 20, 2014

Spring MVC Step 1


So I've been digging around Spring. It's a very versatile framework. So you might need to research which parts you actually need.

I'm into Java EE Web so I want the core-part and the web-mvc (yes they actually have a subproject tailored to MVC on the web).

So I went and found the easiest tutorial I could find. Some tutorials are great but they give you like 50 classes from the start. If you'r trying to learn Spring then you might want to know if all the classes are needed or not.

But I found one that was small with good explanations (http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-hello-world-example/)


So I will first show my setup (I use Gradle instead of Maven like the tutorial uses).

Folder-Structure

Project
--src
----main
----test
--build.gradle

The src just hade a package structure for one class, the HelloWorldController.java
Right now the test-folder is empty, it will be used for future junit-tests.

build.gradle:

The rest is identical to the tutorial. If you want to run it in a Wildfly-server (the tutorial uses tomcat), you just run "gradle assemble" and either copy the war to the deployment-folder or you can use the web-managment and deploy it there.

So now some explanation of how it all binds together:

DispatcherServlet

The most central part is the DispatcherServlet. This is a servlet that Spring have created to mediate all the traffic. The http-request come to dispatcher and it then uses the RequestMappings from the controller-classes to retrieve the right view and model.

It reminds me a bit of the FacesServlet that all the facelets with JSF goes through.

So in web.xml you define the DispatcherSevlet just like you would do any (before the Annotation-trend started) and map it to the "/" url-pattern.

When you define the DispatcherServlet you may pick a name for it. It will then look for this name as a xml file: "name-servlet.xml" in WEB-INF.

So what would we set up in this xml? How the dispatcher will resolve the connections.
You can define all the components one by one, do a component-scan or a component-scan via Annotations (not in the xml).

In the tutorial (which I also use) they defined a bean, in this cause a bean of "org.springframework.web.servlet.view.InternalResourceViewResolver". This will be used to resolve the views.

In it we set some properties:
prefix: the folder where the views are.
suffix: .jsp

This makes it easy for the classes. They can return a String "hello" and it can be resolved to /WEB-INF/views/hello.jsp". This makes the Controller classes more readable.

Then we need a Controller to actually do anything. I put a "index.jsp" that just says welcome. It shows when the "/" url is requested. My web-app folder structure looks like:



Controller

A controller is a class with @Controller annotation. It is likely that you want the @RequestMapping("/hello") also. 

You can then do a method that returns a String. This method should have the @RequestMapping(method = RequestMethod.GET) annotation. This only registers so that when a request is used via GET, this method is returned. 

This means that you can have seperate methods for the others ex. POST

You can now return "hello" or "helloWorld" depending on what you named your jsp-file.

Conclusion:

We need:
A web.xml which configs the DispatcherServlet
A dispatcher-servlet.xml which configs the view-resolver.
A Controller

The dispatcher will register all the mappings from the controllers and direct the traffic.
The controller will return the name of the view to the view-resolver which puts prefix and suffix on it.

If you put your views in the WEB-INF users can't directly request them, they have to use the url-mappings of the controllers, which is a very good thing.


(EDIT: and after the ViewResolver sends it back to the dispatcher the dispatcher sends the html to the user that requested it)


So that's the first step in Spring Web MVC. I find Spring very interesting and will continue to learn and explore. I will continue to use Wildfly and Gradle. One of the things I want to do is an automatic deployment without a IDE. You can do this with Cargo-plugin but I want to find a different way.

Happy Coding!



Sunday, September 14, 2014

Early Spring

I'm almost done with my first real Java EE project (JSF, JPA, Wildfly) and its currently running on a raspberry pi.

My next project will be learning the Spring Framework.
I will also try to do some post about how to start with Spring.

Initial impression:

My first impression of Spring is that its a well made framework that fills every part of your development (specific test-suite and more). The different modules of the framework really gives you a good feeling of security for functions.

But there is a big step for a newbie to just jump into Spring!

First problem: Overview
What does what and how "do they want you to code". With pure Java EE you can do as you want,
but in Spring there is a certain way "they" want you to code. I'm not saying it's bad. But there is a problem of learning it.

Second problem: Too big tutorials
Many of the tutorials start to big with 20+ ready to use classes. And the tutorial uses these classes. This makes you feel like you jump into the middle of things and not understand the whole process.

Third problem: Spring Boot
Lovely little thing that really can kickstart your project, but it doesn't help you in understanding much.

Fourth problem: Name usage
Some of the names conflicts with pure Java EE7 names. A Spring Bean is configured differently then a regular Bean that can be Dependency Injected.
Dependency Injection in Spring is different then Pure Java EE7. EE7 has @Inject while Spring uses @Autowired that usally is a Interface that a config-class or a xml has mapped to a implementation class.


So that's some of the hurdles a rookie will deal with.

On of the links I've found useful (haven't had time to read to much yet) is: springbyexample.org/examples/core-concepts.htm

This site ACTUALLY explains the core concepts.

So when I have some more information about the core concepts that are essential I will make a post about it.

Happy Coding!