Tutorial: step by step Helloworld with Spring MVC (part 1)

Friday, November 20th, 2015 (1 Comment)

Today I will guide step by step create project Helloworld with Spring MVC. This is my knowledge learned and working on Spring MVC, if something goes wrong you wish the enthusiastic feedback.
The first article is a basic one that you will encounter in any programming books: Hello World.
At first, we must prepare something:

  1. Eclipse (I use eclipse kepler)
  2. JDK 7
  3. Maven 3 (You click here to see more detail install and config maven)
  4. Spring 4.1.4.RELEASE
  5. Jboss 7.1 or Tomcat 7.0 (in here, I work on Jboss 7.1, you can run on Tomcat or other Web Server), to install and run on Jboss 7.1 you click here to view run jboss with eclipse

As this is my first post should be detailed step by step from create project and run it. From the following article, I will skip this step and focus on the main content of the article.

Now, let’s start eclipse and select Workspace to storing spring mvc project

Step 1 : In the work of the Eclipse window, choose File –> New -> Maven Project. If not found Maven Project, you select line Others. In the window that appears, you find maven project in search box, then click Next button.

Step 2: Check on Use default Workspace location and click Next button, filter and select Artifact is “maven-archetype-webapp”, and Next.

Step 3: in here, you need set name of project:

  • Group Id:   demo -> Group ID will be used for the packaging of the future
  • Artifact Id: springmvc -> name of project will show in eclipse
  • Version:      0.1-SNAPSHOT
  • Package:     com.demo.springmvc

Click Finish button.

If have error : could not resolve archetype org.apache.maven.archetypes … you can fixed as below:

  1. Open Window > Preferences
  2. Select Maven > Archetypes
  3. Click “Add Remote Catalog” and add the following:

Step 4: Modify project again, right click project name on eclipse and select Properties and the window will appears:

  1. Select Project facets, only check Dynamic Web Module, Java, Javascript.
  2. Select Runtime tab in right and check jboss 7.1 Runtime
  3. Select Java Build Path, in Source tab -> uncheck Allow output folders for source folders. After project will appears demo/src/main/java include source code java files

Step 5: modify maven to config spring mvc

Open porn.xml file and edit:

Create 2 file web.xml and servlet-context.xml (or spring-mvc-servlet.xml) in WEB-INF folder

web.xml:

DispatcherServlet will point to Spring’s Dispatcher. Every request must through Dispatcher. You can add config files in <param-value></param-value> of <context-param>

servlet-context.xml:

Line 11: select package include controllers

Line 16: jsp page to layout

Controller file: create or modify HelloWorldController.java in com.demo.springmvc:

  • Annotation @Controller: this is Controller.
  • Annotation @RequestMapping: value is define url of this controller, method is POST or GET of Controller
  • Return hello: return to hello.jsp file

JSP file: create or modify hello.jsp file in WEB-INF / pages

Step 6: Config before build and run web

Right click on project’s name in the project explorer and select properties:

  1. Select Deployment Assembly, click add and select “Java Build Path Entries”, Next and select “Maven Dependencies” -> finish (fixed for Error configuring application listener of class org.springframework.web.context.ContextLoaderListener: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener).
  2. Select “Java Build Path”: in libraries tab, remove and only add 3 lib (jre, jboss, maven). If have not jboss, you click “add libraries” -> Server runtime -> Next -> Jboss 7.1 runtime -> finish
config build spring

config build spring

Step 7: Run web application on jboss 7.1

  1. Right click on Jboss 7.1 Runtime Server and select Add and Remove
  2. Select springmvc in Available tab and Add to Configured tab, click finish button
  3. In Server tab, click Publish to the server (Ctrl + Alt + P) to up your project to Server
server jboss 7.1

server jboss 7.1

Click Start to run web (you can start the server in debug mode)

Open url with http://localhost:8080/springmvc/hello

Create file index.jsp in WebContent or webapp folder and add code:

Open url with http://localhost:8080/springmvc will redirect to http://localhost:8080/springmvc/hello

Tags: , , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

One Comment:

  1. Srinivasa Rao Pujari

    It is very nice and helpful. Thank you so much for the detailed explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *