Java Code Examples for org.springframework.web.servlet.ModelAndView#getViewName()

The following examples show how to use org.springframework.web.servlet.ModelAndView#getViewName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageHandlerInterceptor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) {
	HttpSession session = request.getSession();

	if (session != null) {
		Message message = (Message) session.getAttribute(Message.ATTRIBUTE_KEY);
		if (message != null && !mav.getModelMap().containsKey(Message.ATTRIBUTE_KEY)) {
			mav.addObject(Message.ATTRIBUTE_KEY, message);
		}

		boolean shouldRemove = true;
		if (mav.hasView() && mav.getView() instanceof RedirectView) {
			shouldRemove = false;
		}
		if (mav.getViewName() != null && mav.getViewName().startsWith("redirect:")) {
			shouldRemove = false;
		}

		if (shouldRemove) {
			session.removeAttribute(Message.ATTRIBUTE_KEY);
		}
	}
}
 
Example 2
Source File: MvcViewRender.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public void doRender(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
	if(localeResolver!=null){
		Locale locale = this.localeResolver.resolveLocale(request);
		response.setLocale(locale);
	}
	
	View view;
	if (mv.isReference()) {
		view = resolveViewName(mv.getViewName(), mv.getModel(), response.getLocale(), request);
		if (view == null) {
			throw new BaseException("Could not resolve view with name '" + mv.getViewName() + "' in '" + getClass().getSimpleName() + "'");
		}
	}
	else {
		view = mv.getView();
		if (view == null) {
			throw new BaseException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in '" + getClass().getSimpleName() + "'");
		}
	}

	if (mv.getStatus() != null) {
		response.setStatus(mv.getStatus().value());
	}
	view.render(mv.getModel(), request, response);
}
 
Example 3
Source File: ErrorController.java    From codenjoy with GNU General Public License v3.0 6 votes vote down vote up
private String error(Exception exception, HttpServletRequest req, ModelMap model) {
    String url = req.getRequestURL().toString();

    // для "not found" запросов вытаскиваем доп инфо
    if (req instanceof SecurityContextHolderAwareRequestWrapper) {
        ServletRequest request = ((SecurityContextHolderAwareRequestWrapper) req).getRequest();
        if (request instanceof FirewalledRequest) {
            ServletRequest request2 = ((FirewalledRequest) request).getRequest();
            if (request2 instanceof Request) {
                url = String.format("%s [%s]",
                        url, ((Request)request2).getOriginalURI());
            }
        }
    }

    ModelAndView view = ticket.get(url, exception);
    model.mergeAttributes(view.getModel());

    return view.getViewName();
}
 
