mvc-overview.notes
Spring Web MVC Overview

my-bean.json
{
"bean":{
"children":[
{
"children":[
{
"children":[
],
"stringValue":"ddd",
"intValue":400,
"booleanValue":false
}
],
"stringValue":"bbb",
"intValue":200,
"booleanValue":false
},
{
"children":[
],
"stringValue":"ccc",
"intValue":300,
"booleanValue":true
}
],
"stringValue":"aaa",
"intValue":100,
"booleanValue":true
}
}
my-bean.xml
<myBean>
<intValue>100</intValue>
<stringValue>aaa</stringValue>
<booleanValue>true</booleanValue>
<children>
<myBean>
<intValue>200</intValue>
<stringValue>bbb</stringValue>
<booleanValue>false</booleanValue>
<children>
<myBean>
<intValue>400</intValue>
<stringValue>ddd</stringValue>
<booleanValue>false</booleanValue>
<children/>
</myBean>
</children>
</myBean>
<myBean>
<intValue>300</intValue>
<stringValue>ccc</stringValue>
<booleanValue>true</booleanValue>
<children/>
</myBean>
</children>
</myBean>
using-lishys-notes.notes
Guide
The guide pane on the right contains information organized into sections.
Use the

and

buttons to move through the sections, or click an accordion tab (shown below) to go directly to a particular topic.

Code Reference
Each paragraph in the guide is usually associated with a particular piece of code. An orange arrow indicates that the code displayed in the middle pane is relevant to the guide text.

If the arrow is gray, simply click anywhere on the paragraph and the relevant code will be displayed.

Code Highlighter
The highlight feature identifies areas of the code which relate to a word or term in the guide. Hover the mouse over any underlined word and the relevant code will be highlighted.
You can disable the highlighting feature at any time by clicking on the

button.
Don't forget to make the appropriate code page visible by clicking on any paragraph with a gray arrow.
ExcelPageView.java
package lishy.views.web;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.web.servlet.view.document.AbstractJExcelView;
public class ExcelPageView extends AbstractJExcelView {
@Override
protected void buildExcelDocument(Map<String, Object> model,
WritableWorkbook wb,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
int[] numbers = (int[]) model.get("numbers");
int size = numbers.length;
WritableSheet sheet = wb.createSheet("random numbers", 0);
sheet.addCell(new Label(0, 0, "Numbers"));
for (int i=0; i < size; i++) {
sheet.addCell(new Number(0, i+1, numbers[i]));
}
sheet.addCell(new Formula(0, size+1, "SUM(A1:A" + (size+1) + ")"));
}
}
PdfPageView.java
package lishy.views.web;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class PdfPageView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model,
Document doc,
PdfWriter writer,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
doc.add( new Paragraph((String) model.get("message")));
}
}
ViewController.java
package lishy.views.web;
import java.io.StringReader;
import lishy.views.MyBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ViewController {
@RequestMapping("/message/{view}")
public ModelAndView messageData(@PathVariable("view") String view) {
return new ModelAndView(view, "message", "A message from the model");
}
@RequestMapping("/xml/{view}")
public ModelAndView xmlData(@PathVariable("view") String view) {
final StringReader xmlReader = new StringReader(
"<?xml version='1.0' encoding='ISO-8859-1'?>" +
"<message>This is added to the model in the XML</message>");
return new ModelAndView(view, "xmlSource", xmlReader);
}
@RequestMapping("/numbers/{view}")
public ModelAndView excelData(@PathVariable("view") String view) {
return new ModelAndView(view, "numbers", new int[]{10,30,60,5,90,12});
}
@RequestMapping("/bean/{view}")
public ModelAndView beanData(@PathVariable("view") String view) {
return new ModelAndView(view, "bean", populateBean());
}
private MyBean populateBean() {
MyBean parent = new MyBean(100, "aaa", true);
MyBean firstChild = new MyBean(200, "bbb", false);
MyBean secondChild = new MyBean(300, "ccc", true);
MyBean grandChild = new MyBean(400, "ddd", false);
firstChild.addChild(grandChild);
parent.addChild(firstChild);
parent.addChild(secondChild);
return parent;
}
}
MyBean.java
package lishy.views;
import java.util.ArrayList;
import java.util.List;
public class MyBean {
private int intValue;
private String stringValue;
private boolean booleanValue;
private List<MyBean> children = new ArrayList<MyBean>();
public MyBean(){};
public MyBean(int intValue, String stringValue, boolean booleanValue) {
this.intValue = intValue;
this.stringValue = stringValue;
this.booleanValue = booleanValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public boolean isBooleanValue() {
return booleanValue;
}
public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
public List<MyBean> getChildren() {
return children;
}
public void addChild(MyBean child) {
this.children.add(child);
}
public String toString() {
return toString(" ");
}
public String toString(String indent) {
StringBuffer result = new StringBuffer();
result.append(getIntValue() + ", " +
getStringValue() + ", " +
isBooleanValue());
for (MyBean child : getChildren()) {
result.append("\n" + indent + child.toString(indent + " "));
}
return result.toString();
}
}
jsp-view.jsp
<html>
<body>
<h1>JSP View</h1>
<p style="color:green">${message}</p>
<p>
<a href="../../index.html">back</a>
</p>
</body>
</html>
xslt-view.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<body>
<h1>XSLT View</h1>
<p style="color:green"><xsl:value-of select="."/>
and rendered using XSLT
</p>
<p>
<a href="../../index.html">back</a>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
views-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="lishy.views.web"/>
<bean class="org.springframework.web.servlet.view.XmlViewResolver"/>
</beans>
views.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="jsp-view"
class="org.springframework.web.servlet.view.JstlView"
p:url="/WEB-INF/jsp/jsp-view.jsp"/>
<bean id="xslt-view"
class="org.springframework.web.servlet.view.xslt.XsltView"
p:url="/WEB-INF/xslt/xslt-view.xslt"
p:cacheTemplates="false"/>
<bean id="pdf-view" class="lishy.views.web.PdfPageView"/>
<bean id="excel-view" class="lishy.views.web.ExcelPageView"/>
<bean id="json-view"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
<bean id="marshaller-view"
class="org.springframework.web.servlet.view.xml.MarshallingView"
p:marshaller-ref="marshaller"
p:modelKey="bean"/>
<bean id="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="myBean">lishy.views.MyBean</prop>
</props>
</property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>Views</display-name>
<servlet>
<servlet-name>views</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>views</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
<html>
<body>
<h1>Views</h1>
<p>
<a href="app/message/jsp-view">JSP</a>
</p>
<p>
<a href="app/xml/xslt-view">XSLT</a>
</p>
<p>
<a href="app/message/pdf-view">PDF</a>
</p>
<p>
<a href="app/numbers/excel-view">Excel</a>
</p>
<p>
<a href="app/bean/json-view">JSON</a>
</p>
<p>
<a href="app/bean/marshaller-view">XML Marshaller</a>
</p>
</body>
</html>