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

The following examples show how to use org.springframework.web.servlet.ModelAndView#addAllObjects() . These examples are extracted from open source projects. 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
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) {
    if (isRedirection(mav)) {
        WorkflowParameters forwardParams = WorkflowParametersHelper.get(mav.getModelMap());

        if (forwardParams == null) {
            forwardParams = WorkflowParametersHelper.find(request);
        }

        WorkflowParametersHelper.put(RequestContextUtils.getOutputFlashMap(request), forwardParams);
        
        if (forwardParams != null && !forwardParams.isEmpty()) {
            // add forwarded parameters to url
            mav.addAllObjects(forwardParams.toMap());
        }
    }
}
 
Example 2
/**
 * 加载编辑用户信息的页面.
 * @param userId - 用户的唯一标识符
 * @param request - HttpServletRequest对象
 * @param response - HttpServletResponse对象
 * @return 包含编辑用户信息的ModelAndView对象
 */
@RequestMapping(value="/edit-user/{userId}", method=RequestMethod.GET)
public ModelAndView editUserView(
		@PathVariable(value = "userId") long userId,
		HttpServletRequest request, HttpServletResponse response) {
	User user = userService.getUserUsingUid(userId);
	Map<String, Object> userMeta = userService.getUserMetaUsingUid(user);
	if ( user == null ) {
		throw new ResourceNotFoundException();
	}
	
	List<UserGroup> userGroups = userService.getUserGroups();
	List<Language> languages = languageService.getAllLanguages();
	ModelAndView view = new ModelAndView("administration/edit-user");
	view.addObject("user", user);
	view.addAllObjects(userMeta);
	view.addObject("userGroups", userGroups);
	view.addObject("languages", languages);
	return view;
}
 
Example 3
/**
 * Generate the success view. The result will contain the assertion and the proxy iou.
 *
 * @param assertion the assertion
 * @param proxyIou the proxy iou
 * @param service the validated service
 * @param proxyGrantingTicket the proxy granting ticket
 * @return the model and view, pointed to the view name set by
 */
private ModelAndView generateSuccessView(final Assertion assertion, final String proxyIou,
                                         final WebApplicationService service,
                                         final TicketGrantingTicket proxyGrantingTicket) {

    final ModelAndView success = new ModelAndView(this.successView);
    success.addObject(CasViewConstants.MODEL_ATTRIBUTE_NAME_ASSERTION, assertion);
    success.addObject(CasViewConstants.MODEL_ATTRIBUTE_NAME_SERVICE, service);
    success.addObject(CasViewConstants.MODEL_ATTRIBUTE_NAME_PROXY_GRANTING_TICKET_IOU, proxyIou);
    if (proxyGrantingTicket != null) {
        success.addObject(CasViewConstants.MODEL_ATTRIBUTE_NAME_PROXY_GRANTING_TICKET, proxyGrantingTicket.getId());
    }
    final Map<String, ?> augmentedModelObjects = augmentSuccessViewModelObjects(assertion);
    if (augmentedModelObjects != null) {
        success.addAllObjects(augmentedModelObjects);
    }
    return success;
}
 
Example 4
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if(!(returnValue instanceof MultiView)){
        processor.handleReturnValue(new Result<>(true,returnValue),returnType,mavContainer,webRequest);
        return;
    }
    MultiView view = (MultiView) returnValue;
    //如果是ajax请求,则返回json
    if("XMLHttpRequest".equals(webRequest.getHeader("X-Requested-With"))){
        processor.handleReturnValue(new Result<>(true,view.getModelMap()),returnType,mavContainer,webRequest);
        return;
    }

    ModelAndView temp = new ModelAndView(view.getViewName());
    if(view.getView() != null) {
        temp.setView(view.getView());
    }
    temp.setStatus(view.getStatus());
    temp.addAllObjects(view.getModelMap());
    super.handleReturnValue(temp, returnType, mavContainer, webRequest);
}
 