Example 4
Source File: ModelAndViewMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	ModelAndView mav = (ModelAndView) returnValue;
	if (mav.isReference()) {
		String viewName = mav.getViewName();
		mavContainer.setViewName(viewName);
		if (viewName != null && isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else {
		View view = mav.getView();
		mavContainer.setView(view);
		if (view instanceof SmartView && ((SmartView) view).isRedirectView()) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	mavContainer.setStatus(mav.getStatus());
	mavContainer.addAllAttributes(mav.getModel());
}
 
Example 5
Source File: ComputerOrMobileAdapterAspect.java    From springBoot with MIT License 5 votes vote down vote up
/**
 * 环绕通知
 */
@Around(value = "pointcut()")
public Object around(ProceedingJoinPoint pjp) {
    //request对象
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

    try {
        //获取返回值
        Object o = pjp.proceed();

        //拦截返回值为ModelAndView即可
        if("org.springframework.web.servlet.ModelAndView".equals(o.getClass().getName())){
             /*
            PC端ua:windows nt、macintosh
            移动端ua:iphone、ipod、android
         */
            String adapter;
            String ua = request.getHeader("user-agent").toLowerCase();
            if (ua.contains("iphone") || ua.contains("ipod") || ua.contains("android")) {
                adapter = "mobile";
            } else {
                adapter = "computer";
            }
            log.info("PC端、手机端页面适配器:" + adapter);

            //强势修改
            ModelAndView modelAndView = (ModelAndView) o;
            String viewName = modelAndView.getViewName();
            modelAndView.setViewName(adapter + "/" + viewName);
            o = modelAndView;
        }

        return o;
    } catch (Throwable throwable) {
        //返回统一错误页面
        log.error(throwable.getMessage());
        return new ModelAndView("common/error/500");
    }
}
 
Example 6
Source File: ModelAndViewMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	ModelAndView mav = (ModelAndView) returnValue;
	if (mav.isReference()) {
		String viewName = mav.getViewName();
		mavContainer.setViewName(viewName);
		if (viewName != null && isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else {
		View view = mav.getView();
		mavContainer.setView(view);
		if (view instanceof SmartView && ((SmartView) view).isRedirectView()) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	mavContainer.setStatus(mav.getStatus());
	mavContainer.addAllAttributes(mav.getModel());
}
 
Example 7
Source File: ModelAndViewMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	ModelAndView mav = (ModelAndView) returnValue;
	if (mav.isReference()) {
		String viewName = mav.getViewName();
		mavContainer.setViewName(viewName);
		if (viewName != null && isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else {
		View view = mav.getView();
		mavContainer.setView(view);
		if (view instanceof SmartView) {
			if (((SmartView) view).isRedirectView()) {
				mavContainer.setRedirectModelScenario(true);
			}
		}
	}
	mavContainer.setStatus(mav.getStatus());
	mavContainer.addAllAttributes(mav.getModel());
}
 
Example 8
Source File: AuthorityInterceptor.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    if(modelAndView!=null && modelAndView.getViewName()!=null) {
        if (modelAndView.getModel().get("fileAccess") == null) {
            if(!modelAndView.getViewName().startsWith("redirect:")) {
                modelAndView.getModel().put("fileAccess", ConfigUtils.getFileAccessURL());
            }
        }
    }
}
 
Example 9
Source File: ThymeleafLayoutInterceptor.java    From Spring-MVC-Blueprints with MIT License 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    if (modelAndView == null || !modelAndView.hasView()) {
        return;
    }
    String originalViewName = modelAndView.getViewName();
    if (isRedirectOrForward(originalViewName)) {
        return;
    }
    modelAndView.setViewName(DEFAULT_LAYOUT);
    modelAndView.addObject(DEFAULT_VIEW_ATTRIBUTE_NAME, originalViewName);
}
 
Example 10
Source File: ModelAndViewMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	ModelAndView mav = (ModelAndView) returnValue;
	if (mav.isReference()) {
		String viewName = mav.getViewName();
		mavContainer.setViewName(viewName);
		if (viewName != null && isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else {
		View view = mav.getView();
		mavContainer.setView(view);
		if (view instanceof SmartView) {
			if (((SmartView) view).isRedirectView()) {
				mavContainer.setRedirectModelScenario(true);
			}
		}
	}
	mavContainer.addAllAttributes(mav.getModel());
}
 
Example 11
Source File: ThymeleafLayoutInterceptor.java    From spring-boot-spring-loaded-java8-example with Apache License 2.0 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    if (modelAndView == null || !modelAndView.hasView()) {
        return;
    }

    String originalViewName = modelAndView.getViewName();
    if (isRedirectOrForward(originalViewName)) {
        return;
    }
    String layoutName = getLayoutName(handler);
    modelAndView.setViewName(layoutName);
    modelAndView.addObject(this.viewAttributeName, originalViewName);
}
 
Example 12
Source File: AdminContextInterceptor.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void postHandle(HttpServletRequest request,
		HttpServletResponse response, Object handler, ModelAndView mav)
		throws Exception {
	CmsUser user = CmsUtils.getUser(request);
	// 不控制权限时perm为null,PermistionDirective标签将以此作为依据不处理权限问题。
	if (auth && user != null && !user.isSuper() && mav != null
			&& mav.getModelMap() != null && mav.getViewName() != null
			&& !mav.getViewName().startsWith("redirect:")) {
		mav.getModelMap().addAttribute(PERMISSION_MODEL, user.getPerms());
	}
}
 
Example 13
Source File: MolgenisInterceptor.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void postHandle(
    HttpServletRequest request,
    HttpServletResponse response,
    Object handler,
    ModelAndView modelAndView) {
  if (modelAndView != null) {
    String viewName = modelAndView.getViewName();
    if (viewName == null || !viewName.startsWith("forward:")) {
      // use programmatic transaction instead of transaction annotation since we only want to
      // start a read-only transaction when a modelAndView exists
      TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
      transactionTemplate.setReadOnly(true);
      transactionTemplate.execute(
          status -> {
            modelAndView.addObject(
                KEY_RESOURCE_FINGERPRINT_REGISTRY, resourceFingerprintRegistry);
            modelAndView.addObject(KEY_THEME_FINGERPRINT_REGISTRY, themeFingerprintRegistry);
            modelAndView.addObject(KEY_APP_SETTINGS, createAppSettings());
            modelAndView.addObject(
                KEY_AUTHENTICATION_OIDC_CLIENTS, runAsSystem(this::getOidcClients));
            modelAndView.addObject(
                KEY_AUTHENTICATION_SIGN_UP, authenticationSettings.getSignUp());
            modelAndView.addObject(KEY_ENVIRONMENT, getEnvironmentAttributes());
            modelAndView.addObject(
                KEY_I18N,
                new MessageSourceResourceBundle(messageSource, LocaleContextHolder.getLocale()));
            modelAndView.addObject(KEY_GSON, gson);
            modelAndView.addObject(KEY_LANGUAGE, LocaleContextHolder.getLocale().getLanguage());
            modelAndView.addObject(KEY_FALLBACK_LANGUAGE, appSettings.getLanguageCode());
            modelAndView.addObject(
                KEY_SUPER_USER, userAccountService.getCurrentUser().isSuperuser());
            return null;
          });
    }
  }
}
 
Example 14
Source File: UserInterceptor.java    From tutorials with MIT License 5 votes vote down vote up
public static boolean isRedirectView(ModelAndView mv) {

        String viewName = mv.getViewName();
        if (viewName.startsWith("redirect:/")) {
            return true;
        }

        View view = mv.getView();
        return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView());
    }
 
Example 15
Source File: PluginInterceptor.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Transactional(readOnly = true)
@Override
public void postHandle(
    HttpServletRequest request,
    HttpServletResponse response,
    Object handler,
    ModelAndView modelAndView) {
  if (modelAndView != null) {
    String viewName = modelAndView.getViewName();
    if (viewName == null || !viewName.startsWith("forward:")) {
      PluginController molgenisPlugin = validateHandler(handler);
      String pluginId = molgenisPlugin.getId();

      // allow controllers that handle multiple plugins to set their plugin id
      if (!modelAndView.getModel().containsKey(PluginAttributes.KEY_PLUGIN_ID)) {
        modelAndView.addObject(PluginAttributes.KEY_PLUGIN_ID, pluginId);
      }

      Entity pluginSettings = molgenisPlugin.getPluginSettings();
      boolean pluginSettingsCanWrite;
      if (pluginSettings != null) {
        String pluginSettingsEntityName = pluginSettings.getEntityType().getId();
        pluginSettingsCanWrite =
            permissionService.hasPermission(
                new EntityTypeIdentity(pluginSettingsEntityName),
                EntityTypePermission.UPDATE_DATA);
        modelAndView.addObject(PluginAttributes.KEY_PLUGIN_SETTINGS, pluginSettings);
      } else {
        pluginSettingsCanWrite = false;
      }

      modelAndView.addObject(
          PluginAttributes.KEY_PLUGIN_SHOW_SETTINGS_COG, pluginSettingsCanWrite);
      modelAndView.addObject(PluginAttributes.KEY_MENU, menuReaderService.getMenu().orElse(null));

      modelAndView.addObject(
          PluginAttributes.KEY_AUTHENTICATED, SecurityUtils.currentUserIsAuthenticated());
      modelAndView.addObject(
          PluginAttributes.KEY_PLUGIN_ID_WITH_QUERY_STRING,
          getPluginIdWithQueryString(request, pluginId));
    }
  }
}
 
Example 16
Source File: WebMvcUtils.java    From spring-webmvc-support with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Is page render request
 *
 * @param modelAndView {@link ModelAndView}
 * @return If current request is for page render , return <code>true</code> , or <code>false</code>.
 */
public static boolean isPageRenderRequest(ModelAndView modelAndView) {

    if (modelAndView != null) {

        String viewName = modelAndView.getViewName();

        return StringUtils.hasText(viewName);

    }

    return false;

}