Custom views based on the Beannameviewresolver parser

Source: Internet
Author: User

Overview

Based on SPRING-MVC custom views, beannameviewresolver is used as a parser to meet special needs.

This article is an example of a compressed file that outputs multiple PDF files for the foreground download, but does not provide a service tier implementation.

Implementing the implementation class for creating Abstractview
 PackageCn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.view;Importjava.util.List;ImportJava.util.Map;ImportJavax.annotation.Resource;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.springframework.stereotype.Component;ImportOrg.springframework.web.servlet.view.AbstractView;ImportCn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.domain.DFCL;ImportCn.sinobest.jzpt.zfba.fzyw.xzfy.dfcl.service.IZipService; @Component ("Dfclview") Public classDfclviewextendsAbstractview {@Resource (name= "Fzyw.xzfy.service.zip")    PrivateIzipservice Zipservice; @Overrideprotected voidRendermergedoutputmodel (map<string, object>model, HttpServletRequest request, httpservletresponse response)throwsException {/*1. Setting the response content type*/Response.setcontenttype ("Application/octet-stream;charset=utf-8"); /*2. Add the file name to the response header*/String Zipname= "Material Compression package" + system.currenttimemillis () + ". Zip"; Zipname=NewString (Zipname.getbytes (), "Iso-8859-1"); Response.AddHeader ("Content-disposition", "attachment; Filename= "+zipname); /*3. Output file Contents*/@SuppressWarnings ("Unchecked") List<DFCL> dfcllist = (list<dfcl>) model.get ("Dfcllist");        Zipservice.write (Response.getoutputstream (), dfcllist); /*4. Close the stream*/Response.getoutputstream (). Flush ();    Response.getoutputstream (). Close (); }}

Description

Note component The bean used to declare spring

Returns the name of the view Bean in the controller

Private Static Final String view_default = "Dfclview"; @RequestMapping ("/download") public String Download ( Model model, String writids) {    List<DFCL> dfcllist = DFCLSERVICE.GETDFCL (writids);    Model.addattribute ("Dfcllist", dfcllist);     return View_default;}
More content

Note: The following introduction is based on the configuration of the project framework, the specific implementation of the code of the class is inconvenient to post, right for reference .

In Spring-mvc, the name returned by the controller needs to be parsed by the view parser, the parser finds the corresponding view, and calls the corresponding method.

In the project, usually the controller's method returns the relative path of the JSP, that is, the corresponding content can be obtained in the foreground, because the following configuration exists in the Webmvc-config.xml:

class class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"--    <property name= "prefix" value= "/>    <property name=" suffix "value=" "/></bean>

And the detailed table configuration file, usually after the JSP returns according to the form's Formdata property asynchronous request, the corresponding Controller method returns "Xmlview"; the foreground can get the corresponding XML and render the view. This is because the following configuration exists in the Webmvc-config.xml:

class= "Org.springframework.web.servlet.view.BeanNameViewResolver"/>

The Beannameviewresolver parser attempted to use the returned name as the view Bean's name, while the Webmvc-config.xml was configured with a view of "Xmlview":

class= "Cn.sinobest.jzpt.framework.mvc.legacysystemsmac.xml.LegacySystemsXMLView" >    <property Name= "Xmltransformer" >        class= " Cn.sinobest.jzpt.framework.mvc.legacysystemsmac.xml.LegacySystemsXMLTransformer "/>    </property> </bean>

Detailed study of the details of the configuration file return, it is necessary to see the source code of Legacysystemsxmlview and Legacysystemsxmltransformer.

The Controller method typically returns "Jsonview" when it is necessary to return JSON because of the beannameviewresolver configuration in Webmvc-config.xml and the configuration of "Jsonview":

class= "Org.springframework.web.servlet.view.json.MappingJacksonJsonView" >    <property name= " Encoding ">        <value type=" org.codehaus.jackson.JsonEncoding ">UTF8</value>    </property >    <property name= "ContentType" value= "Text/html;charset=utf-8"/></bean>

Spring provides a set of view and Viewresolver inheritance systems

Refer to the Spring Custom view for more content.

Custom views based on the Beannameviewresolver parser

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.