JAVA Tutorials

What are Spring Controllers / Handlers?

The Controller is constrained for processing user requests and building an appropriate model and passes it to the view for rendering. As we know, Spring Controllers are the core concept of the Spring MVC application.

  1. ControllerClassNameHandlerMapping: The ControllerClassNameHandlerMapping class uses a convention to determine the mapping between request URLs and the Spring Controllers instances that are to handle those requests. In this case, we do not need to declare a bean name for the Controller. In the example below, the ControllerClassNameHandlerMapping will map to the HelloWorldController all URL requests that start with helloWorld, or helloWorld*. These properties allow ControllerClassNameHandlerMapping to also map to the HelloWorldController all URL requests with uppercase characters, like helloWorldJavaCode, as also URL requests with path prefix like /javacode/helloWorld.

mvc-dispatcher-servlet.xml

<bean  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
   <property name="caseSensitive" value="true" />
     <property name="pathPrefix" value="/javacode" />
   </bean>  
  1. SimpleUrlHandlerMapping: The SimpleUrlHandlerMapping provides a property called mappings so as to be configured. This property is set in the bean declaration and consists of key-value mapping pairs. It can be set in two ways, as shown below:

mvc-dispatcher-servlet.xml

First Way:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
           <prop key="/helloWorld.htm">helloWorldController</prop>
           <prop key="/*/hello.htm">helloWorldController</prop>
           <prop key="/hello*.htm">helloWorldController</prop>
         </props>
       </property>
    </bean>
Second Way: 
   <bean id="helloWorldController" class="com.java.snippets.enterprise.HelloWorldController" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
         <value>
           /helloWorld.htm=helloWorldController
           /*/hello.htm=helloWorldController
           /hello*.htm=helloWorldController
        </value>
       </property>
    </bean>

Spring MVC Handler Mapping:

The HandlerMapping is an Interface which is implemented by the objects that define a mapping between the requests and the handler objects. Here, we see DispatcherServlet uses BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping.

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
     <bean name="/helloWorld.htm" 
          class="com.javainterviewpoint.HelloWorldController" />
     <bean name="/hello*.htm" 
         class="com.javainterviewpoint.HelloWorldController" />  
</bean>

Example:

HelloWorldController.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloWorldController_1 extends AbstractController
{
    @Override
    protected ModelAndView_1 handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("Inside the HelloWorldController");
        ModelAndView model_1 = new ModelAndView("helloWorld");
        model.addObject("msg", "Java");      
        return model_1;
    }
}

WelcomeController.java

This program is almost the same as HelloWorldController except the redirecting page and the String passed.

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 WelcomeController extends AbstractController
{
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("Inside the WelcomeController");
        ModelAndView model_2 = new ModelAndView("welcome");
        model.addObject("msg", "Java");        
        return model_2;
    }
}

helloWorld.jsp

<html>
    <head>       
       <title>Welcome</title>
    </head>
    <body>
       <h2>Hello World ${msg}</h2> 
    </body>
</html>

welcome.jsp

<html>
    <head>        
        <title>Welcome</title>
    </head>
    <body>
        <h2> Welcome to ${msg}</h2>
    </body>
</html>

web.xml

<display-name>SpringMVCFormHandling</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>SpringConfig</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>SpringConfig</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

SpringConfig-servlets.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/Jsp/"/>
         <property name="suffix" value=".jsp"/>
     </bean> 
      <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
     <bean name="/helloWorld.htm" 
         class="com.java.HelloWorldController" /> 
     <bean name="/hello*" 
         class="com.java.HelloWorldController" />
     <bean name="/welcome.htm"
         class="com.java.WelcomeController"/> 
</beans>

Output:

when HelloWorldController called
Hello World Java
when WelcomeController called
Welcome to Java

Facebook Comments
Tags

Related Articles

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button
Close
Close