Example usage for org.springframework.web.servlet.view JstlView render

List of usage examples for org.springframework.web.servlet.view JstlView render

Introduction

In this page you can find the example usage for org.springframework.web.servlet.view JstlView render.

Prototype

@Override
public void render(@Nullable Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception 

Source Link

Document

Prepares the view given the specified model, merging it with static attributes and a RequestContext attribute, if necessary.

Usage

From source file:org.bibsonomy.webapp.view.CSVView.java

@Override
protected void renderMergedOutputModel(final Map<String, Object> model, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    /*//  w ww  .ja  va 2 s.c  o m
     * get the data
     */
    final Object object = model.get(BaseCommandController.DEFAULT_COMMAND_NAME);
    if (object instanceof SimpleResourceViewCommand) {
        /*
         * we can only handle SimpleResourceViewCommands ...
         */
        final SimpleResourceViewCommand command = (SimpleResourceViewCommand) object;

        /*
         * set the content type headers
         */
        response.setContentType("text/csv");
        response.setCharacterEncoding("UTF-8");

        /*
         * get the requested path
         * we need it to generate the file names for inline content-disposition
         * FIXME: The path is written into the request by the UrlRewriteFilter 
         * ... probably this is not a good idea
         */
        //         final String requPath = (String) request.getAttribute("requPath");
        //         response.setHeader("Content-Disposition", "attachement; filename=" + Functions.makeCleanFileName(requPath) + extension);

        try {
            /*
             * write the buffer to the response
             */
            final ServletOutputStream outputStream = response.getOutputStream();
            final CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            /*
             * write header
             */
            csvWriter.writeNext(new String[] { "intrahash", "user", "date", "tags", "groups", "description",

                    "title",

                    // publication-only fields
                    "bibtexKey",
                    // from here: sorted by alphabet
                    "address", "annote", "author", "entrytype", "booktitle", "chapter", "crossref", "day",
                    "edition", "editor", "howpublished", "institution", "journal", "key", "month", "note",
                    "number", "organization", "pages", "publisher", "school", "series", "type", "volume",
                    "year",

                    // remaining "special" fields
                    "private note", "misc", "abstract"

            });

            /*
             * write publications
             */
            final List<Post<BibTex>> publicationList = command.getBibtex().getList();
            if (publicationList != null) {
                for (final Post<BibTex> post : publicationList) {
                    final BibTex resource = post.getResource();
                    csvWriter.writeNext(getArray(post, resource.getAuthor(), resource.getEditor(),
                            resource.getBibtexKey(), resource.getAnnote(), resource.getBooktitle(),
                            resource.getCrossref(), resource.getAddress(), resource.getEntrytype(),
                            resource.getChapter(), resource.getEdition(), resource.getDay(),
                            resource.getHowpublished(), resource.getInstitution(), resource.getJournal(),
                            resource.getMonth(), resource.getKey(), resource.getNumber(),
                            resource.getOrganization(), resource.getNote(), resource.getPages(),
                            resource.getPublisher(), resource.getSchool(), resource.getSeries(),
                            resource.getType(), resource.getVolume(), resource.getYear(),
                            resource.getPrivnote(), resource.getMisc(), resource.getAbstract()));
                }
            }

            /*
             * write bookmarks
             */
            final List<Post<Bookmark>> bookmarkList = command.getBookmark().getList();
            if (bookmarkList != null) {
                for (final Post<Bookmark> post : bookmarkList) {
                    csvWriter.writeNext(getArray(post, null, null, null, null, null, null, null, null, null,
                            null, null, null, null, null, null, null, null, null, null, null, null, null, null,
                            null, null, null, null, null, null));
                }
            }

            csvWriter.close();

        } catch (final IOException e) {
            log.error("Could not render CSV view.", e);
            /*
             * layout could not be found or contains errors -> set HTTP status to 400
             */
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            /*
             * get the errors object and add the error message
             */
            final BindingResult errors = ViewUtils.getBindingResult(model);
            errors.reject("error.layout.rendering", new Object[] { e.getMessage() },
                    "Could not render layout: " + e.getMessage());
            /*
             * do the rendering ... a bit tricky: we need to get an appropriate JSTL view and give it the 
             * application context
             */
            final JstlView view = new JstlView("/WEB-INF/jsp/error.jspx");
            view.setApplicationContext(getApplicationContext());
            view.render(model, request, response);
        }
    } else {
        /*
         * FIXME: what todo here?
         */
    }
}

From source file:org.bibsonomy.webapp.view.LayoutView.java

@Override
protected void renderMergedOutputModel(final Map<String, Object> model, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    /*/*from   www.  j  a va  2  s  .  c o m*/
     * get the data
     */
    final Object object = model.get(BaseCommandController.DEFAULT_COMMAND_NAME);
    if (object instanceof SimpleResourceViewCommand) {
        /*
         * we can only handle SimpleResourceViewCommands ...
         */
        final SimpleResourceViewCommand command = (SimpleResourceViewCommand) object;
        final String loginUserName = command.getContext().getLoginUser().getName();

        /*
         * get requested layout
         */
        final String layout = command.getLayout();
        final boolean formatEmbedded = command.getformatEmbedded();
        /*
         * get the requested path
         * we need it to generate the file names for inline content-disposition
         * FIXME: The path is written into the request by the UrlRewriteFilter 
         * ... probably this is not a good idea
         */
        final String requPath = (String) request.getAttribute("requPath");

        log.info("rendering layout " + layout + " for user " + loginUserName + " with path " + requPath);

        /*
         * 
         */
        final List<Post<BibTex>> publicationPosts = command.getBibtex().getList();

        try {
            /*
             * our basic (and only) renderer is the jabref layout renderer, which supports 
             * only publications
             */
            if (layoutRenderer.supportsResourceType(BibTex.class)) {
                /*
                 * render publication posts
                 */
                renderResponse(layout, requPath, publicationPosts, loginUserName, response, formatEmbedded);
            } else {
                /*
                 * we could not find a suitable renderer - this should never happen!
                 */
                throw new RuntimeException("No layout for publications renderer available.");
            }
        } catch (final LayoutRenderingException e) {
            log.error("Could not render layout " + layout + ": " + e.getMessage());
            /*
             * layout could not be found or contains errors -> set HTTP status to 400
             */
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            /*
             * get the errors object and add the error message
             */
            final BindingResult errors = ViewUtils.getBindingResult(model);
            errors.reject("error.layout.rendering", new Object[] { e.getMessage() },
                    "Could not render layout: " + e.getMessage());
            /*
             * do the rendering ... a bit tricky: we need to get an appropriate JSTL view and give it the 
             * application context
             */
            final JstlView view = new JstlView("/WEB-INF/jsp/error.jspx");
            view.setApplicationContext(getApplicationContext());
            view.render(model, request, response);

        }
    } else {
        /*
         * FIXME: what todo here?
         */
    }
}