Example 5
/**
 * 加载用户的个人信息.
 * @param userId - 用户的唯一标识符
 * @param request - HttpServletRequest对象
 * @param response - HttpResponse对象
 * @return 包含用户个人信息的ModelAndView对象
 */
@RequestMapping(value="/user/{userId}", method=RequestMethod.GET)
public ModelAndView userView(
		@PathVariable("userId") long userId,
		HttpServletRequest request, HttpServletResponse response) {
	User user = userService.getUserUsingUid(userId);
	if ( user == null || "judgers".equals(user.getUserGroup().getUserGroupSlug()) ) {
		throw new ResourceNotFoundException();
	}
	
	ModelAndView view = new ModelAndView("accounts/user");
	view.addObject("user", user);
	view.addAllObjects(userService.getUserMetaUsingUid(user));
	
	view.addObject("submissions", submissionService.getSubmissionOfUser(userId));
	view.addObject("submissionStats", submissionService.getSubmissionStatsOfUser(userId));
	return view;
}
 
Example 6
Source Project: erp-framework   File: CalendarController.java    License: MIT License 5 votes vote down vote up
@GetMapping("/index")
public ModelAndView index(ModelAndView modelAndView) {
    List<ErpRoom> list = roomService.selectAll();
    Map<String, List<ErpRoom>> map = new HashMap<>();
    map.put("roomList", list);
    modelAndView.addAllObjects(map);
    return modelAndView;
}
 
Example 7
/**
 * Return a ModelAndView object with the specified view name.
 * <p>The content of the {@link RequestContextUtils#getInputFlashMap
 * "input" FlashMap} is also added to the model.
 * @see #getViewName()
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	String viewName = getViewName();

	if (getStatusCode() != null) {
		if (getStatusCode().is3xxRedirection()) {
			request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, getStatusCode());
		}
		else {
			response.setStatus(getStatusCode().value());
			if (getStatusCode().equals(HttpStatus.NO_CONTENT) && viewName == null) {
				return null;
			}
		}
	}

	if (isStatusOnly()) {
		return null;
	}

	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addAllObjects(RequestContextUtils.getInputFlashMap(request));
	if (viewName != null) {
		modelAndView.setViewName(viewName);
	}
	else {
		modelAndView.setView(getView());
	}
	return modelAndView;
}
 
Example 8
Source Project: jakduk-api   File: ViewExceptionHandler.java    License: MIT License 5 votes vote down vote up
@ExceptionHandler({ ServiceException.class })
public ModelAndView handleServiceException(HttpServletRequest request, ServiceException exception) {

    ServiceError serviceError = exception.getServiceError();
    HttpStatus httpStatus = HttpStatus.valueOf(serviceError.getHttpStatus());

    RestErrorResponse restErrorResponse = new RestErrorResponse(serviceError, exception.getLocalizedMessage());

    String logMessage;

    try {
        logMessage = ObjectMapperUtils.writeValueAsString(restErrorResponse);
    } catch (JsonProcessingException e) {
        logMessage = "code : " + exception.getServiceError().getCode() + ", message : " + exception.getLocalizedMessage();
    }

    ModelAndView modelAndView = new ModelAndView();

    if (httpStatus.is4xxClientError()) {
        modelAndView.setViewName("error/4xx");
        log.warn(logMessage, exception);

    } else if (httpStatus.is5xxServerError()) {
        modelAndView.setViewName("error/" + httpStatus.toString());
        log.error(logMessage, exception);
    }

    Map<String, Object> map = getErrorAttributes(request, false);
    modelAndView.addAllObjects(map);
    modelAndView.setStatus(httpStatus);
    modelAndView.addObject("status", httpStatus.value());

    return modelAndView;
}
 
Example 9
@GetMapping("/block")
public ModelAndView block(HttpServletRequest request, long height) {
	ModelAndView model = new ModelAndView("explorer/block");
	model.addAllObjects(properties.toMap());
	model.addObject("height", height);
	model.addObject("baseURL", baseUrl(request));
	return model;
}
 
Example 10
@GetMapping("/tx")
public ModelAndView tx(HttpServletRequest request, String txid) {
	ModelAndView model = new ModelAndView("explorer/tx");
	model.addAllObjects(properties.toMap());
	model.addObject("txid", txid);
	model.addObject("baseURL", baseUrl(request));
	return model;
}
 
Example 11
@PostMapping("/history")
public ModelAndView history(HttpServletRequest request, String key, String type) {
	ModelAndView model = new ModelAndView("explorer/history");
	model.addAllObjects(properties.toMap());
	model.addObject("key", key);
	model.addObject("type", type);
	model.addObject("baseURL", baseUrl(request));
	return model;
}
 
Example 12
Source Project: Aooms   File: AoomsAbstractController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Thymeleaf 渲染
 * @return
 */
