springmvc--View and view resolver

Source: Internet
Author: User

    • When the request processing method finishes executing, a Modelandview object is returned. For those processing methods that return types such as String,view or Modemap, Spring MVC also assembles them internally into a Modelandview object that contains a view of the logical names and model objects
    • Spring MVC uses the View parser (Viewresolver) to get the final view object (view), which can be either a JSP or a view of various representations, such as Excel, Jfreechart, etc.
    • The processor is not concerned about what view objects are ultimately being taken to render the model data, and the processor focuses on the work of the production model data to fully decouple the MVC
First, the View
    • The purpose of the view is to render the model data and present the data in the model to the customer in some form.
    • In order to decouple the view model and the implementation technology, Spring defines a highly abstract view interface in the Org.springframework.web.servlet package:
    • The View object is instantiated by the view resolver. Because views are stateless, they do not have thread security issues
Common View Implementation Classes
View Implementation Class Description
Internalresourceview Encapsulates a JSP or other resource into a view, which is a view that internalresourceviewresolver resolves to
Jstlview If you need to use the Jstl internationalization tagging feature in your JSP file, you will need the view class, not the Internalresourceview view class
Xsltview Xstl-driven view
Tilesview Views based on the Tiles page layout
Tilesjstlview If the JSP composition file of the tiles template is used for jstl, you need to replace the tilesview with that view.
Abstractexcelview Excel view abstract class, developers need to inherit Abstractexcelview, get the view model to populate, implement their own document view, need to rely on POI
Abstractjexcelview And Abstractexcelview only he is dependent on JEXCELAPI
Abstractpdfstamperview PDF document view abstract class for working with PDF documents via Acroform
Abstractpdfview PDF document View Abstract class that allows you to implement your own PDF document view with this abstract class, relying on Itext
Freemarkview View using the Freemark template engine
Velocitylayoutview View using the Velocity template engine
Velocityview View using the Velocity template engine
Configurablejasperreportsview View using the Java JasperReports Reporting technology
Jasperreportscsvview View using the Java JasperReports Reporting technology
Jasperreportshtmlview View using the Java JasperReports Reporting technology
Jasperreportsmultiformatview View using the Java JasperReports Reporting technology
Jasperreportspdgview View using the Java JasperReports Reporting technology
Jasperreportsxlsview View using the Java JasperReports Reporting technology
Marshallingview Output model data in XML with OXM and Marshaller techniques
Mappingjackson2jsonview Objectmapper of model data through the Jackson Development Framework has been output in JSON mode
Mappingjackson2xmlview Objectmapper XML output of model data through the Jackson Development Framework
Redirectview Redirected views via redirect: And ForWord: prefixes
Second, view parser
    • SPRINGMVC provides different strategies for parsing logical view names, configuring one or more parsing strategies in the Spring WEB context, and specifying their sequencing . Each mapping strategy corresponds to a specific view resolver implementation class.
    • The view parser has a single function: parsing a logical view into a specific view object.
    • All view parsers must implement the Viewresolver interface
Common View resolvers
View Parser Implementation class Description
Beannameviewresolver Commonly used to resolve the view name to a bean with the view name being the Bean's ID
Xmlviewresolver Like Beannameviewresolver, it differs from beannameviewresolver in that the bean definition is placed in an XML file rather than in a Dispatchservert configuration file
Resourcebundleviewresolver You can use this class to provide different parsing results for different types of localization
Internalresourceviewresolver Typically, this class configures prefixes and suffixes, and then resolves to a URL file, such as a JSP page, with the lowest resolution priority
Xsltviewresolver Resolves the view name to a URL file for a specified XSLT style sheet
Jasperreportsviewresolver JasperReports is a Java-based, open source reporting tool that resolves the view name to the path corresponding to the report file
Freemarkerviewresolver Parsing template files based on freemarker template technology
Velocityviewresovler, Velocitylayoutviewresovler Parse as a template file based on velocity template technology
Contentnegotiatingviewresovler Commonly used, the content negotiation view resolver, which is not responsible for specific view resolution, and according to the requested media type, select an appropriate parser from the registered view parser to parse the view, the highest resolution priority

Programmers can choose a view parser or mix multiple view parsers

    • Each view resolver implements the Ordered interface and opens an order property that specifies the precedence of the parser through the Order property, and the smaller the order, the higher the priority .
    • SPRINGMVC will parse the logical view name in the order of precedence of the view parser until the parse succeeds and the View object is returned, otherwise the servletexception exception will be thrown
Internalresourceviewresolver

JSP is the most common view technique, and you can use Internalresourceviewresolver as the View parser:

<!--Configure the View resolver: How to resolve the handler method return value to the actual physical view -    <!--the priority of the view is the maximum value of integer -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/pages/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean>
Excel View

If you want to use Excel to present a list of data, you only need to expand the Abstractexcelview or Abstractjexcel View provided by SPRINGMVC. Implement the Buildexceldocument () method to build an Excel document using the model data object in the method.

    • Abstractexcelview is based on the POI API, while Abstractjexcelview is based on Jexcelapi.
    • The view object needs to be configured with a Bean in the IOC container, and using Beannameviewresolver as the View parser
    • If you want to directly download Excel documents directly in your browser, you can set the response header Content-disposition value to Attachment;filename=xxx.xls
<!--Configure View Beannameviewresolver parser: Use the name of the view to parse the view -    <!--the priority of the view resolver is defined by the Order property, and the smaller the order value the higher the priority -    <Beanclass= "Org.springframework.web.servlet.view.BeanNameViewResolver">        < Propertyname= "Order"value= "+"></ Property>    </Bean>
Third, redirect and forward

①, in general, the Controller method returns the value of the string type as a logical view name processing

②, if the returned string is prefixed with forward: or redirect:, the SPRINGMVC will treat them specially:

Forward: and redirect: As an indicator, followed by a string as a URL to handle

    • redirect:success.jsp: A redirect operation to Success.jsp is completed
    • FORWARD:SUCCESS.JSP: A forwarding operation to success.jsp is completed
Org.springframework.web.servlet.view.UrlBasedViewResolver.class
protectedView CreateView (String viewName, locale locale)throwsException {if(! This. Canhandle (viewName, Locale)) { return NULL; } Else{String forwardurl; if(Viewname.startswith ("redirect:") ) {Forwardurl= viewname.substring ("redirect:". Length ()); Redirectview View=NewRedirectview (Forwardurl,
          This. Isredirectcontextrelative (),
This. isredirecthttp10compatible ()); return This. Applylifecyclemethods (ViewName, view); } Else if(Viewname.startswith ("Forward:") ) {Forwardurl= Viewname.substring ("Forward:". Length ()); return NewInternalresourceview (Forwardurl); } Else { return Super. CreateView (viewName, locale); } } }

springmvc--View and view resolver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.