Spring MVC – 05 – MultiActionController – PropertiesMethodNameResolver

Merhaba Arkadaslar.
Onceki bolumde MultiActionController sinifinin kullanimini incelemistik.
methodNameResolver olarak varsayilan olarak InternalPathMethodNameResolver sinifi kullanilmaktadir.
Bu bolumde methodNameResolver olarak PropertiesMethodNameResolver sinifinin kullanimini inceleyecegiz.

Onceki ornegimizde kullandigimiz OrderController sinifini aynen farkli bir package’ta kullanacagiz.

OrderController.java

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

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

04.propertiesMethodNameResolver.xml

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

	<bean class="_04.propertiesMethodNameResolver.controller.OrderController">
	  <property name="methodNameResolver">
	      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
	         <property name="mappings">
		         <props>
					 <prop key="/order/customerAdd">add</prop>
					 <prop key="/order/customerRemove">remove</prop>
					 <prop key="/order/customerList">list</prop>
				 </props>
			</property>
	      </bean>
	  </property>
	</bean>
 -->	
	
	<!-- BeanNameUrlHandlerMapping  -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<bean name="/order/*" class="_04.propertiesMethodNameResolver.controller.OrderController">
	  <property name="methodNameResolver">
	      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
	         <property name="mappings">
		         <props>
					 <prop key="/order/customerAdd">add</prop>
					 <prop key="/order/customerRemove">remove</prop>
					 <prop key="/order/customerList">list</prop>
				 </props>
			</property>
	      </bean>
	  </property>
	</bean>
	
	
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
...

XML konfigurasyonumuzu inceleyecek olursak OrderController bean taniminda methodNameResolver property icin PropertiesMethodNameResolver bean tanimini yapiyoruz.
PropertiesMethodNameResolver bean taniminda mappings property icin URL bilgilerini veriyoruz. Burada dikkat edecek olursak /order ile basliyoruz , yani oncelikle OrderController sinifi icin tanimladigimiz URL bilgisini yaziyoruz.

04.propertiesMethodNameResolver.view dizininde jsp dosyalarimiz yer alacak. Basit olarak add.jsp , remove.jsp , list.jsp dosyalarimizin icerigi ;

add.jsp

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

Ornegimizi calistirdigimizda;

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

propertiesMethodNameResolver add

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

propertiesMethodNameResolver remove

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

propertiesMethodNameResolver 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 *