Spring MVC – 17 – ViewResolver – 01 | XmlViewResolver | ResourceBundleViewResolver

Merhaba Arkadaslar
Bu bolumde View Resolver konusunu incelemeye baslayacagiz.
Simdiye kadar olan orneklerimizde ViewResolver implementation olarak InternalResourceViewResolver sinifini kullandik.

Bu bolumde inceleyecegimiz ViewResolver implementation’lar ;

  • XmlViewResolver
  • ResourceBundleViewResolver

Gercek uygulamalarda JSP dosyalari WebContent altinda degil mutlaka WEB-INF klasoru altinda yer alir. Boylelikle direkt erisime karsi security/guvenlik saglanmis olur.
WEB-INF altina koydugumuz JSP dosyalarini Spring bizim icin cozumleyecektir ve render edecektir. Iste bu cozumlemeyi yapabilmek icin ViewResolver implementation’ini kullanilir.

ViewResolver da resolveViewName metodu yer almaktadir ve geriye org.springframework.web.servlet.View donmektedir.

ViewResolver.java

package org.springframework.web.servlet;
import java.util.Locale;
public interface ViewResolver {
    View resolveViewName(String viewName, Locale locale) throws Exception;
}

view secildikten sonra , DispatcherServlet render metodunu cagirir.

DispatcherServlet.java

...
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
...
}
...

View.java

package org.springframework.web.servlet;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface View {
    String getContentType();
    void render(Map<String, ?> model,HttpServletRequest request, HttpServletResponse response) throws Exception;
}

XmlViewResolver

XmlViewResolver , varsayilan olarak /WEB-INF/views.xml dosyasini kullanir. (DEFAULT_LOCATION) . views.xml dosyasinda view’ler yer alacaktir.

XmlViewResolver.java

package org.springframework.web.servlet.view;
...
public class XmlViewResolver extends AbstractCachingViewResolver
		implements Ordered, InitializingBean, DisposableBean {

	/** Default if no other location is supplied */
	public final static String DEFAULT_LOCATION = "/WEB-INF/views.xml";


...
}

16.xmlViewResolver.xml
Konfigurasyon dosyamizda XmlViewResolver icin bean tanimini gerceklestirelim.
location property degerini degistirelim.

...
<context:component-scan base-package="_16.xmlViewResolver.controller" />

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
   <property name="location">
       <value>/WEB-INF/16.views.xml</value>
   </property>
</bean>
...

16.views.xml

Views tanimlarinin yer aldigi xml dosyamizi inceleyecek olursak JstlView sinifi tipinde bean tanimlarini gerceklestirelim. View dosyalari olarak JSP sayfalari kullandigimiz icin JstlView tipindeki bean tanimi uygun olacaktir.

url property degerini ayarliyoruz , boylece ilgili bean id icin kullanilacak JSP sayfamizi belirtiyoruz.

....
<bean id="myBeanViewNamePage1" class="org.springframework.web.servlet.view.JstlView">
    <property name="url" value="WEB-INF/jsp/16.xmlViewResolver.view/myPage1.jsp" />
</bean>

<bean id="myBeanViewNamePage2" class="org.springframework.web.servlet.view.JstlView">
    <property name="url" value="WEB-INF/jsp/16.xmlViewResolver.view/myPage2.jsp" />
</bean>
...

ResolverController.java
Controller sinifimizi inceleyecek olursak view name olarak “myBeanViewNamePage1” ve “myBeanViewNamePage2” degerini kullaniyoruz. Bu degerler yukarida tanimladigimiz XML dosyamizda yer alan bean id degerlerine karsilik gelmektedir.

package _16.xmlViewResolver.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ResolverController {

	@RequestMapping("/page1")
	public ModelAndView handleViewResolverForPage1() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "page1");
		modelAndView.setViewName("myBeanViewNamePage1");
		return modelAndView;
	}
	
	@RequestMapping("/page2")
	public ModelAndView handleViewResolverForPage2() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "page2");
		modelAndView.setViewName("myBeanViewNamePage2");
		return modelAndView;
	}
}

web.xml
web.xml dosyamizi guncelleyelim.

...
	<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
			 /WEB-INF/03.multiActionController.xml
                         ........
			--> 			
			 /WEB-INF/16.xmlViewResolver.xml			
			</param-value> 			
		 </init-param>	
		<load-on-startup>1</load-on-startup>
	</servlet>
...

JSP dosyalarimiz basit sekilde olacak ;

myPage1.jsp

<title>XmlViewResolver Page1</title>
</head>
<body>
	<h2>${message}</h2>
</body>

bu bolumdeki diger JSP sayfalari da benzer sekilde olacak.

Ornegimizi test edelim ;

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

page1

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

page2

ResourceBundleViewResolver

ResourceBundleViewResolver , varsayilan olarak classpath’te views.properties dosyasini arayacaktir. Bu dosyada view dosyalari icin tanimlar yer alacaktir.

17.resourceBundleViewResolver.xml

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
	<property name="basename" value="17-viewResolver-views" />
</bean>

17-viewResolver-views.properties
Dosya icerigini inceleyecek olursak .(class) ve .url property tanimlarini kullaniyoruz.
myBeanViewNamePage3 ve myBeanViewNamePage4 degerleri ile Controller sinifimizda donen degerler eslesecektir.

Not : Bu dosya classpath’te olmalidir. Bu nedenle yeni bir source folder ekledim.
Not2 : Dosya ismi olarak 17.viewResolver.views.properties olarak yaptigimda problemle karsilastim. Bu nedenle (-) karakteri kullandim.

 

myBeanViewNamePage3.(class)=org.springframework.web.servlet.view.JstlView
myBeanViewNamePage3.url=/WEB-INF/jsp/17.resourceBundleViewResolver.view/myPage3.jsp

myBeanViewNamePage4.(class)=org.springframework.web.servlet.view.JstlView
myBeanViewNamePage4.url=/WEB-INF/jsp/17.resourceBundleViewResolver.view/myPage4.jsp

ResolverController2.java
Controller sinifimizda view name olarak “myBeanViewNamePage3” ve “myBeanViewNamePage4” degerini kullaniyoruz. Bu degerler properties dosyamizdaki degerler ile eslesmektedir.

package _17.resourceBundleViewResolver.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ResolverController2 {

	@RequestMapping("/page3")
	public ModelAndView handleViewResolverForPage3() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "page3");
		modelAndView.setViewName("myBeanViewNamePage3");
		return modelAndView;
	}

	@RequestMapping("/page4")
	public ModelAndView handleViewResolverForPage4() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "page4");
		modelAndView.setViewName("myBeanViewNamePage4");
		return modelAndView;
	}
}

web.xml
web.xml dosyamizi guncelleyelim.
/WEB-INF/17.resourceBundleViewResolver.xml  tanimini ekleyelim.

...
	<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
			 /WEB-INF/03.multiActionController.xml
			--> 			
			 /WEB-INF/17.resourceBundleViewResolver.xml					
			</param-value> 			
		 </init-param>	
		<load-on-startup>1</load-on-startup>
	</servlet>
...
http://localhost:8080/injavawetrust.springmvc/page3

page3

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

page4

Not : ResourceBundleViewResolver kullanilarak farkli Locale’e gore farkli JSP sayfalari kullanilabilir.

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.