Spring MVC – 03 – Handler Mapping

Merhaba Arkadaslar,
Bu bolumde HandlerMapping kavramini inceleyecegiz . HandlerMapping , gelen istegin/request , hangi Controller tarafindan karsilanacagini/handle tanimlamamizi saglar.
Bir Spring Web Application’ina ilk istek geldiginde istek DispatcherServlet tarafindan karsilanmaktadir. (handle request)
Sonrasinda bu istek , gercekten ilgili istegi karsilayacak Controller’a yonlendirilmelidir. (dispatch)
DispatcherServlet , ilgili Controller’a istegi yonlendirmek icin HandlerMapping implementation’inindan yararlanmaktadir.

HandlerMapping.java

package org.springframework.web.servlet;
import javax.servlet.http.HttpServletRequest;
public interface HandlerMapping {
   HandlerExecutionChain getHandler(HttpServletRequest request)
   throws Exception;
}

Oncelikle web.xml dosyamiza , yeni uygulamamiz icin kullanacagimiz XML konfigurasyon dosyamizin bilgisini ekleyelim. Onceki ornekte kullandigimiz 01.appContext.xml i yorum satiri haline getirebiliriz;

web.xml

servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
			<!--/WEB-INF/01.appContext.xml -->
			/WEB-INF/02.00.appContext.xml</param-value>
		</init-param>

		<load-on-startup>1</load-on-startup>
</servlet>

02.00.appContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">

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

</beans>

BeanNameUrlHandlerMapping

BeanNameUrlHandlerMapping , varsayilan/default handler mapping sinifidir. Spring tanimlimli bir handler mapping bulamadiginda BeanNameUrlHandlerMapping ‘i kullanacaktir.

Controller siniflarimizi tanimlayalim , burada diger bir yaklasim olarak AbstractController sinifini kalitacagiz.

Page1Controller.java

package _02_01.beanNameUrlHandlerMapping.controller;

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 Page1Controller extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView modelAndView = new ModelAndView("02.01.beanNameHandlerMapping.view/myPage1");
		modelAndView.addObject("message", "Hello Page1");
		return modelAndView;
	}

}

AbstractController abstract sinifini kalittigimizda handleRequestInternal metodunu override etmemiz gereklidir.
ModelAndView objemizi olusturuyoruz , view bilgisi olarak 02.01.beanNameHandlerMapping.view dizininde yer alan myPage1.jsp dosyasini veriyoruz. Sonuna jsp yazmadigimiza dikkat edelim. 02.00.appContext.xml dosyasinda suffix olarak .jsp bilgisini zaten ekledik.

myPage1.jsp

...
<body>
	<h2>${message}</h2>
</body>
...

Page2Controller ve Page3Controller sinifimiz da ayni sekilde tanimliyoruz. myPage2.jsp ve myPage3.jsp dosyasini view olarak kullacak sekilde duzenliyoruz.

Page2Controller.java

package _02_01.beanNameUrlHandlerMapping.controller;

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 Page2Controller extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView modelAndView = new ModelAndView("02.01.beanNameHandlerMapping.view/myPage2");
		modelAndView.addObject("message", "Hello Page2");
		return modelAndView;
	}

}


Page3Controller.java

package _02_01.beanNameUrlHandlerMapping.controller;

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 Page3Controller extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView modelAndView = new ModelAndView("02.01.beanNameHandlerMapping.view/myPage3");
		modelAndView.addObject("message", "Hello Page3");
		return modelAndView;
	}

}

02.01.beanNameUrlHandlerMapping.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">


	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<bean name="/page1"  class="_02_01.beanNameUrlHandlerMapping.controller.Page1Controller" />
	<bean name="/page2"  class="_02_01.beanNameUrlHandlerMapping.controller.Page2Controller" />
	<bean name="/page3*" class="_02_01.beanNameUrlHandlerMapping.controller.Page3Controller" />

</beans>

Oncelikle BeanNameUrlHandlerMapping sinifi icin bean tanimini gerceklestiriyoruz. Sonrasinda tanimladigimiz Controller siniflarimiz icin bean tanimini gerceklestiriyoruz.
Burada dikkat edecek olursak /page1 , /page2 ve /page3* seklinde name attribute bilgisi verdik.

NOT : BeanNameUrlHandlerMapping icin bean name / ile baslamalidir.

02.00.appContext.xml

...
<import resource="02.01.beanNameUrlHandlerMapping.xml"/>
...

02.00.appContext.xml konfigurasyon dosyamiza 02.01.beanNameUrlHandlerMapping.xml dosyamizi import edelim.

Ornegimizi calistiralim ;
“/page1” , Page1Controller.java sinifi tarafindan karsilanacaktir. View olarak myPage1.jsp kullanilacaktir.

http://localhost:8080/injavawetrust.springmvc/page1

beannameurlhandlermapping page1

“/page2” , Page2Controller.java sinifi tarafindan karsilanacaktir. View olarak myPage2.jsp kullanilacaktir.

http://localhost:8080/injavawetrust.springmvc/page2

beannameurlhandlermapping page2

“/page3” , Page3Controller.java sinifi tarafindan karsilanacaktir. View olarak myPage3.jsp kullanilacaktir.

http://localhost:8080/injavawetrust.springmvc/page3

beannameurlhandlermapping page3

bean name olarak “/page3*” oldugu icin yani * wildcard kullanildigi icin “/page3XXXX” URL bilgisi de Page3Controller.java sinifi tarafindan karsilanacaktir.

beannameurlhandlermapping page4

SimpleUrlHandlerMapping

SimpleUrlHandlerMapping tanimininda setMappings metodu yer almaktadir.

	public void setMappings(Properties mappings) {
		CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);
	}

