Spring MVC – 04 – MultiActionController – InternalPathMethodNameResolver

Merhaba Arkadaslar.
Bu bolumde MultiActionController sinifini inceleyecegiz. MultiActionController‘u kullanarak tek Controller ile birden fazla action icin istek karsilanabilir. (handle request)

NOT : MultiActionController Spring 4.3 versiyonunda deprecated oldu.
@deprecated as of 4.3, in favor of annotation-driven handler methods

MultiActionController , AbstractController sinifini kalitmaktadir.

public class MultiActionController extends AbstractController implements LastModified {
...
}

MultiActionController icin kullanacagimiz metotlar su formata uygun olmalidir !

public (ModelAndView | Map | String | void) actionName(HttpServletRequest request, HttpServletResponse response, [,HttpSession] [,AnyObject]);

OrderController.java

package _03.multiActionController.controller;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

// public class MultiActionController extends AbstractController implements
// LastModified { ...}
public class OrderController extends MultiActionController {

	// public (ModelAndView | Map | String | void) actionName(HttpServletRequest
	// request, HttpServletResponse response, [,HttpSession] [,AnyObject]);

	public String add(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("message", "add method");
		return "03.multiActionController.view/add";
	}
	public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("03.multiActionController.view/remove");
		modelAndView.addObject("message", "remove method");
		return modelAndView;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
		modelAndView.setViewName("03.multiActionController.view/list");
		modelAndView.addObject("message", "list method");
		return modelAndView;
	}
}

Controller sinifimizi olusturduk , MultiActionController sinifini kalitiyoruz. Metotlarin donus tipi String , Map ya da ModelAndView olabilir.
ModelAndView objemiz ile view ve model’imizi duzenleyebiliriz.

03.multiActionController.xml

	
...
	<!--######## BeanNameUrlHandlerMapping begin ######## -->
	
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
	<bean name="/order/*" class="_03.multiActionController.controller.OrderController"/>
	
	<!--######## BeanNameUrlHandlerMapping end ######## --> 
	
	<!--######## SimpleUrlHandlerMapping begin ######## -->
	<!-- 
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/order/*">orderController</prop>
				<prop key="/siparis/*">orderController</prop>
			</props>
		</property>
	</bean>
	
	<bean name="orderController" class="_03.multiActionController.controller.OrderController"/>
	 -->
	<!--######## SimpleUrlHandlerMapping begin ######## -->
	 
	<!-- ######## ControllerClassNameHandlerMapping begin ########  -->
	<!-- 
	<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />	
	<bean class="_03.multiActionController.controller.OrderController"/>
 	-->
	<!-- ######## ControllerClassNameHandlerMapping end ########  -->
	
	<!-- ######## ControllerBeanNameHandlerMapping begin ########  -->
	<!-- 
	<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />
	<bean name="order/*" class="_03.multiActionController.controller.OrderController"/>	
	 -->
	<!-- ######## ControllerBeanNameHandlerMapping end ########  -->
...

Onceki bolumlerde inceledigimiz HandlerMapping siniflarindan diledigimizi kullanabiliriz. ControllerClassNameHandlerMapping disindaki diger 3 HandlerMapping icin /* ile bitirmemiz gereklidir. Boylelikle OrderController sinifinda yer alan metotlara karsilik gelen istekleri karsilayabiliriz.

jsp dosyalarimiz 03.multiActionController.view dizininde yer alacaktir.
add.jsp , remove.jsp ve list.jsp dosyalarimizin icerigi basit olarak ;

add.jsp

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

web.xml
web.xml dosyamizda onceki orneklerimizde kullandigimiz XML konfigurasyon dosyalarini yorum satiri haline getirip , bu ornegimizde kullanacagimiz konfigurasyonu ekleyelim.

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

Ornegimizi calistiralim;
Dikkat edecek olursak add metodu icin /add , remove metodu icin /remove ve list metodu icin /list URL bilgisini kullaniyoruz.

http://localhost:8080/injavawetrust.springmvc/order/add

multiactioncontroller add

http://localhost:8080/injavawetrust.springmvc/order/remove

multiactioncontroller remove

http://localhost:8080/injavawetrust.springmvc/order/list

multiactioncontroller list

InternalPathMethodNameResolver

InternalPathMethodNameResolver , MultiActionController icin varsayilan olarak kullanilan metot ismi cozumleyicisidir.
OrderController bean tanimi icin methodNameResolver property bilgisini kullanalim. prefix ve suffix property bilgisini veriyoruz.

<!--######## ControllerClassNameHandlerMapping start ######## -->

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
	
<!--######## ControllerClassNameHandlerMapping end ######## -->

<bean class="_03.multiActionController.controller.OrderController">
    <property name="methodNameResolver">
	    <bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
   		    <property name="prefix" value="my" />
  	 		<property name="suffix" value="Method" />
		</bean>
    </property>
</bean>

OrderController.java

...
   //org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver
   //prefix and suffix
   public String myprefixMethod(HttpServletRequest request, HttpServletResponse response) {
      request.setAttribute("message", "myprefixMethod");
      return "03.multiActionController.view/add";
   }
...

Ornegimizi calistiralim ;
Dikkat edecek olursak /prefix URL bilgisini kullaniyoruz. prefix olarak “my” , suffix olarak “Method” degerini kullanmistik. Boylelikle my+suffix+Method –> mySuffixMethod istegi karsilayacaktir.

http://localhost:8080/injavawetrust.springmvc/order/prefix

multiactioncontroller prefix suffix

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 *