List of usage examples for org.springframework.web.servlet ModelAndView clear
public void clear()
From source file:com.mmj.app.common.interceptor.ExportFileAnnotationInterceptor.java
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (handler == null || response == null) { return;/*from ww w. java 2 s . c o m*/ } HandlerMethod hm = (HandlerMethod) handler; ExportWordFile exportWordFile = hm.getMethodAnnotation(ExportWordFile.class); // word, if (exportWordFile != null) { String wordName = exportWordFile.value(); if (StringUtils.isEmpty(wordName)) { return; } String contentDis = "attachment;filename=" + wordName + ".doc"; response.setHeader("content-disposition", contentDis); response.setContentType("application/msword;"); response.setCharacterEncoding("UTF-8"); } ExportExcelFile exportExcelFile = hm.getMethodAnnotation(ExportExcelFile.class); // excel, if (exportExcelFile != null) { String xlsName = exportExcelFile.value(); if (StringUtils.isEmpty(xlsName)) { return; } List<?> list = (List<?>) modelAndView.getModel().get("list"); String[] head = (String[]) modelAndView.getModel().get("head"); modelAndView.clear(); HSSFWorkbook workbook = ExcelUtils.defBuildExcel(list, xlsName, head); if (workbook == null) { try { response.getOutputStream().print("Not conform to the requirements of data"); } catch (IOException e) { logger.error(e.getMessage(), e); } return; } response.setHeader("content-disposition", "attachment;filename=" + xlsName + ".xls"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("UTF-8"); // response OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } }
From source file:org.openmrs.module.personalhr.web.controller.AddPersonController.java
/** * Prepares the form view/*from ww w.j a va 2 s.c o m*/ */ @Override public ModelAndView showForm(final HttpServletRequest request, final HttpServletResponse response, final BindException errors) throws Exception { this.log.debug("In showForm method"); ModelAndView mav = super.showForm(request, response, errors); // If a invalid age is submitted, give the user a useful error message. if (this.invalidAgeFormat) { mav = new ModelAndView(this.FORM_ENTRY_ERROR_URL); mav.addObject("errorTitle", "Person.age.error"); mav.addObject("errorMessage", "Person.birthdate.required"); return mav; } final Object o = mav.getModel().get(this.getCommandName()); final List personList = (List) o; this.log.debug("Found list of size: " + personList.size()); if ((personList.size() < 1) && Context.isAuthenticated()) { getParametersFromRequest(request); if (this.viewType == null) { this.viewType = "edit"; } this.log.debug("name: " + this.name + " birthdate: " + this.birthdate + " age: " + this.age + " gender: " + this.gender); if (!this.name.equals("") || !this.birthdate.equals("") || !this.age.equals("") || !this.gender.equals("")) { mav.clear(); mav.setView(new RedirectView(getPersonURL("", this.personType, this.viewType, request))); } } return mav; }
From source file:com.zb.app.common.interceptor.ExportFileAnnotationInterceptor.java
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (handler == null || response == null) { return;//from w w w . j a va 2 s .co m } HandlerMethod hm = (HandlerMethod) handler; ExportWordFile exportWordFile = hm.getMethodAnnotation(ExportWordFile.class); // word, if (exportWordFile != null) { String wordName = exportWordFile.value(); if (StringUtils.isEmpty(wordName)) { return; } wordName = new String(wordName.getBytes(), "ISO8859-1"); String contentDis = "attachment;filename=" + wordName + ".doc"; response.setHeader("content-disposition", contentDis); response.setContentType("application/msword;"); response.setCharacterEncoding("UTF-8"); } ExportExcelFile exportExcelFile = hm.getMethodAnnotation(ExportExcelFile.class); // excel, if (exportExcelFile != null) { String xlsName = exportExcelFile.value(); if (StringUtils.isEmpty(xlsName)) { return; } xlsName = new String(xlsName.getBytes(), "UTF-8"); List<?> list = (List<?>) modelAndView.getModel().get("list"); String[] head = (String[]) modelAndView.getModel().get("head"); modelAndView.clear(); HSSFWorkbook workbook = ExcelUtils.defBuildExcel(list, xlsName, head); if (workbook == null) { try { response.getOutputStream().print("Not conform to the requirements of data"); } catch (IOException e) { logger.error(e.getMessage(), e); } return; } response.setHeader("content-disposition", "attachment;filename=" + xlsName + ".xls"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("UTF-8"); // response OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } }
From source file:org.openmrs.web.controller.person.AddPersonController.java
/** * Prepares the form view// w w w . j av a 2 s .c om */ public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception { log.debug("In showForm method"); ModelAndView mav = super.showForm(request, response, errors); // If a invalid age is submitted, give the user a useful error message. if (invalidAgeFormat) { mav = new ModelAndView(FORM_ENTRY_ERROR_URL); mav.addObject("errorTitle", "Person.age.error"); mav.addObject("errorMessage", "Person.birthdate.required"); return mav; } Object o = mav.getModel().get(this.getCommandName()); List personList = (List) o; log.debug("Found list of size: " + personList.size()); if (personList.size() < 1 && Context.isAuthenticated()) { Map<String, String> person = getParametersFromRequest(request); String name = person.get(NAME); String birthdate = person.get(BIRTH_DATE); String age = person.get(AGE); String gender = person.get(GENDER); String viewType = person.get(VIEW_TYPE); String personType = person.get(PERSON_TYPE); if (viewType == null) { viewType = "edit"; } log.debug("name: " + name + " birthdate: " + birthdate + " age: " + age + " gender: " + gender); if (StringUtils.isNotEmpty(name) || StringUtils.isNotEmpty(birthdate) || StringUtils.isNotEmpty(age) || StringUtils.isNotEmpty(gender)) { mav.clear(); mav.setView(new RedirectView(getPersonURL("", personType, viewType, request))); } } return mav; }
From source file:org.cloudifysource.rest.interceptors.ApiVersionValidationAndRestResponseBuilderInterceptor.java
@Override public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception { Object model = filterModel(modelAndView, handler); modelAndView.clear(); response.setContentType(MediaType.APPLICATION_JSON); if (model instanceof Response<?>) { String responseBodyStr = new ObjectMapper().writeValueAsString(model); response.getOutputStream().write(responseBodyStr.getBytes()); response.getOutputStream().close(); } else {//from ww w . j a v a 2s. c o m Response<Object> responseBodyObj = new Response<Object>(); responseBodyObj.setResponse(model); responseBodyObj.setStatus("Success"); responseBodyObj.setMessage(messageSource.getMessage(CloudifyMessageKeys.OPERATION_SUCCESSFULL.getName(), new Object[] {}, Locale.US)); responseBodyObj.setMessageId(CloudifyMessageKeys.OPERATION_SUCCESSFULL.getName()); String responseBodyStr = new ObjectMapper().writeValueAsString(responseBodyObj); response.getOutputStream().write(responseBodyStr.getBytes()); response.getOutputStream().close(); } }
From source file:org.jamwiki.servlets.ServletUtil.java
/** * Modify the current ModelAndView object to create a Spring redirect * response, meaning that the view name becomes "redirect:" followed by the * redirection target.//from ww w . ja v a2 s. c om * * @param next * The current ModelAndView object, which will be reset by this * method. * @param virtualWiki * The virtual wiki name for the page being redirected to. * @param destination * The topic or page name that is the redirection target. An example * might be "Special:Login". * @throws WikiException * Thrown if any error occurs while processing. */ protected static void redirect(ModelAndView next, String virtualWiki, String destination) throws WikiException { String target = null; try { target = LinkUtil.buildTopicUrl(null, virtualWiki, destination, true); } catch (DataAccessException e) { throw new WikiException(new WikiMessage("error.unknown", e.getMessage()), e); } String view = ServletUtil.SPRING_REDIRECT_PREFIX + target; next.clear(); next.setViewName(view); }
From source file:org.springmodules.xt.ajax.AjaxInterceptor.java
/** * Post-handle the http request and if it was an ajax request firing a submit, looks for a mapped ajax handler, executes it and * returns an ajax response.<br>/*from w ww.ja v a 2 s . c o m*/ * If the matching mapped handler returns a null or empty ajax response, the interceptor looks for a view configured with * the {@link #AJAX_REDIRECT_PREFIX} and make a redirect to it; if the configured view has no * {@link #AJAX_REDIRECT_PREFIX}, an exception is thrown. * * @throws UnsupportedEventException If the event associated with this ajax request is not supported by any * mapped handler. * @throws EventHandlingException If an error occurred during event handling. * @throws NoMatchingHandlerException If no mapped handler matching the URL can be found. * @throws IllegalViewException If the view to which redirect to doesn't contain the {@link #AJAX_REDIRECT_PREFIX}. */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (WebUtils.isIncludeRequest(request)) { return; } try { // If modelAndView object is null, it means that the controller handled the request by itself ... // See : http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/Controller.html#handleRequest(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse) if (modelAndView == null) { logger.info("Null ModelAndView object, proceeding without Ajax processing ..."); return; } // // Store the model map: this.storeModel(request.getSession(), modelAndView.getModel()); // // Continue processing: // String requestType = request.getParameter(this.ajaxParameter); if (requestType != null && requestType.equals(AJAX_SUBMIT_REQUEST)) { String eventId = request.getParameter(this.eventParameter); if (eventId == null) { throw new IllegalStateException("Event id cannot be null."); } logger.info(new StringBuilder("Post-handling ajax request for event: ").append(eventId)); List<AjaxHandler> handlers = this.lookupHandlers(request); if (handlers.isEmpty()) { throw new NoMatchingHandlerException("Cannot find an handler matching the request: " + this.urlPathHelper.getLookupPathForRequest(request)); } else { AjaxSubmitEvent event = new AjaxSubmitEventImpl(eventId, request); AjaxResponse ajaxResponse = null; boolean supported = false; for (AjaxHandler ajaxHandler : handlers) { if (ajaxHandler.supports(event)) { // Set event properties: this.initEvent(event, request); Map model = this.getModel(request.getSession()); if (model != null) { Object commandObject = this.formDataAccessor.getCommandObject(request, response, handler, model); Errors errors = this.formDataAccessor.getValidationErrors(request, response, handler, model); event.setCommandObject(commandObject); event.setValidationErrors(errors); event.setModel(model); } // Handle event: ajaxResponse = ajaxHandler.handle(event); supported = true; break; } } if (!supported) { throw new UnsupportedEventException("Cannot handling the given event with id: " + eventId); } else { if (ajaxResponse != null && !ajaxResponse.isEmpty()) { // Need to clear the ModelAndView because we are handling the response by ourselves: modelAndView.clear(); AjaxResponseSender.sendResponse(response, ajaxResponse); } else { // No response, so try an Ajax redirect: String view = modelAndView.getViewName(); if (view != null && view.startsWith(AJAX_REDIRECT_PREFIX)) { view = view.substring(AJAX_REDIRECT_PREFIX.length()); this.redirectToView(view, request, response, modelAndView); } else if (view != null && view.startsWith(STANDARD_REDIRECT_PREFIX)) { view = view.substring(STANDARD_REDIRECT_PREFIX.length()); this.redirectToView(view, request, response, modelAndView); } else { throw new IllegalViewException("No Ajax redirect prefix: " + AJAX_REDIRECT_PREFIX + " found for view: " + view); } } } } } } catch (Exception ex) { logger.error(ex.getMessage(), ex); throw ex; } }
From source file:org.springmodules.xt.ajax.AjaxInterceptor.java
private void redirectToView(String view, HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) throws IOException { // Creating Ajax redirect action: AjaxResponse ajaxResponse = new AjaxResponseImpl(); AjaxAction ajaxAction = new RedirectAction( new StringBuilder(request.getContextPath()).append(view).toString(), modelAndView); ajaxResponse.addAction(ajaxAction);// w ww .j a v a 2 s. c o m // Need to clear the ModelAndView because we are handling the response by ourselves: modelAndView.clear(); AjaxResponseSender.sendResponse(response, ajaxResponse); }