Bean taniminda ve etiketlerini kullanarak key olarak url bilgisi , value olarak controller bilgisini verebiliriz.

Oncelikle Controller siniflarimizi tanimlayalim ;

LoginController.java

package _02_02.simpleUrlHandlerMapping.controller;

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 LoginController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("02.02.simpleUrlHandlerMapping.view/myLoginPage");
		modelAndView.addObject("message", "Hello Login Page");
		return modelAndView;
	}

}

LogoutController.java

package _02_02.simpleUrlHandlerMapping.controller;

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 LogoutController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("02.02.simpleUrlHandlerMapping.view/myLogoutPage");
		modelAndView.addObject("message", "Hello Logout Page");
		return modelAndView;
	}

}

02.02.simpleUrlHandlerMapping.xml

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/login">loginController</prop>
				<prop key="/index">loginController</prop>
				<prop key="/welcome">loginController</prop>
				<prop key="/logout">logoutController</prop>
                                <prop key="/exit">logoutController</prop>             
			</props>
		</property>
	</bean>

	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<value>
				/guest=loginController
				/newuser=loginController
			</value>
		</property>
	</bean>

	<bean id="loginController"  class="_02_02.simpleUrlHandlerMapping.controller.LoginController" />
        <bean id="logoutController" class="_02_02.simpleUrlHandlerMapping.controller.LogoutController" />

/login /index /welcome URL adresleri LoginController tarafindan karsilanacaktir.
LoginController icin id bilgisi “loginController” olarak tanimliyoruz.

/logout ve /exit URL adresleri LogoutController tarafindan karsilanacaktir.
LogoutController icin id bilgisini “logoutController” olarak tanimliyoruz.

SimpleUrlHandlerMapping icin 2.yontemle de tanimlama yapabiliriz.
/guest ve /newuser , loginController tarafindan karsilanacaktir.

02.00.appContext.xml

...
<!--
<import resource="02.01.beanNameUrlHandlerMapping.xml"/>
-->
<import resource="02.02.simpleUrlHandlerMapping.xml"/> 
...

Ornegimizi calistiralim , /login , /guest /logout icin test ettigimizde ;

http://localhost:8080/injavawetrust.springmvc/login
http://localhost:8080/injavawetrust.springmvc/index
http://localhost:8080/injavawetrust.springmvc/welcome
http://localhost:8080/injavawetrust.springmvc/guest
http://localhost:8080/injavawetrust.springmvc/newuser
http://localhost:8080/injavawetrust.springmvc/logout
http://localhost:8080/injavawetrust.springmvc/exit

simpleurlhandlermapping login

simpleurlhandlermapping guest

simpleurlhandlermapping logout

ControllerClassNameHandlerMapping

ControllerClassNameHandlerMapping , tanimladigimiz Controller siniflari icin “Controller” ekini kaldirir. Basina / karakteri ekler ve kucuk karakter olacak sekilde URL bilgisine uygun sekilde mapping uygulanir.

Ornegin CustomerOrderController icin ; Controller prefix silinir ve kucuk harf olacak sekilde /customerorder URL olacaktir.

CustomerOrderController.java

package _02_03.controllerClassNameHandlerMapping.controller;

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 CustomerOrderController extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("02.03.controllerClassNameHandlerMapping.view/myOrderPage");
		modelAndView.addObject("message", "Hello OrderPage");
		return modelAndView;
	}

}

CustomerTransactionController.java

package _02_03.controllerClassNameHandlerMapping.controller;

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 CustomerTransactionController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("02.03.controllerClassNameHandlerMapping.view/myTransactionPage");
		modelAndView.addObject("message", "Hello TransactionPage");
		return modelAndView;
	}

}

02.03.controllerClassNameHandlerMapping.xml

	<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

	<bean class="_02_03.controllerClassNameHandlerMapping.controller.CustomerOrderController" />
	<bean class="_02_03.controllerClassNameHandlerMapping.controller.CustomerTransactionController" />

bean taniminda sadece class bilgisini vermemiz yeterli olacaktir.

02.00.appContext.xml

...
<!--
<import resource="02.01.beanNameUrlHandlerMapping.xml"/>
<import resource="02.02.simpleUrlHandlerMapping.xml"/>
-->
<import resource="02.03.controllerClassNameHandlerMapping.xml"/>  
...

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.springmvc/customerorder
http://localhost:8080/injavawetrust.springmvc/customertransaction

controllerclassnamehandlermapping customerorder

controllerclassnamehandlermapping transaction

ControllerBeanNameHandlerMapping

ControllerBeanNameHandlerMapping , BeanNameUrlHandlerMapping gibi calisir.
BeanNameUrlHandlerMapping de URL bilgisi / ile baslamak zorundadir.

ReportController.java

package _02_04.controllerBeanNameHandlerMapping.controller;

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 ReportController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView("02.04.controllerBeanNameHandlerMapping.view/myReportPage");
		modelAndView.addObject("message", "Hello ReportPage");
		return modelAndView;
	}

}

02.04.controllerBeanNameHandlerMapping.xml

	<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />

	<bean name="report" class="_02_04.controllerBeanNameHandlerMapping.controller.ReportController" />

02.00.appContext.xml

...
<!--
<import resource="02.01.beanNameUrlHandlerMapping.xml"/>
<import resource="02.02.simpleUrlHandlerMapping.xml"/>
<import resource="02.03.controllerClassNameHandlerMapping.xml"/>  
-->
<import resource="02.04.controllerBeanNameHandlerMapping.xml"/>  
...

Ornegimizi calistirdigimizda ;

http://localhost:8080/injavawetrust.springmvc/report

Github kaynak kodlar / source folder
Injavawetrust-springmvc-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

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