Example usage for org.springframework.web.servlet.mvc Controller getClass

List of usage examples for org.springframework.web.servlet.mvc Controller getClass

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc Controller getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:no.dusken.aranea.web.spring.ChainedController.java

/**
 * Calls the handleRequest controller for each of the Controllers in the
 * chain sequentially, merging the ModelAndView objects returned after each
 * call and returning the merged ModelAndView object. An exception thrown by
 * any of the controllers in the chain will propagate upwards through the
 * handleRequest() method of the ChainedController. The ChainedController
 * itself does not support any communication between the controllers in the
 * chain, but this can be effected by the controllers posting to a common
 * accessible object such as the ApplicationContext. Note that this will
 * introduce coupling between the controllers and will be difficult to
 * arrange into a parallel chain. A controller can stop processing of the
 * chain by returning a null ModelAndView object. Enhanced with adding the
 * chained views to the controller. This enables the controller to render
 * the views from the chained controllers.
 *
 * @param request  the HttpServletRequest object.
 * @param response the HttpServletResponse object.
 * @return the merged ModelAndView object for all the controllers.
 * @throws Exception if one is thrown by one of the controllers in the chain.
 *///w  w  w. j  av  a  2 s  .  c o m
public ModelAndView handleRequestSequentially(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ModelAndView mergedModel = new ModelAndView();
    List<String> mergedViews = new ArrayList<String>();
    for (Controller controller : controllers) {
        try {
            ModelAndView model = controller.handleRequest(request, response);
            if (model == null) {
                // chain will stop if a controller returns a null
                // ModelAndView object.
                break;
            }
            mergedModel.addAllObjects(model.getModel());
            mergedViews.add(model.getViewName());
        } catch (Exception e) {
            throw new Exception(
                    "Controller: " + controller.getClass().getName() + " threw exception: " + e.getMessage(),
                    e);
        }
    }
    mergedModel.addObject("views", mergedViews);
    if (StringUtils.isNotEmpty(this.viewName)) {
        mergedModel.setViewName(this.viewName);
    }
    return mergedModel;
}

From source file:org.castafiore.web.servlet.CastafioreMethodServlet.java

@Override
public void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {

    if (request.getParameter("controller") != null) {
        String controller = request.getParameter("controller");

        Controller c = (Controller) BaseSpringUtil.getBean(controller);
        c.handleRequest(request, response);
        return;/*from   w w  w  .j  a  v a  2  s. c  o  m*/
    }

    String applicationid = request.getParameter("applicationid");
    String componentid = request.getParameter("componentid");
    String methodName = request.getParameter("method");
    String param = request.getParameter("paramName") != null
            ? request.getParameter(request.getParameter("paramName"))
            : null;
    try {

        Application applicationInstance = (Application) ((HttpServletRequest) request).getSession()
                .getAttribute(applicationid);
        Container c = ComponentUtil.getContainerById(applicationInstance, componentid);
        if (c instanceof Controller) {
            ((Controller) c).handleRequest(request, response);
            return;
        }

        Object o = c.getClass().getMethod(methodName, String.class).invoke(c, param);
        if (o != null) {
            if (o instanceof InputStream) {
                ChannelUtil.TransferData((InputStream) o, response.getOutputStream());
                response.getOutputStream().flush();
            } else if (o instanceof JSObject) {
                response.getOutputStream().write(((JSObject) o).getJavascript().getBytes());
                response.getOutputStream().flush();
            } else {
                response.getOutputStream().write(o.toString().getBytes());
                response.getOutputStream().flush();
            }

            //response.setContentType(MimeUtility);
            ((HttpServletResponse) response).setHeader("Content-Disposition",
                    "filename=" + methodName.replace("_", "."));
        }

    } catch (Exception e) {
        throw new ServletException("unable to load method since the params passed are not correct", e);
    }

}