View in spring
---------------------------
i. In a spring based application a model object is responsible to hold the data and this model object is used by the view component for rendering / generating the output.
ii. The internal code of AbstructController of handlerRequest() method takes care of setting the property the view or browser (check particular type request mustbe handled or not) and calling handleRequestInternal() method.
Example 1.
-------------------
package pack;
public class HellowWorldController extends AbstructController {
public HellowWorldController() {
String rmets[] = new String [2];
rmets[0]=”GET”;
rmets[0]=”POST”;
this.setSupportedMethods(rmets);
this.setCatcheSecond(300);
}
Protected ModelAndView handleRequestInternal (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName(“one”);
mav.addObject(“ourModel”,”Hello world”);
return mav;
}
}
The following steps will be carried out when the browser send the request /hellowWorld.htm given bellow:-
------------------------------------------------------------------------------------
i. Web container creates request, response object.
ii. Web container starts the execution of spring code available in DispatcherServlet by passing request, response object.
iii. The spring code calls the handleRequest() on HellowWorldContoller by passing request, response object.
iv. The JVM starts the execution of AbstructController class handdleRequest() method and it internally calls the handleRequestInternal() method.
v. The JVM starts the execution of handleRequestInternal() and creates ModelAndView object, pass the model and view in the object and return to the spring code.
vi. The spring code stores the model object in the request object with the attribute name ‘ourModel’.
vii. By using InternalResourceViewResolver ‘one’ will be converted as /pages/one.jsp.
viii. Spring code forward the request to the one.jsp and this JSP receives request and response object.
ix. Finally the browser receives the output generated by one.jsp that is acts as the view components.
x. The combination of DispatcherServlet and HellowWorldController acts as the controller component.
JSP code (one.jsp):-
Output of the one.jsp < br>
< %= request.getAttribute(“ourModel”); %> < br>
Model object => ${requestScope.ourModel} < br>
< % System.out.println(“------one.jsp---------“);
Example 2.
------------------------
We can use any type of object as a model object i.e. we can use a vector object as a model in place of string object. In the erlyer application we have used only one string object as a model object. Spring allows us multiple objects as model object.
Bean add in spring-servlet.xml -
< bean name=”/slist.htm” class=”pack.COne”>
< property name=”cacheSeconds” value=”0”>< /property>
< property name=”supportedMethods” value=”GET,POST”>< /property>
< /bean>
package pack;
public class COne extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
Mav.setViewName(“genRep”);
//code to get list
Vector slist = new Vector();
slist.add(“one”);
slist.add(“two”);
mav.setObject(“sl”, slist);
return mav;
}
}
JSP code (genRep.jsp):-
< %@ taglib prefix=”c” uri=http://java.sun.com/jstl/core%>
< %@ page isELIgnored=”true” %>
Output of the genRep.jsp < br>
List content < br>
< c:forEach var=”sname” items=”${requestScope.sl}” >
< c:out value=”${sname}” />< br>
< /c:forEach>
< % System.out.println(“------genRep.jsp---------“);
Example 3.
----------------------------
Bean add in spring-servlet.xml -
< bean name=”/slist.htm” class=”pack.CTwo”>
< property name=”cacheSeconds” value=”0”>< /property>
< property name=”supportedMethods” value=”GET, POST”>< /property>
< /bean>
package pack;
public class CTwo extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
Mav.setViewName(“genOP”);
//code to get map
Map < String, Object> mm = new Map < String, Object>();
mm.put(“mone”,”one”);
mm.put(“mtwo”,”two”);
mm.put(“mthr”,”thr”);
mav.setAllObjects(mm);
return mav;
}
}
The spring code stores the reference of the object available in the Map as part of request object before forward the request JSP.
JSP code (genOP.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ page isELIgnored=”true” %>
Output of the genOP.jsp < br>
< c:out value=”${requestScope.mone}” />< br>
< c:out value=”${requestScope.mtwo}” />< br>
< c:out value=”${requestScope.mthr}” />< br>
< % System.out.println(“------genRep.jsp---------“);
User define view class as view component (Our view class as view component):-
-----------------------------------------------------------------------------------
i. Spring is very flexible it allows us to use our own view classes for generating the output in place of the JSP.
ii. When we want to use our won view class then we use the method setView() in place of setViewName().
iii. A class that provides the implementation of org.springframework.web.servlet.View interface is called as view class.
iv. There is two methods in View interface.
=> getContentType() : should return a string indicating the type of contain ( text /xml, image/ JPG, text/html etc).
=> render() : as part of this method we must provide the code to generate the output has be send to the browser.
Example 4.
-------------------------
Bean add in spring-servlet.xml -
< bean name=”/slist.htm” class=”pack.CThr”>
< property name=”cacheSeconds” value=”0”>< /property>
< property name=”supportedMethods” value=”GET,POST”>< /property>
< /bean>
package pack;
public class CThr extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
Mav.setViewe(new pack.OurView());
//code to get map
Map < String, Object> mm = new Map < String, Object>();
mm.put(“mone”,”one”);
mm.put(“mtwo”,”two”);
mm.put(“mthr”,”thr”);
mav.setAllObjects(mm);
return mav;
}
}
package pack;
public class OurView implements View {
public String getContextType() {
return “text/html”;
}
Public void render( Map mm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
PrintWriter out = response.getWriter();
out.print(mm.get(“mone”));
out.print(mm.get(“mtwo”));
out.print(mm.get(“mthr”));
}
}
In case of above application instead of forwarding the reuest to the JSP file spring executes the render() on the View object.
User define view class as view component (Our PDF view class as view component):-
---------------------------------------------------------------------------
i. We can use our own view classes for generating the output in PDF format, XML format etc. For this, we must provide our own view classes as the sub classes of AbstructPdfView, AbstructJxlView.
ii. AbstructPdfView class implements the View interface. The render() method is provided in this class and the internal code of this class calls buildPdfDocument().
Example 5.
--------------------
Bean add in spring-servlet.xml -
< bean name=”/slist.htm” class=”pack.Cfiv”>
< property name=”cacheSeconds” value=”0”>< /property>
< property name=”supportedMethods” value=”GET,POST”>< /property>
< /bean>
package pack;
public class CFiv extends AbstructController {
protected ModelAndView handleRequestInternal (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
Mav.setViewe(new pack.GenPdfView());
//code to get list
Vector slist = new Vector();
slist.add(“one”);
slist.add(“two”);
mav.setObject(“sl”, slist);
return mav;
}
}
package pack;
public class GenPdfView extends AbstructPdfView {
protected void buildPdfDocument( Map model, Document doc, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Vector v=(Vector) model.get(“sl”);
Chunk heading = new Chunk( “List od Data”);
heading.setBackground(Color.RED);
heading.setUnderline(5.5f, -3f);
doc.add(heading);
Paragraph para = new Paragraph)(;
for(int i=0; i< v.size(); i++) {
para.add(v.get(i));
para.add(“\r\n”);
}
doc.add(para);
}
}
No comments:
Post a Comment