JOBS4TIMES jobs4timesLogo

Describing Spring View-Resolver and View

Agenda :
  1. Types of View Resolver
    1. UrlBasedViewResolver
    2. InternalResourceViewResolver
    3. ResourceBundleViewResolver
    4. BeanNameViewResolver
    5. XmlViewResolver

View Resolver

  1. UrlBasedViewResolver
  2. InternalResourceViewResolver
  3. ResourceBundleViewResolver
  4. BeanNameViewResolver
  5. XmlViewResolver

UrlBasedViewResolver

applicationContext.xml
<bean id="viewResolver" 
   class="org.springframework.web.servlet.view.UrlBasedViewResolver">
 <property name="prefix" value="/WEB-INF/jsps"/>
 <property name="suffix" value=".jsp"/>
 <property name="viewClass" 
   value="org.springframework.web.servlet.view.InternalResourceView"/>
</bean>

InternalResourceViewResolver

<bean id="viewResolver" 
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/jsps"/>
 <property name="suffix" value=".jsp"/>
 <property name="viewClass" 
  value="org.springframework.web.servlet.view.InternalResourceView"/>
</bean>

ResourceBundleViewResolver

<bean id="viewResolver" 
 class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
 <property name="basename" value="myViews"/>
</bean>
myViews.properties

BeanNameViewResolver

<bean id="viewResolver" 
 class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

XmlViewResolver

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

Configuring Multiple ViewResolvers :

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

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
 <property name="order" value="1"/>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"/>
  <property name="suffix" value=".jsp"/>
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  <property name="order" value="2"/>
</bean>

Understanding View :

MyGifView.java
 package com.view;

import java.util.Map;

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

import org.springframework.web.servlet.View;

public class MyGifView implements View {

public String getContentType() {
 return "image/gif";
}

public void render(Map model,
		   HttpServletRequest request,
		   HttpServletResponse response) throws Exception {
 byte[]	content=(byte[]) model.get("ResponseContent");
 ServletOutputStream sos=response.getOutputStream();
 sos.write(content);
 sos.flush();
 sos.close();
}//render()

}

Working with JSTL View :

  • JSP Standard Tag Library(JSTL) is a collection of custom tag libraries, which provide core functionality used for JSP document.
  • JSTL reduces the use of scriptlets in a JSP page.
  • The use of JSTL tags allows developers to use predefined tags instead of writing the Java code.
JSTL provides 4 types of Tag Libraries :
  1. Core Tags : used to process core operations in a JSP page.
  2. XML Tags : used for parsing, selecting, and transforming XML data in a JSP page.
  3. Format Tags : used for formatting the data used in a JSP page according to locale.
  4. SQL Tags : used to access the relational database used in a JSP page.
You Want more JSTL
Example for JSTLView
SerachEmployeeController.java

EmpDAO.java

EmpDAOImplDB.java

EmpRowMapper.java

EmpDetails.java

services-contect.xml

webConfig-context.xml

search.jsp

employeeDetails.jsp

employeeList.jsp

dbError.jsp

notANumberError.jsp

ApplicationResources_en.properties

web.xml

Generating Views using Velocity :

BACK