Example usage for org.springframework.web.servlet View render

List of usage examples for org.springframework.web.servlet View render

Introduction

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

Prototype

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

Source Link

Document

Render the view given the specified model.

Usage

From source file:org.springframework.web.servlet.LogDispatcherServlet.java

/**
 * Render the given ModelAndView.//  www  . j a  va2  s  .co m
 * <p>This is the last stage in handling a request. It may involve resolving the view by name.
 * @param mv the ModelAndView to render
 * @param request current HTTP servlet request
 * @param response current HTTP servlet response
 * @throws ServletException if view is missing or cannot be resolved
 * @throws Exception if there's a problem rendering the view
 */
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    // Determine locale for request and apply it to the response.
    Locale locale = this.localeResolver.resolveLocale(request);
    response.setLocale(locale);

    View view;
    if (mv.isReference()) {
        // We need to resolve the view name.
        view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
        if (view == null) {
            throw new ServletException("Could not resolve view with name '" + mv.getViewName()
                    + "' in servlet with name '" + getServletName() + "'");
        }
    } else {
        // No need to lookup: the ModelAndView object contains the actual View object.
        view = mv.getView();
        if (view == null) {
            throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a "
                    + "View object in servlet with name '" + getServletName() + "'");
        }
    }

    // Delegate to the View object for rendering.
    if (logger.isDebugEnabled()) {
        logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
    }
    try {
        view.render(mv.getModelInternal(), request, response);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '"
                    + getServletName() + "'", ex);
        }
        throw ex;
    }
}

From source file:org.springframework.web.servlet.SimpleDispatcherServlet.java

/**
 * Render the given ModelAndView.// w ww  . j  av a2 s .c o m
 * <p>This is the last stage in handling a request. It may involve resolving the view by name.
 * @param mv the ModelAndView to render
 * @param request current HTTP servlet request
 * @param response current HTTP servlet response
 * @throws Exception if there's a problem rendering the view
 */
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    // Determine locale for request and apply it to the response.
    Locale locale = this.localeResolver.resolveLocale(request);
    response.setLocale(locale);

    View view;
    if (mv.isReference()) {
        // We need to resolve the view name.
        view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
        if (view == null) {
            throw new ServletException("Could not resolve view with name '" + mv.getViewName()
                    + "' in servlet with name '" + getServletName() + "'");
        }
    } else {
        // No need to lookup: the ModelAndView object contains the actual View object.
        view = mv.getView();
        if (view == null) {
            throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a "
                    + "View object in servlet with name '" + getServletName() + "'");
        }
    }

    //DEBUG
    Map<String, Object> modelInternal = mv.getModelInternal();
    for (String modelName : modelInternal.keySet()) {
        System.out.println("modelKey: " + modelName);
        System.out.println("modelValue: " + modelInternal.get(modelName));
    }

    // Delegate to the View object for rendering.
    if (logger.isDebugEnabled()) {
        logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
    }
    view.render(mv.getModelInternal(), request, response);
}

From source file:uk.ac.abdn.fits.support.thymeleaf.springmail.web.MailController.java

@RequestMapping(value = "/sendMailSimple", method = RequestMethod.POST)
public @ResponseBody RESTFulRequest sendSimpleMail(HttpSession session, HttpServletRequest request,
        //          HttpServletResponse response,
        Model model, final Locale locale) throws MessagingException {

    String recipientName = "Cheng Zeng";
    String recipientEmail = "c.zeng@abdn.ac.uk";
    String fname = (String) session.getAttribute("fname");
    String lname = (String) session.getAttribute("lname");
    String email = (String) session.getAttribute("email");
    if (fname != null && lname != null) {
        recipientName = fname + " " + lname;
        System.out.println("recipientName: " + recipientName);
    }/*from w  ww.j  av a2s  .  com*/
    if (email != null && !email.equals("")) {
        recipientEmail = email;
        System.out.println("recipientEmail: " + recipientEmail);
    }

    List<TOption> not_relaxed = (List<TOption>) session.getAttribute("options");
    List<TOption> relaxed_options = (List<TOption>) session.getAttribute("relaxed_options");
    String date_of_travel = (String) session.getAttribute("date_of_travel");
    String origin_postcode = (String) session.getAttribute("origin_postcode");

    List<TOption> not_relaxed_rtn = (List<TOption>) session.getAttribute("options_rtn");
    List<TOption> relaxed_options_rtn = (List<TOption>) session.getAttribute("relaxed_options_rtn");
    String origin_postcode_rtn = (String) session.getAttribute("origin_postcode_rtn");

    String url = null;
    View resolvedView;
    try {
        model.addAttribute("date_of_travel", date_of_travel);
        model.addAttribute("origin_postcode", origin_postcode);
        model.addAttribute("options", not_relaxed);
        model.addAttribute("relaxed_options", relaxed_options);
        model.addAttribute("caption", "Transport options ranked using preferences");
        model.addAttribute("origin_postcode_rtn", origin_postcode_rtn);
        model.addAttribute("options_rtn", not_relaxed_rtn);
        model.addAttribute("relaxed_options_rtn", relaxed_options_rtn);
        model.addAttribute("email_view", "email_view");
        resolvedView = this.viewResolver.resolveViewName("matching", Locale.UK);
        MockHttpServletResponse mockResp = new MockHttpServletResponse();
        resolvedView.render(model.asMap(), request, mockResp);
        //          System.out.println("rendered html : " + mockResp.getContentAsString());
        url = saveAsHtml(request, mockResp.getContentAsString());
        emailService.sendRichMail(recipientName, recipientEmail, locale, url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new RESTFulRequest(1, "mail sent to " + recipientEmail);
}