What you'll need:
Eclipse (I use Helios 3.6.7)
Where: www.eclipse.org/downloads/
Maven Dependencies (So that you dont need to download the libraries and let Maven do the job for you)
JBOSS Application server (I'm currently using version 4.2.2)
Mentioned above are just the versions that I use. You can download the latest one, or choose your own server. The focus of this tutorial is Spring MVC.
Note: Make sure that you start your server before running the project. You can do this by opening File > New > Other > Server and select the server you want, in our case choose JBOSS. As a noob myself, sometimes I forget this.
What you'll be doing:
[ ]Controller
[ ]Model
[ ]JSP Page, usually with JSTL
[ ]Run program
First of all, let me discuss what spring MVC means. MVC stands for MODEL VIEW CONTROLLER.
MODEL = Domain Objects
VIEW = JSP page with CSS and HTML
CONTROLLER = Controls which JSP page and MODELS should be displayed
MODEL = Domain Objects
VIEW = JSP page with CSS and HTML
CONTROLLER = Controls which JSP page and MODELS should be displayed
Let's start.
File > New > Project > Other > Spring SourceTool Suite > Spring Template Project > Spring MVC Project
You can see all the files created for you by Eclipse and Maven. And at the bottom of the project, you can see a POM.xml.
Explanation
POM.xml : The pom.xml is generated by Maven. It contains the LIBRARIES needed for you to run the project. The difference between using maven is that YOU DO NOT NEED TO add the libraries manually because MAVEN will do it for you. We would study them one by one in future blog posts. But for now, you can have a look at it first.
/WEB-INF/SPRING: appServlet will be used to process your request.
/WEB-INF/VIEWS: The views folder will contain the JSP pages for your web app. For those who are not familiar with the JSP page, it is similar to HTML but with you can add java functionality.
src/main/java : This folder will contain the controller and other codes for your application.
Step 1. Create a simple contact list that would contain the following fields: Name, Address and Tel. Number.
Go to src/main/java > New > Package
Name: com.contactlist.domain
This package will contain all the "MODEL" or the data. Each class will have setter and getter methods. So let's create ours!
Step 2. Create a class named "Contact" under the package "com.contactlist.domain".
The domain above is actually called the MODEL.
Step 3. Create a class named "ContactListContoller" under the package com.contactlist.controller. Now, this is where SPRING MVC will come in.
@Controller : You need this to indicate that THIS CLASS is a controller that CONTROLS the REQUEST being sent by the user.
@RequestMapping : This means that if you call this mapping, http://localhost:8080/contactlist/display, the method listAll will be called.
Note: Notice that, you are actually returning a STRING, which will be the filename of the JSP Page that you would create to show the contact list.
model.addAttribute("contactList", contactList) : This line actually PUTS the data to the model that would be RETRIEVED later in the JSP page. We will see this in a while.
Step 4. Create the JSP Page and name it "contactPage". All JSP pages should be saved in the /WEB-INF/views folder. So right click on views and choose JSP Page. If you can't find it, go to others and look for JSP page.
Explanation
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - Do not forget to put this on your JSP to be able to show the output. So basically, you can access any data you put on the model by using JSTL tags. In our case, since we used model.addAttribute("contact", contact); in our controller, we accessed it by
<c:out value="${contact.name}"></c:out> where name is a FIELD in the ContactList Domain. Learn more about JSTL tags here: http://www.tutorialspoint.com/jsp/jstl_core_param_tag.htm
So, let's see:
Controller [check]
Model [check]
JSP Page [Check]
and you're done! So now, let's run the program.
Controller [check]
Model [check]
JSP Page [Check]
and you're done! So now, let's run the program.
The complete directory hierarchy:
Voila!
http://localhost:8080/contactlist/display and look what we have here.
Questions that you may have in mind and couldn't find them on the internet? Ask me and I hope I have the answer.
OUTPUT
What is happening?
1. A request is being sent to the servlet as http://localhost:8080/contactlist/display. As you can see from the servet-context.xml file under src/main/WEB-INF/Spring/appServlet, you will find <annotation-driven/>, which means that Spring will look for annotations to find your controller. What are annotations? Annotations are @Controller, @Requestmapping and the like. Earlier versions of spring actually used XML instead of annotations until they introduced it at Spring 2.5. So now, Spring will find a controller with mapping
"/display" for you.
2. Spring finds controller with mapping "/display" and boom! Found it! Then performs the method to which it is mapped, in our case, listAll method. So it adds the model, because we have the line model.addAttribute("contact", contact);
3. return "contactPage". This string then goes back to the servet-context. Now, if you would look at the file, you will see a prefix and suffix. So therefore,
Prefix = "/WEB-INF/views/"
Suffix = ".jsp"
Wait, we have our jsp page on the /WEB-INF/views/ path right? so it means, we are in the VIEW part of the MVC and it actually creates the path for us. So, the servlet will now create the path /WEB-INF/views/contactPage.jsp for your app and voila! There's your list of contacts.
What's next? Noob forms and validation, Android Dev't, IOS Dev't, some struts and grails maybe. Who knows? Anway, if you have questions, just send me a message and I will TRY to answer it. Like what I've said: for the noobs by the NOOB. I'm also a beginner at this.
Have a happy weekend! xoxo.
DOWNLOAD THE WHOLE PROJECT: contactlist.zip
"Sharing knowledge is not about giving people something, or getting something from them. That is only valid for information sharing. Sharing knowledge occurs when people are genuinely interested in helping one another develop new capacities for action; it is about creating learning processes.
Read more: http://www.finestquotes.com/select_quote-category-Knowledge Sharing-page-0.htm#ixzz1r9b47fMz"
0 comments:
Post a Comment