public void renderThymeleaf(String viewName,Map<String,Object> model){
    ModelAndView mv = new ModelAndView();
    mv.setViewName(viewName);
    if(model != null){
        mv.addAllObjects(model);
    }
    this.doRender(RenderFactory.me().getThymeleafRender(mv),null);
}
 
Example 13
/**
 * Return a ModelAndView object with the specified view name.
 * <p>The content of the {@link RequestContextUtils#getInputFlashMap
 * "input" FlashMap} is also added to the model.
 * @see #getViewName()
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	String viewName = getViewName();

	if (getStatusCode() != null) {
		if (getStatusCode().is3xxRedirection()) {
			request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, getStatusCode());
			viewName = (viewName != null && !viewName.startsWith("redirect:") ? "redirect:" + viewName : viewName);
		}
		else {
			response.setStatus(getStatusCode().value());
			if (isStatusOnly() || (getStatusCode().equals(HttpStatus.NO_CONTENT) && getViewName() == null)) {
				return null;
			}
		}
	}

	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addAllObjects(RequestContextUtils.getInputFlashMap(request));

	if (getViewName() != null) {
		modelAndView.setViewName(viewName);
	}
	else {
		modelAndView.setView(getView());
	}

	return (isStatusOnly() ? null : modelAndView);
}
 
Example 14
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    String from = null;
    if(request.getMethod().equals("GET")){
        from = request.getRequestURI();
        if(request.getQueryString()!=null){
            from +="?"+ request.getQueryString();
        }
        try {
            from = URLEncoder.encode(from,"UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
    }
    ex = getActualException(ex);
    boolean isXhr = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    if(isXhr){
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE);
        ModelAndView mv = new ModelAndView(new FastJsonJsonView());
        String errorMsg = ex.getMessage();
        if(!(ex instanceof IllegalArgumentException) && !(ex instanceof ServiceException)){
            errorMsg = "系统错误";
        }
        if (ex instanceof NotLoginException) {
            mv.addAllObjects(new _HashMap<String, Object>().add("code", Result.NOT_LOGIN).add("errorMsg", "会话已过期"));
        }else {
            mv.addAllObjects(new _HashMap<String, Object>().add("code", -1).add("errorMsg", errorMsg));
        }
        return mv;
    }

    if(ex instanceof NotLoginException){
        String redirectURL = "redirect:/login";
        if(from!=null){
            redirectURL +="?refer="+from;
        }
        return new ModelAndView(redirectURL);
    }
    return super.doResolveException(request, response, handler, ex);
}
 
Example 15
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView result = new ModelAndView();
	result.setViewName(view);

	Map<String, Object> model = new HashMap<>();
	model.put("appConfig", config);
	model.put("server", server);
	model.put("memory", new MemoryInfo());
	model.put("javaProps", getJavaPropStrings());
	model.put("envVars", getEnvVarStrings());
	result.addAllObjects(model);

	return result;
}
 
Example 16
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView result = new ModelAndView();
	result.setViewName(view);

	Map<String, Object> model = new HashMap<>();
	model.put("appConfig", config);
	model.put("server", server);
	model.put("memory", new MemoryInfo());
	result.addAllObjects(model);

	return result;
}
 
Example 17
/**
 * Return a ModelAndView object with the specified view name.
 * <p>The content of the {@link RequestContextUtils#getInputFlashMap
 * "input" FlashMap} is also added to the model.
 * @see #getViewName()
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	String viewName = getViewName();

	if (getStatusCode() != null) {
		if (getStatusCode().is3xxRedirection()) {
			request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, getStatusCode());
			viewName = (viewName != null && !viewName.startsWith("redirect:") ? "redirect:" + viewName : viewName);
		}
		else {
			response.setStatus(getStatusCode().value());
			if (isStatusOnly() || (getStatusCode().equals(HttpStatus.NO_CONTENT) && getViewName() == null)) {
				return null;
			}
		}
	}

	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addAllObjects(RequestContextUtils.getInputFlashMap(request));

	if (getViewName() != null) {
		modelAndView.setViewName(viewName);
	}
	else {
		modelAndView.setView(getView());
	}

	return (isStatusOnly() ? null : modelAndView);
}
 
Example 18
@RequestMapping(ERROR_PATH)
public ModelAndView getErrorPath(HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, false));

    Throwable cause = getCause(request);
    int status = (Integer) model.get("status");
    //错误信息
    String message = (String) model.get("message");
    //友好提示
    String errorMessage = getErrorMessage(cause);
    //后台打印日志信息方方便查错
    log.info(message, cause);
    response.setStatus(status);
    if (!isJsonRequest(request)) {
        ModelAndView view = new ModelAndView("/error.html");
        view.addAllObjects(model);
        view.addObject("status", status);
        view.addObject("errorMessage", errorMessage);
        view.addObject("cause", cause);
        return view;
    } else {
        Map error = new HashMap();
        error.put("success", false);
        error.put("errorMessage", getErrorMessage(cause));
        error.put("message", message);
        writeJson(response, error);
        return null;
    }


}
 
Example 19
@RequestMapping(ERROR_PATH)
public ModelAndView getErrorPath(HttpServletRequest request, HttpServletResponse response) {
	Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
			request, false));
	Throwable cause =getCause(request);
	int status =  (Integer)model.get("status");
	//错误信息
	String message = (String)model.get("message");
	//友好提示
	String errorMessage = getErrorMessage(cause);
	String requestPath = (String)model.get("path");
	
	List<FieldError> filedErrors = this.getFieldError(model, cause);
	
	//后台打印日志信息方方便查错
	log.info(status+":"+message+filedErrors, cause);
	log.info("requestPath"+":"+requestPath);
	
	
	response.setStatus(status);
	if(!isJsonRequest(request)){
		ModelAndView view = new ModelAndView("/error.html");
		view.addAllObjects(model);
		view.addObject("errorMessage", errorMessage);
		view.addObject("filedErrors", filedErrors);
		view.addObject("cause", cause);
		view.addObject("requestPath", requestPath);
		
		return view;
		
	}else{
		if(filedErrors==null){
			if(status==404){
				writeJson(response,JsonResult.http404(requestPath));
			}else{
				writeJson(response,JsonResult.failMessage(getErrorMessage(cause)));
			}
			
		}else{
			
			writeJson(response,JsonResult.fail(this.wrapFieldErrors(filedErrors)));
		}
		
	
		return null;
	}
	
	
}
 
Example 20
Source Project: kaif   File: PartTemplate.java    License: Apache License 2.0 4 votes vote down vote up
private static ModelAndView layout(String layoutName) {
  ModelAndView modelAndView = new ModelAndView("part-template");
  modelAndView.addAllObjects(ImmutableMap.of("part", ImmutableMap.of("layout", layoutName)));
  return modelAndView;
}