Monday 19 November 2018

Spring mvc: SimpleUrlHandlerMapping annotation based configuration

In my previous post, I explained an example of SimpleUrlHandlerMapping using xml based approach. In this post, I am going to rewrite the same example using annotation based approach.

         <bean
         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                  <property name="mappings">
                           <value>
                                    /hello=helloMsg
                                    /welcome=helloMsg
                                    /*/welcome=helloMsg
                           </value>
                  </property>
         </bean>

         <bean id="helloMsg" class="com.sample.myApp.controllers.HelloWorldController" />

Above configurations can be written like below.

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
         SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();

         Map<String, Object> urlMap = new HashMap<>();
         urlMap.put("/hello", helloMsg());
         urlMap.put("/welcome", helloMsg());
         urlMap.put("/*/welcome", helloMsg());
         simpleUrlHandlerMapping.setUrlMap(urlMap);

         return simpleUrlHandlerMapping;
}

@Bean
public HelloWorldController helloMsg() {
         return new HelloWorldController();
}

Find the below working application.

ApplicationConfig.java
package com.sample.myApp.configs;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;

import com.sample.myApp.controllers.HelloWorldController;

@Configuration
public class ApplicationConfig {
 @Bean
 public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
  SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();

  Map<String, Object> urlMap = new HashMap<>();
  urlMap.put("/hello", helloMsg());
  urlMap.put("/welcome", helloMsg());
  urlMap.put("/*/welcome", helloMsg());
  simpleUrlHandlerMapping.setUrlMap(urlMap);

  return simpleUrlHandlerMapping;
 }

 @Bean
 public HelloWorldController helloMsg() {
  return new HelloWorldController();
 }

}


HelloWorldController.java
package com.sample.myApp.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  ModelAndView modelAndView = new ModelAndView("hello");

  modelAndView.addObject("message", "Welcome to Spring MVC framework");
  return modelAndView;

 }

}

Create hello.jsp file under WEB-INF/jsp folder.

hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h2>${message}</h2>
</body>
</html>


Create web.xml, HelloWorld-servlet.xml file under WEB-INF folder.

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 MVC Hello WorldApplication</display-name>

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

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>


HelloWorld-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

</beans>

Create index.jsp under webapp folder.

index.jsp
<html>
<body>
 <h2>Hello World!</h2>
</body>
</html>


Total project structure looks like below.


Run the application on server and hit any of below urls.

You will get the message 'Welcome to Spring MVC framework'.




Previous                                                 Next                                                 Home

No comments:

Post a Comment