Monday, January 4, 2016

Spring MVC hello world annotation example

REf:- http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-annotation-example/

1. Directory Structure

spring2-mvc-annotation-hello-world

2. Controller & Handler Mapping

Now, you can use @Controller and @RequestMapping to replace the XML configuration.
  1. Controller – The controller class is no longer need to extend the base controller likeAbstractController or SimpleFormController, just simply annotate the class with a@Controller annotation.
  2. Handler Mapping – No more declaration for the handler mapping likeBeanNameUrlHandlerMappingControllerClassNameHandlerMapping orSimpleUrlHandlerMapping, all are replaced with a standard @RequestMappingannotation.
HelloWorldController.java
package com.mkyong.common.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/welcome")
public class HelloWorldController{
 
 @RequestMapping(method = RequestMethod.GET)
 public ModelAndView helloWorld(){
 
  ModelAndView model = new ModelAndView("HelloWorldPage");
  model.addObject("msg", "hello world");
 
  return model;
 }
}

If the @RequestMapping is applied at the class level (can apply at method level with multi-actions controller), it required to put a RequestMethod to indicate which method to handle the mapping request.
In this case, if a URI pattern /welcome is requested, it will map to this HelloWorldController, and handle the request with helloWorld() method.


3. Spring XML Configuration

You still need to configure the view resolver and component scanning in XML file.
/WEB-INF/spring-mvc-config.xml
<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-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
 
 <context:component-scan base-package="com.mkyong.common.controller" />

</beans>

4. JSP Page

A simple JSP page for demonstration.
HelloWorldPage.jsp.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
 <h1>Spring MVC Hello World Annotation Example</h1>

 <h2>${msg}</h2>
</body>
</html>

5. web.xml

web.xml
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring Web MVC Application</display-name>

 <servlet>
  <servlet-name>mvc-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-mvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

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

</web-app>

No comments:

Post a Comment