Spring MVC – 19 – ViewResolver – 03 | XsltViewResolver

Merhaba arkadaslar
Bu bolumde bir baska ViewResolver implementation olarak XsltViewResolver ‘i kullanacagiz.
Oncelikle XSL ve XSLT hakkinda on bilgi edinelim ;

XSL (Extensible Stylesheet Language) anlamina gelmektedir. World Wide Web Consortium (W3C) tarafindan XML-based Stylesheet language ihtiyacina yonelik gelistirilmeye baslanmistir.
CSS (Cascading Style Sheets) –> HTML dosyalari icin Stylesheet
XSL (Extensible Stylesheet Language) –> XML dosyalari icin Stylesheet
EXtensible Stylesheet Language Transformation, XSLT  olarak kisaltilmaktadir.
XSLT , XML dosya formatini baska bir formata (XML , XHTML) donusturmek icin kullanilmaktadir.
Daha fazla bilgi icin ;

 

20.xsltViewResolver.xml
XsltViewResolver icin konfigurasyona dikkat edecek olursak InternalResourceViewResolver ‘a benzer sekilde suffix ve prefix property degerlerini kullaniyoruz.
Burada bir farklilik olarak view’lerimiz JSP dosyalari degil XSL dosyalari olacagi icin .xsl olarak suffix tanimliyoruz.
...
<context:component-scan base-package="_20.xsltViewResolver.controller" />

<bean class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.xslt.XsltView"/>
	<property name="prefix" value="/WEB-INF/jsp/" />
	<property name="suffix" value=".xsl" />
</bean>
...

EmployeeController.java
ServletContext.getResourceAsStream metodu yardimiyla projemizde yer alan kaynaklara erisim saglayabiliriz.
NOT : detayli bilgi icin Servlet&JSP yazilarima bakabilirsiniz.
Erisim sagladigimiz employees.xml dosyamizi attribute olarak ekliyoruz.

package _20.xsltViewResolver.controller;

import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmployeeController {

	@RequestMapping(value = "/getEmployees")
	public ModelAndView handleEmployeeList(HttpServletRequest request, HttpServletResponse response) {

		InputStream inputStream = request.getServletContext().getResourceAsStream("/resources/xml/employees.xml");
		ModelAndView model = new ModelAndView("20.xsltViewResolver.view/employeeList");
		model.addObject("xmlSource", inputStream);
		return model;
	}
}

employeeList.xsl
View olarak JSP sayfasi degil xsl dosyasi kullanacagiz. XML data dosyamizda yer alan employees , employee , ssn , firstname ,lastname , role gibi taglari burada kullaniyoruz.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="html" indent="yes" />
    <xsl:template match="/">
        <html>
            <body>
            	<div align="left">
                	<xsl:apply-templates />
                </div>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="employees">
        <table border="1">
            <xsl:for-each select="employee">
                <tr>
                    <td>
                        <xsl:value-of select="ssn" />
                    </td>
                    <td>                       
                        <xsl:value-of select="firstname" />                       
                    </td>
                    <td>
                        <xsl:value-of select="lastname" />
                    </td>
                    <td>
                        <xsl:value-of select="role" />
                    </td>
                    <td>
                        <xsl:value-of select="salary" />
                    </td>
                     
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

XML data dosyamizin icerigi ;

<?xml version='1.0' encoding='UTF-8'?>
<employees>
    <employee>
        <ssn>1000</ssn>
        <firstname>Levent</firstname>
        <lastname>Erguder</lastname>
        <role>Software Engineer</role>
        <salary>1000</salary>
    </employee>
    <employee>
        <ssn>1001</ssn>
        <firstname>Recep</firstname>
        <lastname>Bostanci</lastname>
        <role>Software Engineer</role>
        <salary>2000</salary>
    </employee>
    <employee>
        <ssn>1002</ssn>
        <firstname>Burak</firstname>
        <lastname>Okumus</lastname>
        <role>Manager</role>
        <salary>15000</salary>
    </employee>
    <employee>
        <ssn>1003</ssn>
        <firstname>Ozkan</firstname>
        <lastname>Ozbas</lastname>
        <role>Manager</role>
        <salary>15000</salary>
    </employee>
</employees>

web.xml
web.xml dosyamizi duzenleyelim ;

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

Ornegimizi calistirdigimizda ;

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

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.