diff --git a/spring-framework-reference/src/rest.xml b/spring-framework-reference/src/rest.xml
index 91d9f837ec..9edd32c452 100644
--- a/spring-framework-reference/src/rest.xml
+++ b/spring-framework-reference/src/rest.xml
@@ -38,120 +38,6 @@
configuration to understand the general programming model.
-
-
- Views
-
- Several views were added in Spring 3 to help support creating
- RESTful services. They are:
-
-
-
- AbstractAtomFeedView - returns an Atom
- feed
-
-
-
- AbstractRssFeedView - returns a RSS
- feed
-
-
-
- MarshallingView - returns an XML
- representation using Spring's Object to XML mapping (OXM)
- functionality
-
-
-
-
- Available separately is the
- JacksonJsonView included as part of the Spring
- JavaScript project.
-
-
-
- Feed Views
-
- Both AbstractAtomFeedView and
- AbstractRssFeedView inherit from the base class
- AbstractFeedView and are used to provide Atom
- and RSS Feed views respectfully. They are based on java.net's ROME project and are located
- in the package
- org.springframework.web.servlet.view.feed.
-
- AbstractAtomFeedView requires you to
- implement the buildFeedEntries method and
- optionally override the buildFeedMetadata
- method (the default implementation is empty), as shown below
-
- public class SampleContentAtomView extends AbstractAtomFeedView {
-
- @Override
- protected void buildFeedMetadata(Map<String, Object> model, Feed feed,
- HttpServletRequest request) {
- // implementation omitted
- }
-
- @Override
- protected List<Entry> buildFeedEntries(Map<String, Object> model,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- // implementation omitted
- }
-}
-
- Similar requirements apply for implementing
- AbstractRssFeedView, as shown below
-
- public class SampleContentAtomView extends AbstractRssFeedView {
-
- @Override
- protected void buildFeedMetadata(Map<String, Object> model, Channel feed,
- HttpServletRequest request) {
- // implementation omitted
- }
-
- @Override
- protected List<Item> buildFeedItems(Map<String, Object> model,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- // implementation omitted
- }
-
-}
-
- The buildFeedItems and
- buildFeedEntires pass in the HTTP request in
- case you need to access the Locale. The HTTP response is passed in
- only for the setting of cookies or other HTTP headers. The feed will
- automatically be written to the response object after the method
- returns.
-
- For an example of creating a Atom view please refer to Alef
- Arendsen's SpringSource TeamBlog entry.
-
-
-
- XML Marshalling View
-
- The MarhsallingView uses a XML
- Marshaller defined in the
- org.springframework.oxm package to render the
- response content as XML. The object to be marshalled can be set
- explicitly using MarhsallingView's
- modelKey bean property. Alternatively, the view
- will iterate over all model properties marhsall only those types that
- are supported by the Marshaller. For
- more information on the functionality in the
- org.springframework.oxm package refer to the
- chapter Marshalling XML using O/X
- Mappers.
-
-
-
HTTP Method Conversion
diff --git a/spring-framework-reference/src/view.xml b/spring-framework-reference/src/view.xml
index c3a62ea67a..3e462f1226 100644
--- a/spring-framework-reference/src/view.xml
+++ b/spring-framework-reference/src/view.xml
@@ -1,7 +1,6 @@
-
+"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
View technologies
@@ -24,7 +23,8 @@
Spring provides a couple of out-of-the-box solutions for JSP and
JSTL views. Using JSP or JSTL is done using a normal view resolver defined
in the WebApplicationContext. Furthermore,
- of course you need to write some JSPs that will actually render the view.
+ of course you need to write some JSPs that will actually render the
+ view.
View resolvers
@@ -33,44 +33,46 @@
Spring, for JSPs you'll need a view resolver that will resolve your
views. The most commonly used view resolvers when developing with JSPs
are the InternalResourceViewResolver and the
- ResourceBundleViewResolver. Both are declared in the
- WebApplicationContext:
+ ResourceBundleViewResolver. Both are declared in
+ the WebApplicationContext:
- <!-- the ResourceBundleViewResolver -->
-
-
+ <!-- the ResourceBundleViewResolver -->
+<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
+ <property name="basename" value="views"/>
+</bean>
-]]># And a sample properties file is uses (views.properties in WEB-INF/classes):# And a sample properties file is uses (views.properties in WEB-INF/classes):
welcome.class=org.springframework.web.servlet.view.JstlView
welcome.url=/WEB-INF/jsp/welcome.jsp
productList.class=org.springframework.web.servlet.view.JstlView
-productList.url=/WEB-INF/jsp/productlist.jsp]]>
-
- As you can see, the ResourceBundleViewResolver needs
- a properties file defining the view names mapped to 1) a class and 2) a URL. With a
- ResourceBundleViewResolver you can mix different types of views using
- only one resolver.
-
-
-
-
-
-]]>
-
- The InternalResourceBundleViewResolver can be configured for using
- JSPs as described above. As a best practice, we strongly encourage
- placing your JSP files in a directory under the 'WEB-INF' directory, so
- there can be no direct access by clients.
+productList.url=/WEB-INF/jsp/productlist.jsp
+
+ As you can see, the
+ ResourceBundleViewResolver needs a properties
+ file defining the view names mapped to 1) a class and 2) a URL. With a
+ ResourceBundleViewResolver you can mix different
+ types of views using only one resolver.
+
+ <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+ <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
+ <property name="prefix" value="/WEB-INF/jsp/"/>
+ <property name="suffix" value=".jsp"/>
+</bean>
+
+ The InternalResourceBundleViewResolver can
+ be configured for using JSPs as described above. As a best practice, we
+ strongly encourage placing your JSP files in a directory under the
+ 'WEB-INF' directory, so there can
+ be no direct access by clients.'Plain-old' JSPs versus JSTL
- When using the Java Standard Tag Library you must use a special view
- class, the JstlView, as JSTL needs some preparation
- before things such as the i18N features will work.
+ When using the Java Standard Tag Library you must use a special
+ view class, the JstlView, as JSTL needs some
+ preparation before things such as the i18N features will work.
@@ -83,10 +85,10 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
HTML escaping features to enable or disable
escaping of characters.
- The tag library descriptor (TLD) is included in the
- spring.jar as well in the distribution itself.
- Further information about the individual tags can be found in the appendix entitled
- .
+ The tag library descriptor (TLD) is included in the spring.jar as well in the distribution
+ itself. Further information about the individual tags can be found in
+ the appendix entitled .
@@ -95,11 +97,12 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
As of version 2.0, Spring provides a comprehensive set of data
binding-aware tags for handling form elements when using JSP and Spring
Web MVC. Each tag provides support for the set of attributes of its
- corresponding HTML tag counterpart, making the tags familiar and intuitive
- to use. The tag-generated HTML is HTML 4.01/XHTML 1.0 compliant.
+ corresponding HTML tag counterpart, making the tags familiar and
+ intuitive to use. The tag-generated HTML is HTML 4.01/XHTML 1.0
+ compliant.
- Unlike other form/input tag libraries, Spring's form tag library is
- integrated with Spring Web MVC, giving the tags access to the command
+ Unlike other form/input tag libraries, Spring's form tag library
+ is integrated with Spring Web MVC, giving the tags access to the command
object and reference data your controller deals with. As you will see in
the following examples, the form tags make JSPs easier to develop, read
and maintain.
@@ -115,23 +118,23 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
spring.jar. The library descriptor is called
spring-form.tld.
- To use the tags from this library, add the following directive to
- the top of your JSP page:
+ To use the tags from this library, add the following directive
+ to the top of your JSP page:<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
- ... where form is the tag name prefix you want
- to use for the tags from this library.
+ ... where form is the tag name prefix you
+ want to use for the tags from this library.The form tag
- This tag renders an HTML 'form' tag and exposes a binding path to
- inner tags for binding. It puts the command object in the
+ This tag renders an HTML 'form' tag and exposes a binding path
+ to inner tags for binding. It puts the command object in the
PageContext so that the command object can be
- accessed by inner tags. All the other tags in this library are
- nested tags of the form tag.
+ accessed by inner tags. All the other tags in this library
+ are nested tags of the form tag.Let's assume we have a domain object called
User. It is a JavaBean with properties such as
@@ -160,9 +163,9 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
The firstName and lastName
values are retrieved from the command object placed in the
- PageContext by the page controller. Keep
- reading to see more complex examples of how inner tags are used with the
- form tag.
+ PageContext by the page controller.
+ Keep reading to see more complex examples of how inner tags are used
+ with the form tag.The generated HTML looks like a standard form:
@@ -185,12 +188,13 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
</form>
The preceding JSP assumes that the variable name of the form
- backing object is 'command'. If you have put the form
- backing object into the model under another name (definitely a best
- practice), then you can bind the form to the named variable like
+ backing object is 'command'. If you have put the
+ form backing object into the model under another name (definitely a
+ best practice), then you can bind the form to the named variable like
so:
- <form:form commandName="user">
+ <form:form commandName="user">
<table>
<tr>
<td>First Name:</td>
@@ -220,11 +224,12 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>The checkbox tag
- This tag renders an HTML 'input' tag with type 'checkbox'.
+ This tag renders an HTML 'input' tag with type
+ 'checkbox'.
- Let's assume our User has preferences such
- as newsletter subscription and a list of hobbies. Below is an example of
- the Preferences class:
+ Let's assume our User has preferences
+ such as newsletter subscription and a list of hobbies. Below is an
+ example of the Preferences class:public class Preferences {
@@ -290,15 +295,16 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
</table>
</form:form>
- There are 3 approaches to the checkbox tag which
- should meet all your checkbox needs.
+ There are 3 approaches to the checkbox tag
+ which should meet all your checkbox needs. Approach One - When the bound value is of type
java.lang.Boolean, the
input(checkbox) is marked as 'checked' if the
- bound value is true. The value
- attribute corresponds to the resolved value of the
- setValue(Object) value property.
+ bound value is true. The
+ value attribute corresponds to the resolved
+ value of the setValue(Object) value
+ property.
@@ -313,8 +319,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
Approach Three - For any other bound value type, the
input(checkbox) is marked as 'checked' if the
- configured setValue(Object) is equal to the bound
- value.
+ configured setValue(Object) is equal to the
+ bound value.
@@ -336,16 +342,16 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
What you might not expect to see is the additional hidden field
after each checkbox. When a checkbox in an HTML page is
- not checked, its value will not be sent to the server
- as part of the HTTP request parameters once the form is submitted, so we
- need a workaround for this quirk in HTML in order for Spring form data
- binding to work. The checkbox tag follows the existing
- Spring convention of including a hidden parameter prefixed by an
- underscore ("_") for each checkbox. By doing this, you are effectively
- telling Spring that
- the checkbox was visible in the form and I want my object to
- which the form data will be bound to reflect the state of the checkbox
- no matter what
+ not checked, its value will not be sent to the
+ server as part of the HTTP request parameters once the form is
+ submitted, so we need a workaround for this quirk in HTML in order for
+ Spring form data binding to work. The checkbox tag
+ follows the existing Spring convention of including a hidden parameter
+ prefixed by an underscore ("_") for each checkbox. By doing this, you
+ are effectively telling Spring that
+ the checkbox was visible in the form and I want my object
+ to which the form data will be bound to reflect the state of the
+ checkbox no matter what.
@@ -355,15 +361,16 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
'checkbox'.
Building on the example from the previous
- checkbox tag section. Sometimes you prefer not to
- have to list all the possible hobbies in your JSP page. You would rather
- provide a list at runtime of the available options and pass that in to
- the tag. That is the purpose of the checkboxes
- tag. You pass in an Array, a
- List or a Map containing
- the available options in the "items" property. Typically the bound
- property is a collection so it can hold multiple values selected by the
- user. Below is an example of the JSP using this tag:
+ checkbox tag section. Sometimes you prefer not
+ to have to list all the possible hobbies in your JSP page. You would
+ rather provide a list at runtime of the available options and pass
+ that in to the tag. That is the purpose of the
+ checkboxes tag. You pass in an
+ Array, a List or a
+ Map containing the available options in the
+ "items" property. Typically the bound property is a collection so it
+ can hold multiple values selected by the user. Below is an example of
+ the JSP using this tag:
<form:form>
@@ -381,18 +388,18 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>This example assumes that the "interestList" is a
List available as a model attribute containing
strings of the values to be selected from. In the case where you use a
- Map, the map entry key will be used as the value and the map entry's value
- will be used as the label to be displayed. You can also use a custom
- object where you can provide the property names for the value using
- "itemValue" and the label using "itemLabel".
+ Map, the map entry key will be used as the value and the map entry's
+ value will be used as the label to be displayed. You can also use a
+ custom object where you can provide the property names for the value
+ using "itemValue" and the label using "itemLabel".
The radiobutton tagThis tag renders an HTML 'input' tag with type 'radio'.
- A typical usage pattern will involve multiple tag instances bound
- to the same property but with different values.
+ A typical usage pattern will involve multiple tag instances
+ bound to the same property but with different values.<tr>
<td>Sex:</td>
@@ -412,11 +419,11 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
this usage you would use the radiobuttons tag.
You pass in an Array, a
List or a Map containing
- the available options in the "items" property. In the case where you use
- a Map, the map entry key will be used as the value and the map entry's
- value will be used as the label to be displayed. You can also use a
- custom object where you can provide the property names for the value
- using "itemValue" and the label using "itemLabel".
+ the available options in the "items" property. In the case where you
+ use a Map, the map entry key will be used as the value and the map
+ entry's value will be used as the label to be displayed. You can also
+ use a custom object where you can provide the property names for the
+ value using "itemValue" and the label using "itemLabel".
<tr>
<td>Sex:</td>
@@ -438,8 +445,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
</tr>
Please note that by default, the password value is
- not shown. If you do want the password value to be
- shown, then set the value of the 'showPassword'
+ not shown. If you do want the password value to
+ be shown, then set the value of the 'showPassword'
attribute to true, like so.<tr>
@@ -465,8 +472,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
<td><form:select path="skills" items="${skills}"/></td>
</tr>
- If the User's skill were in Herbology, the HTML
- source of the 'Skills' row would look like:
+ If the User's skill were in Herbology, the
+ HTML source of the 'Skills' row would look like:<tr>
<td>Skills:</td>
@@ -496,8 +503,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
</td>
</tr>
- If the User's house was in Gryffindor, the HTML
- source of the 'House' row would look like:
+ If the User's house was in Gryffindor, the
+ HTML source of the 'House' row would look like:<tr>
<td>House:</td>
@@ -545,17 +552,18 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>As the example shows, the combined usage of an
option tag with the options tag
- generates the same standard HTML, but allows you to explicitly specify a
- value in the JSP that is for display only (where it belongs) such as the
- default string in the example: "-- Please Select".
-
- The items attribute is typically populated with a
- collection or array of item objects. itemValue and
- itemLabel simply refer to bean properties of those
- item objects, if specified; otherwise, the item objects themselves will
- be stringified. Alternatively, you may specify a Map
- of items, in which case the map keys are interpreted as option values and
- the map values correspond to option labels. If itemValue
+ generates the same standard HTML, but allows you to explicitly specify
+ a value in the JSP that is for display only (where it belongs) such as
+ the default string in the example: "-- Please Select".
+
+ The items attribute is typically populated
+ with a collection or array of item objects.
+ itemValue and itemLabel simply
+ refer to bean properties of those item objects, if specified;
+ otherwise, the item objects themselves will be stringified.
+ Alternatively, you may specify a Map of items, in
+ which case the map keys are interpreted as option values and the map
+ values correspond to option labels. If itemValue
and/or itemLabel happen to be specified as well,
the item value property will apply to the map key and the item label
property will apply to the map value.
@@ -576,15 +584,15 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
The hidden tag
- This tag renders an HTML 'input' tag with type 'hidden' using the
- bound value. To submit an unbound hidden value, use the HTML
+ This tag renders an HTML 'input' tag with type 'hidden' using
+ the bound value. To submit an unbound hidden value, use the HTML
input tag with type 'hidden'.<form:hidden path="house" />
- If we choose to submit the 'house' value as a hidden one, the HTML
- would look like:
+ If we choose to submit the 'house' value as a hidden one, the
+ HTML would look like:<input name="house" type="hidden" value="Gryffindor"/>
@@ -598,8 +606,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
created by any validators associated with your controller.Let's assume we want to display all error messages for the
- firstName and lastName fields once
- we submit the form. We have a validator for instances of the
+ firstName and lastName fields
+ once we submit the form. We have a validator for instances of the
User class called
UserValidator.
@@ -682,8 +690,8 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
- The example below will display a list of errors at the top of the
- page, followed by field-specific errors next to the fields:
+ The example below will display a list of errors at the top of
+ the page, followed by field-specific errors next to the fields:<form:form>
<form:errors path="*" cssClass="errorBox" />
@@ -742,12 +750,12 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>NOTE: This section focuses on Spring's support
for Tiles 2 (the standalone version of Tiles, requiring Java 5+) in the
org.springframework.web.servlet.view.tiles2 package.
- Spring also continues to support Tiles 1.x (a.k.a. "Struts Tiles",
- as shipped with Struts 1.1+; compatible with Java 1.4) in the original
- org.springframework.web.servlet.view.tiles package.
-
+ Spring also continues to support Tiles 1.x (a.k.a. "Struts Tiles", as
+ shipped with Struts 1.1+; compatible with Java 1.4) in the original
+ org.springframework.web.servlet.view.tiles
+ package.
-
+ DependenciesTo be able to use Tiles you have to have a couple of additional
@@ -758,18 +766,22 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
Tiles version 2.0.4 or higher
+
Commons BeanUtils
+
Commons Digester
+
Commons Logging
- These dependencies are all available in the Spring distribution.
+ These dependencies are all available in the Spring
+ distribution.
@@ -778,198 +790,214 @@ productList.url=/WEB-INF/jsp/productlist.jsp]]>
To be able to use Tiles, you have to configure it using files
containing definitions (for basic information on definitions and other
Tiles concepts, please have a look at ). In Spring this is done
- using the TilesConfigurer. Have a look at the
- following piece of example ApplicationContext configuration:
-
-
-
-
- /WEB-INF/defs/general.xml
- /WEB-INF/defs/widgets.xml
- /WEB-INF/defs/administrator.xml
- /WEB-INF/defs/customer.xml
- /WEB-INF/defs/templates.xml
-
-
-]]>
+ url="http://tiles.apache.org" />). In Spring this is done using the
+ TilesConfigurer. Have a look at the following
+ piece of example ApplicationContext configuration:
+
+ <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
+ <property name="definitions">
+ <list>
+ <value>/WEB-INF/defs/general.xml</value>
+ <value>/WEB-INF/defs/widgets.xml</value>
+ <value>/WEB-INF/defs/administrator.xml</value>
+ <value>/WEB-INF/defs/customer.xml</value>
+ <value>/WEB-INF/defs/templates.xml</value>
+ </list>
+ </property>
+</bean>As you can see, there are five files containing definitions, which
- are all located in the 'WEB-INF/defs' directory.
- At initialization of the WebApplicationContext,
- the files will be loaded and the definitions factory will be initialized.
- After that has been done, the Tiles includes in the definition files can be used
- as views within your Spring web application. To be able to use the views
- you have to have a ViewResolver just as with any
- other view technology used with Spring. Below you can find two
- possibilities, the UrlBasedViewResolver and
- the ResourceBundleViewResolver.
+ are all located in the 'WEB-INF/defs' directory. At initialization
+ of the WebApplicationContext, the files
+ will be loaded and the definitions factory will be initialized. After
+ that has been done, the Tiles includes in the definition files can be
+ used as views within your Spring web application. To be able to use the
+ views you have to have a ViewResolver
+ just as with any other view technology used with Spring. Below you can
+ find two possibilities, the UrlBasedViewResolver
+ and the ResourceBundleViewResolver.
- UrlBasedViewResolver
+
+ UrlBasedViewResolver
+
- The UrlBasedViewResolver instantiates the given
- viewClass for each view it has to resolve.
-
-
-
-]]>
-
+ The UrlBasedViewResolver instantiates the
+ given viewClass for each view it has to
+ resolve.
+
+ <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
+ <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
+</bean>
- ResourceBundleViewResolver
+
+ ResourceBundleViewResolver
+
- The ResourceBundleViewResolver has to be provided with a
- property file containing viewnames and viewclasses the resolver can
- use:
-
-
-
-]]>
+ The ResourceBundleViewResolver has to be
+ provided with a property file containing viewnames and viewclasses the
+ resolver can use:
+
+ <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
+ <property name="basename" value="views"/>
+</bean>
- ...
welcomeView.class=org.springframework.web.servlet.view.tiles2.TilesView
-welcomeView.url=welcome ]]>(this is the name of a Tiles definition)(this is the name of a Tiles definition)
vetsView.class=org.springframework.web.servlet.view.tiles2.TilesView
-vetsView.url=vetsView ]]>(again, this is the name of a Tiles definition)(again, this is the name of a Tiles definition)
findOwnersForm.class=org.springframework.web.servlet.view.JstlView
findOwnersForm.url=/WEB-INF/jsp/findOwners.jsp
-...]]>
+...
- As you can see, when using the ResourceBundleViewResolver,
- you can easily mix different view technologies.
+ As you can see, when using the
+ ResourceBundleViewResolver, you can easily mix
+ different view technologies.
- Note that the TilesView class for Tiles 2 supports
- JSTL (the JSP Standard Tag Library) out of the box, whereas there is a separate
- TilesJstlView subclass in the Tiles 1.x support.
+ Note that the TilesView class for Tiles 2
+ supports JSTL (the JSP Standard Tag Library) out of the box, whereas
+ there is a separate TilesJstlView subclass in the
+ Tiles 1.x support.
- SimpleSpringPreparerFactory and SpringBeanPreparerFactory
+ SimpleSpringPreparerFactory and
+ SpringBeanPreparerFactoryAs an advanced feature, Spring also supports two special Tiles 2
- PreparerFactory implementations. Check out the
- Tiles documentation for details on how to use ViewPreparer
- references in your Tiles definition files.
-
- Specify SimpleSpringPreparerFactory to autowire
- ViewPreparer instances based on specified preparer classes, applying Spring's
- container callbacks as well as applying configured Spring BeanPostProcessors.
- If Spring's context-wide annotation-config has been activated, annotations in
- ViewPreparer classes will be automatically detected and applied.
- Note that this expects preparer classes in the Tiles definition files,
- just like the default PreparerFactory does.
-
- Specify SpringBeanPreparerFactory to operate on specified
- preparer names instead of classes, obtaining the corresponding
- Spring bean from the DispatcherServlet's application context. The full bean
- creation process will be in the control of the Spring application context in
- this case, allowing for the use of explicit dependency injection configuration,
- scoped beans etc. Note that you need to define one Spring bean definition per
- preparer name (as used in your Tiles definitions).
-
-
-
-
- /WEB-INF/defs/general.xml
- /WEB-INF/defs/widgets.xml
- /WEB-INF/defs/administrator.xml
- /WEB-INF/defs/customer.xml
- /WEB-INF/defs/templates.xml
-
-
-
- ]]><!-- resolving preparer names as Spring bean definition names -->
-
-]]>
-
+ PreparerFactory implementations. Check
+ out the Tiles documentation for details on how to use
+ ViewPreparer references in your Tiles
+ definition files.
+
+ Specify SimpleSpringPreparerFactory to
+ autowire ViewPreparer instances based on specified preparer classes,
+ applying Spring's container callbacks as well as applying configured
+ Spring BeanPostProcessors. If Spring's context-wide annotation-config
+ has been activated, annotations in ViewPreparer classes will be
+ automatically detected and applied. Note that this expects preparer
+ classes in the Tiles definition files, just like
+ the default PreparerFactory does.
+
+ Specify SpringBeanPreparerFactory to
+ operate on specified preparer names instead of
+ classes, obtaining the corresponding Spring bean from the
+ DispatcherServlet's application context. The full bean creation
+ process will be in the control of the Spring application context in
+ this case, allowing for the use of explicit dependency injection
+ configuration, scoped beans etc. Note that you need to define one
+ Spring bean definition per preparer name (as used in your Tiles
+ definitions).
+
+ <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
+ <property name="definitions">
+ <list>
+ <value>/WEB-INF/defs/general.xml</value>
+ <value>/WEB-INF/defs/widgets.xml</value>
+ <value>/WEB-INF/defs/administrator.xml</value>
+ <value>/WEB-INF/defs/customer.xml</value>
+ <value>/WEB-INF/defs/templates.xml</value>
+ </list>
+ </property>
+
+ <!-- resolving preparer names as Spring bean definition names -->
+ <property name="preparerFactoryClass"
+ value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/>
+
+</bean>
+ Velocity & FreeMarker
- Velocity and
- FreeMarker are two
- templating languages that can both be used as view technologies within
- Spring MVC applications. The languages are quite similar and serve similar
- needs and so are considered together in this section. For semantic and
- syntactic differences between the two languages, see the Velocity and FreeMarker are two templating
+ languages that can both be used as view technologies within Spring MVC
+ applications. The languages are quite similar and serve similar needs and
+ so are considered together in this section. For semantic and syntactic
+ differences between the two languages, see the FreeMarker web site.Dependencies
- Your web application will need to include
- velocity-1.x.x.jar or
- freemarker-2.x.jar in order to
- work with Velocity or FreeMarker respectively and
- commons-collections.jar
- needs also to be available for Velocity. Typically they are included in
- the WEB-INF/lib folder where they are guaranteed to
- be found by a J2EE server and added to the classpath for your
- application. It is of course assumed that you already have the
- spring.jar in your
- 'WEB-INF/lib' directory too!
- The latest stable Velocity, FreeMarker and Commons
- Collections jars are supplied with the Spring framework and can be
- copied from the relevant /lib/
- sub-directories. If you make use of Spring's 'dateToolAttribute' or
- 'numberToolAttribute' in your Velocity views, you will also need to include the
- velocity-tools-generic-1.x.jar
+ Your web application will need to include velocity-1.x.x.jar or freemarker-2.x.jar in order to work with
+ Velocity or FreeMarker respectively and commons-collections.jar needs also to be
+ available for Velocity. Typically they are included in the
+ WEB-INF/lib folder where they are guaranteed to be
+ found by a J2EE server and added to the classpath for your application.
+ It is of course assumed that you already have the spring.jar in your 'WEB-INF/lib' directory too! The latest
+ stable Velocity, FreeMarker and Commons Collections jars are supplied
+ with the Spring framework and can be copied from the relevant /lib/ sub-directories. If you make use of
+ Spring's 'dateToolAttribute' or 'numberToolAttribute' in your Velocity
+ views, you will also need to include the velocity-tools-generic-1.x.jarContext configurationA suitable configuration is initialized by adding the relevant
- configurer bean definition to your '*-servlet.xml' as shown below:
+ configurer bean definition to your '*-servlet.xml'
+ as shown below:
<!--
This bean sets up the Velocity environment for us based on a root path for templates.
Optionally, a properties file can be specified for more control over the Velocity
environment, but the defaults are pretty sane for file based template loading.
--->
-
-
+-->
+<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
+ <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
+</bean>
-]]><!--
+<!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
--->
-
-
-
-]]>
+-->
+<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
+ <property name="cache" value="true"/>
+ <property name="prefix" value=""/>
+ <property name="suffix" value=".vm"/>
+</bean>
- <!-- freemarker config -->
-
-
+ <!-- freemarker config -->
+<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
+ <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
+</bean>
-]]><!--
+<!--
View resolvers can also be configured with ResourceBundles or XML files. If you need
different view resolving based on Locale, you have to use the resource bundle resolver.
--->
-
-
-
-]]>
+-->
+<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
+ <property name="cache" value="true"/>
+ <property name="prefix" value=""/>
+ <property name="suffix" value=".ftl"/>
+</bean>
- For non web-apps add a VelocityConfigurationFactoryBean or a
- FreeMarkerConfigurationFactoryBean to your application context definition file.
+
+ For non web-apps add a
+ VelocityConfigurationFactoryBean or a
+ FreeMarkerConfigurationFactoryBean to your
+ application context definition file.
@@ -977,13 +1005,13 @@ findOwnersForm.url=/WEB-INF/jsp/findOwners.jsp
Creating templatesYour templates need to be stored in the directory specified by the
- *Configurer bean shown above. This document does not cover
- details of creating templates for the two languages - please see their
- relevant websites for information. If you use the view resolvers
+ *Configurer bean shown above. This document does not
+ cover details of creating templates for the two languages - please see
+ their relevant websites for information. If you use the view resolvers
highlighted, then the logical view names relate to the template file
names in similar fashion to
- InternalResourceViewResolver for JSP's. So if your
- controller returns a ModelAndView object containing a view name of
+ InternalResourceViewResolver for JSP's. So if
+ your controller returns a ModelAndView object containing a view name of
"welcome" then the resolvers will look for the
/WEB-INF/freemarker/welcome.ftl or
/WEB-INF/velocity/welcome.vm template as
@@ -1014,18 +1042,18 @@ findOwnersForm.url=/WEB-INF/jsp/findOwners.jsp
the bean definition for the Velocity config bean by replacing the
"configLocation" property with the following inline properties.
-
-
-
- file
-
+ <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
+ <property name="velocityProperties">
+ <props>
+ <prop key="resource.loader">file</prop>
+ <prop key="file.resource.loader.class">
org.apache.velocity.runtime.resource.loader.FileResourceLoader
-
- ${webapp.root}/WEB-INF/velocity
- false
-
-
-]]>
+ </prop>
+ <prop key="file.resource.loader.path">${webapp.root}/WEB-INF/velocity</prop>
+ <prop key="file.resource.loader.cache">false</prop>
+ </props>
+ </property>
+</bean>
Refer to the API
@@ -1046,16 +1074,16 @@ findOwnersForm.url=/WEB-INF/jsp/findOwners.jsp
freemarkerVariables property requires a
java.util.Map.
-
-
-
-
-
-
+ <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
+ <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
+ <property name="freemarkerVariables">
+ <map>
+ <entry key="xml_escape" value-ref="fmXmlEscape"/>
+ </map>
+ </property>
+</bean>
-]]>
+<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>See the FreeMarker documentation for details of settings and
variables as they apply to the Configuration
@@ -1588,20 +1616,24 @@ New York
My First WordsThis example is a trivial Spring application that creates a list
- of words in the Controller and adds them to the model
- map. The map is returned along with the view name of our XSLT view. See the section
- entitled for details of Spring Web MVC's
- Controller interface. The XSLT view will turn the list of
- words into a simple XML document ready for transformation.
+ of words in the Controller and adds them
+ to the model map. The map is returned along with the view name of our
+ XSLT view. See the section entitled
+ for details of Spring Web MVC's
+ Controller interface. The XSLT view will
+ turn the list of words into a simple XML document ready for
+ transformation.Bean definitionsConfiguration is standard for a simple Spring application. The
dispatcher servlet config file contains a reference to a
- ViewResolver, URL mappings and a single controller
- bean...
- ]]>
+ ViewResolver, URL mappings and a single
+ controller bean...
+
+ <bean id="homeController"class="xslt.HomeController"/>
+
... that encapsulates our word generation logic.
@@ -1609,9 +1641,10 @@ New York
Standard MVC controller codeThe controller logic is encapsulated in a subclass of
- AbstractController, with the handler method being defined like so...
-
- AbstractController, with the handler method
+ being defined like so...
+
+ protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
@@ -1624,22 +1657,21 @@ New York
map.put("wordList", wordList);
return new ModelAndView("home", map);
-}]]>
+}
So far we've done nothing that's XSLT specific. The model data
has been created in the same way as you would for any other Spring MVC
application. Depending on the configuration of the application now,
that list of words could be rendered by JSP/JSTL by having them added
as request attributes, or they could be handled by Velocity by adding
- the object to the VelocityContext. In
- order to have XSLT render them, they of course have to be converted into
- an XML document somehow.
- There are software packages available that will automatically 'domify'
- an object graph, but within Spring, you have complete flexibility to
- create the DOM from your model in any way you choose. This prevents
- the transformation of XML playing too great a part in the structure of
- your model data which is a danger when using tools to manage the
- domification process.
+ the object to the VelocityContext. In order to
+ have XSLT render them, they of course have to be converted into an XML
+ document somehow. There are software packages available that will
+ automatically 'domify' an object graph, but within Spring, you have
+ complete flexibility to create the DOM from your model in any way you
+ choose. This prevents the transformation of XML playing too great a
+ part in the structure of your model data which is a danger when using
+ tools to manage the domification process.
@@ -1648,14 +1680,16 @@ New York
In order to create a DOM document from our list of words or any
other model data, we must subclass the (provided)
org.springframework.web.servlet.view.xslt.AbstractXsltView
- class. In doing so, we must also typically implement the abstract method
- createXsltSource(..) method. The first parameter passed
- to this method is our model map. Here's the complete listing of the
- HomePage class in our trivial word application:
- createXsltSource(..) method. The first
+ parameter passed to this method is our model map. Here's the complete
+ listing of the HomePage class in our trivial
+ word application:
+
+
package xslt;
-]]>// imports omitted for brevity// imports omitted for brevity
public class HomePage extends AbstractXsltView {
@@ -1676,20 +1710,19 @@ public class HomePage extends AbstractXsltView {
return new DOMSource(root);
}
-}]]>
-
- A series of parameter name/value pairs can optionally be
- defined by your subclass which will be added to the transformation
- object. The parameter names must match those defined in your XSLT
- template declared with
- <xsl:param name="myParam">defaultValue</xsl:param>.
- To specify the parameters, override the
- getParameters() method of the
- AbstractXsltView class and return a
- Map of the name/value pairs. If your parameters
- need to derive information from the current request, you can override the
- getParameters(HttpServletRequest request) method instead.
+}
+ A series of parameter name/value pairs can optionally be defined
+ by your subclass which will be added to the transformation object. The
+ parameter names must match those defined in your XSLT template
+ declared with <xsl:param
+ name="myParam">defaultValue</xsl:param>. To specify
+ the parameters, override the getParameters()
+ method of the AbstractXsltView class and return
+ a Map of the name/value pairs. If your
+ parameters need to derive information from the current request, you
+ can override the getParameters(HttpServletRequest
+ request) method instead.
@@ -1699,47 +1732,51 @@ public class HomePage extends AbstractXsltView {
you're using an XML based view resolver as we did in the Velocity
examples above) looks like this for the one-view application that is
'My First Words':
- home.class=xslt.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt
-home.root=words]]>
- Here, you can see how the view is tied in
- with the HomePage class just written which handles the model
- domification in the first property '.class'. The 'stylesheetLocation'
- property points to the XSLT file which will handle the XML
- transformation into HTML for us and the final property '.root' is the
- name that will be used as the root of the XML document. This gets
- passed to the HomePage class above in the second parameter to the
- createXsltSource(..) method(s).
+home.root=words
+
+ Here, you can see how the view is tied in with the
+ HomePage class just written which handles the
+ model domification in the first property '.class'.
+ The 'stylesheetLocation' property points to the
+ XSLT file which will handle the XML transformation into HTML for us
+ and the final property '.root' is the name that
+ will be used as the root of the XML document. This gets passed to the
+ HomePage class above in the second parameter to
+ the createXsltSource(..) method(s).Document transformationFinally, we have the XSLT code used for transforming the above
- document. As shown in the above 'views.properties' file, the stylesheet is called
+ document. As shown in the above
+ 'views.properties' file, the stylesheet is called
'home.xslt' and it lives in the war file in the
'WEB-INF/xsl' directory.
-
-
-
-
-
-
-
-
- Hello!
-
-
My First Words
-
-
-
-
-
-
-
-
-
-]]>
+
+ <?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+ <xsl:output method="html" omit-xml-declaration="yes"/>
+
+ <xsl:template match="/">
+ <html>
+ <head><title>Hello!</title></head>
+ <body>
+ <h1>My First Words</h1>
+ <xsl:apply-templates/>
+ </body>
+ </html>
+ </xsl:template>
+
+ <xsl:template match="word">
+ <xsl:value-of select="."/><br/>
+ </xsl:template>
+
+</xsl:stylesheet>
@@ -1748,7 +1785,7 @@ home.root=words]]>
A summary of the files discussed and their location in the WAR
file is shown in the simplified WAR structure below.
-
+
ProjectRoot
|
+- WebContent
@@ -1773,11 +1810,11 @@ home.root=words]]>
| +- home.xslt
|
+- frontcontroller-servlet.xml
-
- You will also need to ensure that an XML parser and an XSLT engine are available on the
- classpath. JDK 1.4 provides them by default, and most J2EE containers
- will also make them available by default, but it's a possible source of
- errors to be aware of.
+
+ You will also need to ensure that an XML parser and an XSLT engine
+ are available on the classpath. JDK 1.4 provides them by default, and
+ most J2EE containers will also make them available by default, but it's
+ a possible source of errors to be aware of.
@@ -1843,14 +1880,14 @@ pdf.class=pdf.HomePage If you want to start with a
abstract classes in order to implement custom behavior in generating
our output documents. For Excel, this involves writing a subclass of
org.springframework.web.servlet.view.document.AbstractExcelView
- (for Excel files generated by POI)
- or org.springframework.web.servlet.view.document.AbstractJExcelView
- (for JExcelApi-generated Excel files).
- and implementing the buildExcelDocument
+ (for Excel files generated by POI) or
+ org.springframework.web.servlet.view.document.AbstractJExcelView
+ (for JExcelApi-generated Excel files). and implementing the
+ buildExcelDocument
- Here's the complete listing for our POI Excel view which displays
- the word list from the model map in consecutive rows of the first
- column of a new spreadsheet.. package excel;
+ Here's the complete listing for our POI Excel view which
+ displays the word list from the model map in consecutive rows of the
+ first column of a new spreadsheet.. package excel;
// imports omitted for brevity
@@ -1885,8 +1922,9 @@ public class HomePage extends AbstractExcelView {
}
}
}
-
- And this a view generating the same Excel file, now using JExcelApi: package excel;
+
+ And this a view generating the same Excel file, now using
+ JExcelApi: package excel;
// imports omitted for brevity
@@ -1908,13 +1946,12 @@ public class HomePage extends AbstractExcelView {
}
}
}
-
-
-
- Note the differences between the APIs. We've found that the
- JExcelApi is somewhat more intuitive and furthermore, JExcelApi
- has a bit better image-handling capabilities. There have been
- memory problems with large Excel file when using JExcelApi however.
+
+
+ Note the differences between the APIs. We've found that the
+ JExcelApi is somewhat more intuitive and furthermore, JExcelApi has a
+ bit better image-handling capabilities. There have been memory
+ problems with large Excel file when using JExcelApi however.If you now amend the controller such that it returns
xl as the name of the view (return new
@@ -1962,10 +1999,12 @@ public class PDFPage extends AbstractPdfView {
JasperReports
- JasperReports ()
- is a powerful open-source reporting engine that supports the creation of report
- designs using an easily understood XML file format. JasperReports is capable of
- rendering reports output into four different formats: CSV, Excel, HTML and PDF.
+ JasperReports () is a powerful
+ open-source reporting engine that supports the creation of report designs
+ using an easily understood XML file format. JasperReports is capable of
+ rendering reports output into four different formats: CSV, Excel, HTML and
+ PDF.Dependencies
@@ -1978,21 +2017,27 @@ public class PDFPage extends AbstractPdfView {
BeanShell
+
Commons BeanUtils
+
Commons Collections
+
Commons Digester
+
Commons Logging
+
iText
+
POI
@@ -2004,35 +2049,43 @@ public class PDFPage extends AbstractPdfView {
Configuration
- To configure JasperReports views in your Spring container configuration
- you need to define a ViewResolver to map view
- names to the appropriate view class depending on which format you want your
- report rendered in.
+ To configure JasperReports views in your Spring container
+ configuration you need to define a
+ ViewResolver to map view names to the
+ appropriate view class depending on which format you want your report
+ rendered in.
- Configuring the ViewResolver
+ Configuring the
+ ViewResolver
- Typically, you will use the ResourceBundleViewResolver
- to map view names to view classes and files in a properties file.
-
-
-
-]]>
+ Typically, you will use the
+ ResourceBundleViewResolver to map view names to
+ view classes and files in a properties file.
+
+ <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
+ <property name="basename" value="views"/>
+</bean>
- Here we've configured an instance of the ResourceBundleViewResolver
- class that will look for view mappings in the resource bundle with base name
- views. (The content of this file is described in the next section.)
+ Here we've configured an instance of the
+ ResourceBundleViewResolver class that will look
+ for view mappings in the resource bundle with base name
+ views. (The content of this file is described in
+ the next section.)Configuring the Views
- The Spring Framework contains five different View
- implementations for JasperReports, four of which correspond to one of the four output
- formats supported by JasperReports, and one that allows for the format to be determined at runtime:
+ The Spring Framework contains five different
+ View implementations for JasperReports,
+ four of which correspond to one of the four output formats supported
+ by JasperReports, and one that allows for the format to be determined
+ at runtime:
- JasperReports View classes
+ JasperReports View
+ classes
@@ -2042,6 +2095,7 @@ public class PDFPage extends AbstractPdfView {
Class Name
+
Render Format
@@ -2049,40 +2103,52 @@ public class PDFPage extends AbstractPdfView {
JasperReportsCsvView
+
CSV
+
JasperReportsHtmlView
+
HTML
+
JasperReportsPdfView
+
PDFJasperReportsXlsView
+
Microsoft Excel
+
JasperReportsMultiFormatView
- The view is decided upon at runtime
+
+ The view is decided
+ upon at runtime
- Mapping one of these classes to a view name and a report file is a matter of
- adding the appropriate entries into the resource bundle configured in the previous
- section as shown here:
+ Mapping one of these classes to a view name and a report file is
+ a matter of adding the appropriate entries into the resource bundle
+ configured in the previous section as shown here:
-
+ simpleReport.class=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
+simpleReport.url=/WEB-INF/reports/DataSourceReport.jasper
- Here you can see that the view with name simpleReport
- is mapped to the JasperReportsPdfView class, causing the
- output of this report to be rendered in PDF format. The url
- property of the view is set to the location of the underlying report file.
+ Here you can see that the view with name
+ simpleReport is mapped to the
+ JasperReportsPdfView class, causing the output
+ of this report to be rendered in PDF format. The
+ url property of the view is set to the location of
+ the underlying report file.
@@ -2094,33 +2160,35 @@ simpleReport.url=/WEB-INF/reports/DataSourceReport.jasper]]>
extension. Typically, you use the JasperReports Ant task to compile
your .jrxml design file into a
.jasper file before deploying it into your
- application. With the Spring Framework you can map either of these files to your
- report file and the framework will take care of compiling the
- .jrxml file on the fly for you. You should note
- that after a .jrxml file is compiled by the Spring Framework,
- the compiled report is cached for the lifetime of the application. To make
- changes to the file you will need to restart your application.
+ application. With the Spring Framework you can map either of these
+ files to your report file and the framework will take care of
+ compiling the .jrxml file on the fly for you. You
+ should note that after a .jrxml file is compiled by
+ the Spring Framework, the compiled report is cached for the lifetime
+ of the application. To make changes to the file you will need to
+ restart your application.
- Using JasperReportsMultiFormatView
-
- The JasperReportsMultiFormatView allows for
- report format to be specified at runtime. The actual rendering of the
- report is delegated to one of the other JasperReports view classes -
- the JasperReportsMultiFormatView class simply adds
- a wrapper layer that allows for the exact implementation to be
+ Using
+ JasperReportsMultiFormatView
+
+ The JasperReportsMultiFormatView allows
+ for report format to be specified at runtime. The actual rendering of
+ the report is delegated to one of the other JasperReports view classes
+ - the JasperReportsMultiFormatView class simply
+ adds a wrapper layer that allows for the exact implementation to be
specified at runtime.The JasperReportsMultiFormatView class
introduces two concepts: the format key and the discriminator key. The
- JasperReportsMultiFormatView class uses the mapping
- key to lookup the actual view implementation class and uses the format
- key to lookup up the mapping key. From a coding perspective you add an
- entry to your model with the formay key as the key and the mapping key
- as the value, for example:
+ JasperReportsMultiFormatView class uses the
+ mapping key to lookup the actual view implementation class and uses
+ the format key to lookup up the mapping key. From a coding perspective
+ you add an entry to your model with the formay key as the key and the
+ mapping key as the value, for example:
- public ModelAndView handleSimpleReportMulti(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String uri = request.getRequestURI();
@@ -2130,7 +2198,7 @@ HttpServletResponse response) throws Exception {
model.put("format", format);
return new ModelAndView("simpleReportMulti", model);
-}]]>
+}
In this example, the mapping key is determined from the
extension of the request URI and is added to the model under the
@@ -2141,55 +2209,57 @@ HttpServletResponse response) throws Exception {
By default the following mapping key mappings are configured in
JasperReportsMultiFormatView:
-
+
- JasperReportsXlsView
-
-
-
-
-
- So in the example above a request to URI /foo/myReport.pdf
- would be mapped to the JasperReportsPdfView class.
- You can override the mapping key to view class mappings using the
- formatMappings property of
- JasperReportsMultiFormatView.
+ So in the example above a request to URI /foo/myReport.pdf would
+ be mapped to the JasperReportsPdfView class. You
+ can override the mapping key to view class mappings using the
+ formatMappings property of
+ JasperReportsMultiFormatView.
@@ -2201,46 +2271,48 @@ HttpServletResponse response) throws Exception {
your report. For JasperReports this means you must pass in all report
parameters along with the report datasource. Report parameters are
simple name/value pairs and can be added be to the
- Map for your model as you would add any name/value
- pair.
+ Map for your model as you would add any
+ name/value pair.
When adding the datasource to the model you have two approaches to
choose from. The first approach is to add an instance of
- JRDataSource or a Collection type to the
- model Map under any arbitrary key. Spring will then
- locate this object in the model and treat it as the report datasource.
- For example, you may populate your model like so:
-
- JRDataSource or a
+ Collection type to the model
+ Map under any arbitrary key. Spring will
+ then locate this object in the model and treat it as the report
+ datasource. For example, you may populate your model like so:
+
+ private Map getModel() {
Map model = new HashMap();
Collection beanData = getBeanData();
model.put("myBeanData", beanData);
return model;
-}]]>
+}The second approach is to add the instance of
JRDataSource or Collection under a
specific key and then configure this key using the
reportDataKey property of the view class. In both
cases Spring will instances of Collection in a
- JRBeanCollectionDataSource instance. For example:
-
- JRBeanCollectionDataSource instance. For
+ example:
+
+ private Map getModel() {
Map model = new HashMap();
Collection beanData = getBeanData();
Collection someData = getSomeData();
model.put("myBeanData", beanData);
model.put("someData", someData);
return model;
-}]]>
+}
- Here you can see that two Collection
- instances are being added to the model. To ensure that the correct one
- is used, we simply modify our view configuration as appropriate:
-
- Here you can see that two Collection instances
+ are being added to the model. To ensure that the correct one is used, we
+ simply modify our view configuration as appropriate:
+
+ simpleReport.class=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
simpleReport.url=/WEB-INF/reports/DataSourceReport.jasper
-simpleReport.reportDataKey=myBeanData]]>
+simpleReport.reportDataKey=myBeanDataBe aware that when using the first approach, Spring will use the
first instance of JRDataSource or
@@ -2270,10 +2342,12 @@ simpleReport.reportDataKey=myBeanData]]>
report using Spring, your report file must be configured to accept
sub-reports from an external source. To do this you declare a
parameter in your report file like so:
-
- ]]>
-
- Then, you define your sub-report to use this sub-report parameter:
+
+ <parameter name="ProductsSubReport" class="net.sf.jasperreports.engine.JasperReport"/>
+
+ Then, you define your sub-report to use this sub-report
+ parameter:
+
<subreport>
<reportElement isPrintRepeatedValues="false" x="5" y="25" width="325"
height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
@@ -2284,20 +2358,20 @@ simpleReport.reportDataKey=myBeanData]]>
<subreportExpression class="net.sf.jasperreports.engine.JasperReport">
<![CDATA[$P{ProductsSubReport}]]></subreportExpression>
</subreport>
-
- This defines a master report file that
- expects the sub-report to be passed in as an instance of
+
+ This defines a master report file that expects the sub-report to
+ be passed in as an instance of
net.sf.jasperreports.engine.JasperReports under the
parameter ProductsSubReport. When configuring your
Jasper view class, you can instruct Spring to load a report file and
pass into the JasperReports engine as a sub-report using the
subReportUrls property:
-
-
-
-]]>
+
+ <property name="subReportUrls">
+ <map>
+ <entry key="ProductsSubReport" value="/WEB-INF/reports/subReportChild.jrxml"/>
+ </map>
+</property>Here, the key of the Map
corresponds to the name of the sub-report parameter in th report
@@ -2319,8 +2393,9 @@ simpleReport.reportDataKey=myBeanData]]>
the subReportDataKeys property of the your chosen
view class: <property name="subReportDataKeys"
value="SubReportData"/> Here, the key you supply MUST
- correspond to both the key used in your ModelAndView
- and the key used in your report design file.
+ correspond to both the key used in your
+ ModelAndView and the key used in your report design
+ file.
@@ -2332,12 +2407,12 @@ simpleReport.reportDataKey=myBeanData]]>
configure these exporter parameters declaratively in your Spring
configuration file using the exporterParameters
property of the view class. The exporterParameters
- property is typed as Map and in your configuration
- the key of an entry should be the fully-qualified name of a static field
- that contains the exporter parameter definition and the value of an
- entry should be the value you want to assign to the parameter. An
- example of this is shown below:
-
+ property is typed as Map and in your
+ configuration the key of an entry should be the fully-qualified name of
+ a static field that contains the exporter parameter definition and the
+ value of an entry should be the value you want to assign to the
+ parameter. An example of this is shown below:
+
<bean id="htmlReport" class="org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView">
<property name="url" value="/WEB-INF/reports/simpleReport.jrxml"/>
<property name="exporterParameters">
@@ -2351,11 +2426,93 @@ simpleReport.reportDataKey=myBeanData]]>
</map>
</property>
</bean>
-
- Here you can see that the JasperReportsHtmlView is
- being configured with an exporter parameter for
+
+ Here you can see that the
+ JasperReportsHtmlView is being configured with an
+ exporter parameter for
net.sf.jasperreports.engine.export.JRHtmlExporterParameter.HTML_FOOTER
which will output a footer in the resulting HTML.
-
\ No newline at end of file
+
+
+ Feed Views
+
+ Both AbstractAtomFeedView and
+ AbstractRssFeedView inherit from the base class
+ AbstractFeedView and are used to provide Atom and
+ RSS Feed views respectfully. They are based on java.net's ROME project and are located in
+ the package
+ org.springframework.web.servlet.view.feed.
+
+ AbstractAtomFeedView requires you to
+ implement the buildFeedEntries method and
+ optionally override the buildFeedMetadata method
+ (the default implementation is empty), as shown below
+
+ public class SampleContentAtomView extends AbstractAtomFeedView {
+
+ @Override
+ protected void buildFeedMetadata(Map<String, Object> model, Feed feed,
+ HttpServletRequest request) {
+ // implementation omitted
+ }
+
+ @Override
+ protected List<Entry> buildFeedEntries(Map<String, Object> model,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception {
+
+ // implementation omitted
+ }
+}
+
+ Similar requirements apply for implementing
+ AbstractRssFeedView, as shown below
+
+ public class SampleContentAtomView extends AbstractRssFeedView {
+
+ @Override
+ protected void buildFeedMetadata(Map<String, Object> model, Channel feed,
+ HttpServletRequest request) {
+ // implementation omitted
+ }
+
+ @Override
+ protected List<Item> buildFeedItems(Map<String, Object> model,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception {
+ // implementation omitted
+ }
+
+}
+
+ The buildFeedItems and
+ buildFeedEntires pass in the HTTP request in case
+ you need to access the Locale. The HTTP response is passed in only for the
+ setting of cookies or other HTTP headers. The feed will automatically be
+ written to the response object after the method returns.
+
+ For an example of creating a Atom view please refer to Alef
+ Arendsen's SpringSource TeamBlog entry.
+
+
+
+ XML Marshalling View
+
+ The MarhsallingView uses a XML
+ Marshaller defined in the
+ org.springframework.oxm package to render the
+ response content as XML. The object to be marshalled can be set explicitly
+ using MarhsallingView's
+ modelKey bean property. Alternatively, the view will
+ iterate over all model properties marhsall only those types that are
+ supported by the Marshaller. For more
+ information on the functionality in the
+ org.springframework.oxm package refer to the
+ chapter Marshalling XML using O/X
+ Mappers.
+
+