Tuesday, March 29, 2016

Spring MVC hello world project




This is the third post under Spring MVC. In my previous posts I've mentioned following things.


In this post I'm gonna talk about, how to create your first Spring MVC hello world program. This is very simple. I'm going to create one controller and jsp page to demonstrate this with xml based configurations. I can deliver steps as follows.

  • Update web.xml file
  • Create front controller (dispatcher-servlet.xml)
  • Adding basic Beans to the front controller
  • Create Hello controller (Your own controller)
  • Create view page (index.jsp)

Update web.xml file

  1. Go to Spring reference Here I'm using Spring 4.1.6.RELEASE.
  2. Then search for dispatcher ( Hint: Ctrl+f and type dispatcher for search )
  3. Under The DispatcherServlet section, you will be able to see url mapping for front controller.
  4. Add this code to web.xml and do changes as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <display-name>SpringTest</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

</web-app>

Create front controller (dispatcher-servlet.xml)


  • Create new Bean Configuration file in WEB-INF folder (Ctrl + n and type spring)

  • Give the name as dispatcher-servlet.xml (dispatcher in web.xml is exactly same as this file name)



  • Then you can add namespaces or you can just finish creating dispatcher-servlet (namespaces can be added later)
  • If you finished creating dispatcher-servlet.xml then you can add namespaces to the dispatcher-servlet using Namespaces tab.





<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>


  • Both are same, you can use whatever you prefer.
  • Finally your dispatcher-servlet.xml may look as below.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


 <context:component-scan base-package="com.app" />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

  • Now your front controller(disptcher-servlet.xml) is ready :)

Create Hello controller (Your own controller)

  • Create a simple java class in src/main/java folder.
  • I use com.app.controller as my package for that controller class and I name it as HelloController.java


  • Then I create following method to use to view index page(I didn't create index.jsp yet).

package com.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
 
 @RequestMapping("/index")
 public String showPage(){
  return "index";
 }

}


  • @Controller - This is used to make this class as a controller.
  • @RequestMapping - This is used to map the browser request. DispatcherServlet(front controller) obtain user request and send it to relevant controller according to this mapping.
  • You can use any name as the method name in controller.
  • That is a String method and it returns a String value and that String value is captured by view resolver in dispatcher-servlet.xml 
  • Then it returns the relevant jsp page, we have to create this jsp page.

Create view page (index.jsp)


  • Create a jsp file in src->main->webapp->WEB-INF->jsp (I've created a folder called jsp in WEB-INF) Because we have defined this location in dispatcher-servlet.
  • You can add something to your file to identify it.
  • Your file name must be index.jsp. 
  • Because we return "index" in controller.


Now everything is ok. Then you can deploy your project.

First run

  • Right click on your project and go to Run As -> Maven build...

  • Then type "clean install" in Goal. Then Run.


  • Then it will clean your target directory and install all dependencies that you want to run the project (Because of this is the first run, you may don't have anything in target directory) 
  • Then update your project (Alt + F5)
  • Then right click on your project and then select Run As -> Run on Server(You have to create a server instance, here I've used Apache Tomcat ).

Now You will be able to see the output. If you are getting any errors, please check again and rebuild.