Spring MVC – 06 – MultiActionController – ParameterMethodNameResolver

Merhaba Arkadaslar
Onceki bolumlerde MultiActionController sinifinin kullanimini incelemistik.
methodNameResolver olarak varsayilan olarak InternalPathMethodNameResolver sinifi kullanilmaktadir. Onceki bolumde methodNameResolver olarak
PropertiesMethodNameResolver sinifini kullandik.

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

Bu bolumde methodNameResolver olarak ParameterMethodNameResolver sinifinin kullanimini inceleyecegiz.
Onceki orneklerimizde kullandigimiz OrderController sinifini tekrar farkli bir package’ta kullanalim.

OrderController.java

package _05.parameterMethodNameResolver.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 "05.parameterMethodNameResolver.view/add";
	}
	public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("05.parameterMethodNameResolver.view/remove");
		modelAndView.addObject("message", "remove method");
		return modelAndView;
	}

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

05.parameterMethodNameResolver.xml

methodNameResolver property icin ParameterMethodNameResolver sinifini kullaniyoruz.
ParameterMethodNameResolver icin paramName property icin value olarak action yazdik.

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

	<bean class="_05.parameterMethodNameResolver.controller.OrderController">
       <property name="methodNameResolver">
	      <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
	         <property name="paramName" value="action"/>
	      </bean>
       </property>
	</bean>
 
	
	<!-- BeanNameUrlHandlerMapping  
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<bean name="/order" class="_05.parameterMethodNameResolver.controller.OrderController">
       <property name="methodNameResolver">
	      <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
	         <property name="paramName" value="action"/>
	      </bean>
       </property>
	</bean>
	-->
		
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
...

web.xml dosyamizda konfigurasyonu duzenleyelim;

web.xml

...
	    <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
			 /WEB-INF/04.propertiesMethodNameResolver.xml
			 -->			 	
			 /WEB-INF/05.parameterMethodNameResolver.xml
			</param-value> 
		</init-param>
...

05.parameterMethodNameResolver.view dizininde JSP dosyalarimiz yer alacak.

add.jsp

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

Ornegimizi calistiralim , parameter olarak ;

  • ?action=add
  • ?action=remove
  • ?action=list
http://localhost:8080/injavawetrust.springmvc/order?action=add
ParameterMethodNameResolver add
http://localhost:8080/injavawetrust.springmvc/order?action=remove

ParameterMethodNameResolver remove

http://localhost:8080/injavawetrust.springmvc/order?action=list

ParameterMethodNameResolver list

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 *