Java Code Examples for org.springframework.web.servlet.mvc.support.RedirectAttributes#addAttribute()

The following examples show how to use org.springframework.web.servlet.mvc.support.RedirectAttributes#addAttribute() . 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: CustomFieldEditController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping(method= RequestMethod.GET)
public String edit(
		@PathVariable String language,
		@RequestParam long id,
		Model model,
		RedirectAttributes redirectAttributes) {
	CustomField customField = (CustomField) model.asMap().get("customField");
	if (!language.equals(customField.getLanguage())) {
		redirectAttributes.addAttribute("language", language);
		return "redirect:/_admin/{language}/customfields/index";
	}

	CustomFieldEditForm form = CustomFieldEditForm.fromDomainObject(customField);
	model.addAttribute("form", form);

	return "customfield/edit";
}
 
Example 2
Source File: OnlineEditorController.java    From es with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/create/file")
public String createFile(
        @RequestParam(value = "parentPath") String parentPath,
        @RequestParam(value = "name") String name,
        RedirectAttributes redirectAttributes) throws IOException {

    if(isValidFileName(name)) {
        String rootPath = sc.getRealPath(ROOT_DIR);
        parentPath = URLDecoder.decode(parentPath, Constants.ENCODING);

        File parent = new File(rootPath + File.separator + parentPath);
        File currentFile = new File(parent, name);
        currentFile.getParentFile().mkdirs();
        boolean result = currentFile.createNewFile();
        if(result == false) {
            redirectAttributes.addFlashAttribute(Constants.ERROR, "名称为[" + name + "]的文件/目录已经存在");
        } else {
            redirectAttributes.addFlashAttribute(Constants.MESSAGE, "创建成功!");
        }
    } else {
        redirectAttributes.addFlashAttribute(Constants.ERROR, "名称为[" + name + "]不是合法的文件名,请重新命名");
    }

    redirectAttributes.addAttribute("path", parentPath);
    return redirectToUrl(viewName("list"));
}
 
Example 3
Source File: SystemIndexController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@Transactional(propagation = Propagation.REQUIRED)
@RequestMapping(value = "/clear-cache", method = RequestMethod.POST)
public String clearCache(
		@PathVariable String language,
		RedirectAttributes redirectAttributes,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {

	WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext, "org.springframework.web.servlet.FrameworkServlet.CONTEXT.guestServlet");
	if (context == null) {
		throw new ServiceException("GuestServlet is not ready yet");
	}

	DefaultModelAttributeInterceptor interceptor = context.getBean(DefaultModelAttributeInterceptor.class);
	ModelAndView mv = new ModelAndView("dummy");
	interceptor.postHandle(request, response, this, mv);

	SpringTemplateEngine templateEngine = context.getBean("templateEngine", SpringTemplateEngine.class);
	logger.info("Clear cache started");
	templateEngine.clearTemplateCache();
	logger.info("Clear cache finished");

	redirectAttributes.addFlashAttribute("clearCache", true);
	redirectAttributes.addAttribute("language", language);
	return "redirect:/_admin/{language}/system";
}
 
Example 4
Source File: ArticleBulkUnpublishController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping
public String unpublish(
		@Valid @ModelAttribute("form") ArticleBulkUnpublishForm form,
		BindingResult errors,
		String query,
		AuthorizedUser authorizedUser,
		RedirectAttributes redirectAttributes) {
	redirectAttributes.addAttribute("query", query);

	if (errors.hasErrors()) {
		logger.debug("Errors: {}", errors);
		return "redirect:/_admin/{language}/articles/index";
	}

	Collection<Article> unpublishedArticles;
	try {
		unpublishedArticles = articleService.bulkUnpublishArticle(form.toArticleBulkUnpublishRequest(), authorizedUser);
	} catch (ServiceException e) {
		return "redirect:/_admin/{language}/articles/index";
	}

	redirectAttributes.addFlashAttribute("unpublishedArticles", unpublishedArticles);
	return "redirect:/_admin/{language}/articles/index";
}
 
Example 5
Source File: CommentBulkUnapproveController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping
public String unapprove(
		@Valid @ModelAttribute("form") CommentBulkUnapproveForm form,
		BindingResult errors,
		String query,
		AuthorizedUser authorizedUser,
		RedirectAttributes redirectAttributes,
		Model model) {
	redirectAttributes.addAttribute("query", query);

	if (errors.hasErrors()) {
		logger.debug("Errors: {}", errors);
		return "redirect:/_admin/{language}/comments/index";
	}

	Collection<Comment> unapprovedComments;
	try {
		unapprovedComments = commentService.bulkUnapproveComment(form.toCommentBulkUnapproveRequest(), authorizedUser);
	} catch (ServiceException e) {
		return "redirect:/_admin/{language}/comments/index";
	}

	redirectAttributes.addFlashAttribute("unapprovedComments", unapprovedComments);
	return "redirect:/_admin/{language}/comments/index";
}
 
Example 6
Source File: OnlineEditorController.java    From es with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/rename")
public String rename(
        @RequestParam(value = "path") String path,
        @RequestParam(value = "newName") String newName,
        RedirectAttributes redirectAttributes) throws IOException {

    String rootPath = sc.getRealPath(ROOT_DIR);
    path = URLDecoder.decode(path, Constants.ENCODING);

    File current = new File(rootPath + File.separator + path);
    File parent = current.getParentFile();
    String parentPath = parent.getAbsolutePath().replace(rootPath, "");

    File renameToFile = new File(parent, newName);
    boolean result = current.renameTo(renameToFile);
    if(result == false) {
        redirectAttributes.addFlashAttribute(Constants.ERROR, "名称为[" + newName + "]的文件/目录已经存在");
    } else {
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "重命名成功");
    }

    redirectAttributes.addAttribute("path", parentPath);
    return redirectToUrl(viewName("list"));
}
 
Example 7
Source File: RedirectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@PostMapping("/persons")
public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
	if (errors.hasErrors()) {
		return "persons/add";
	}
	redirectAttrs.addAttribute("name", "Joe");
	redirectAttrs.addFlashAttribute("message", "success!");
	return "redirect:/persons/{name}";
}
 
Example 8
Source File: FloatedCourseController.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
@PostMapping("/unfloat")
public String unfloat(RedirectAttributes redirectAttributes,  @PathVariable Course course) {
    FloatedCourse floatedCourse = floatedCourseService.getFloatedCourse(course).orElseThrow(FloatedCourseNotFoundException::new);

    floatedCourseService.unfloatCourse(floatedCourse);
    redirectAttributes.addFlashAttribute("course_success", "Course " + course.getCode() + " unfloated successfully!");
    redirectAttributes.addAttribute("department", course.getDepartment());

    return "redirect:/admin/department/{department}/courses?active=true";
}
 
Example 9
Source File: RedirectPageController.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@RequestMapping(value="/jump_page.html", method=RequestMethod.POST)
public RedirectView sendRedirection(RedirectAttributes atts, @RequestParam("username") String username, 
		@RequestParam("password") String password){
	atts.addFlashAttribute("username", username);
	atts.addFlashAttribute("password", password);
	atts.addAttribute("request", "loginForm");
	return new RedirectView("/redirectviewOld.html",true);
}
 
Example 10
Source File: RedirectPageController.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@RequestMapping(value="/jump_page.html", method=RequestMethod.POST)
public RedirectView sendRedirection(RedirectAttributes atts, @RequestParam("username") String username, 
		@RequestParam("password") String password){
	atts.addFlashAttribute("username", username);
	atts.addFlashAttribute("password", password);
	atts.addAttribute("request", "loginForm");
	return new RedirectView("/redirectviewOld.html",true);
}
 
Example 11
Source File: ArticleEditController.java    From wallride with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method=RequestMethod.GET)
public String edit(
		@PathVariable String language,
		@RequestParam long id,
		Model model,
		RedirectAttributes redirectAttributes) {
	Article article = (Article) model.asMap().get("article");
	if (article == null) {
		throw new HttpNotFoundException();
	}

	if (!article.getLanguage().equals(language)) {
		Article target = articleService.getArticleByCode(article.getCode(), language);
		if (target != null) {
			redirectAttributes.addAttribute("id", target.getId());
			return "redirect:/_admin/{language}/articles/edit?id={id}";
		} else {
			redirectAttributes.addFlashAttribute("original", article);
			redirectAttributes.addAttribute("code", article.getCode());
			return "redirect:/_admin/{language}/articles/create?code={code}";
		}
	}

	Set<CustomField> customFields = customFieldService.getAllCustomFields(language);
	ArticleEditForm form = ArticleEditForm.fromDomainObject(article, customFields);
	model.addAttribute("form", form);

	Article draft = articleService.getDraftById(id);
	model.addAttribute("draft", draft);

	return "article/edit";
}
 
Example 12
Source File: WebController.java    From xxl-sso with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Logout
 *
 * @param request
 * @param redirectAttributes
 * @return
 */
@RequestMapping(Conf.SSO_LOGOUT)
public String logout(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {

    // logout
    SsoWebLoginHelper.logout(request, response);

    redirectAttributes.addAttribute(Conf.REDIRECT_URL, request.getParameter(Conf.REDIRECT_URL));
    return "redirect:/login";
}
 
Example 13
Source File: RedirectTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value="/people", method=RequestMethod.POST)
public Object saveSpecial(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
	if (errors.hasErrors()) {
		return "persons/add";
	}
	redirectAttrs.addAttribute("name", "Joe");
	redirectAttrs.addFlashAttribute("message", "success!");
	return new StringBuilder("redirect:").append("/persons").append("/{name}");
}
 
Example 14
Source File: RedirectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@PostMapping
public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
	if (errors.hasErrors()) {
		return "persons/add";
	}
	redirectAttrs.addAttribute("name", "Joe");
	redirectAttrs.addFlashAttribute("message", "success!");
	return "redirect:/persons/{name}";
}
 
Example 15
Source File: UserEditController.java    From wallride with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method= RequestMethod.POST, params="_step.save")
public String save(
		@PathVariable String language,
		@Valid @ModelAttribute("form") UserEditForm form,
		BindingResult errors,
		String query,
		AuthorizedUser authorizedUser,
		HttpServletRequest request,
		RedirectAttributes redirectAttributes) {
	if (errors.hasErrors()) {
		return "user/edit";
	}

	User user = null;
	try {
		user = userService.updateUser(form.buildUserUpdateRequest(), errors, authorizedUser);
	}
	catch (ValidationException e) {
		if (errors.hasErrors()) {
			logger.debug("Errors: {}", errors);
			return "user/edit";
		}
		throw e;
	}
	
	redirectAttributes.addFlashAttribute("savedUser", user);
	redirectAttributes.addAttribute("language", language);
	redirectAttributes.addAttribute("id", user.getId());
	redirectAttributes.addAttribute("query", query);
	return "redirect:/_admin/{language}/users/describe";
}
 
Example 16
Source File: PageEditController.java    From wallride with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method=RequestMethod.GET, params="draft")
public String editDraft(
		@PathVariable String language,
		@RequestParam long id,
		String query,
		Model model,
		RedirectAttributes redirectAttributes) {
	Page page = (Page) model.asMap().get("page");
	if (!language.equals(page.getLanguage())) {
		redirectAttributes.addAttribute("language", language);
		redirectAttributes.addAttribute("query", query);
		return "redirect:/_admin/{language}/pages/index";
	}

	Page draft = pageService.getDraftById(id);
	if (draft == null) {
		redirectAttributes.addAttribute("language", language);
		redirectAttributes.addAttribute("id", id);
		redirectAttributes.addAttribute("query", query);
		return "redirect:/_admin/{language}/pages/edit";
	}
	Set<CustomField> customFields = customFieldService.getAllCustomFields(language);
	PageEditForm form = PageEditForm.fromDomainObject(draft, customFields);
	model.addAttribute("form", form);

	return "page/edit";
}
 
Example 17
Source File: RedirectController.java    From tutorials with MIT License 4 votes vote down vote up
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
    redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
    return new RedirectView("redirectedUrl");
}
 
Example 18
Source File: WebController.java    From xxl-sso with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Login
 *
 * @param request
 * @param redirectAttributes
 * @param username
 * @param password
 * @return
 */
@RequestMapping("/doLogin")
public String doLogin(HttpServletRequest request,
                    HttpServletResponse response,
                    RedirectAttributes redirectAttributes,
                    String username,
                    String password,
                    String ifRemember) {

    boolean ifRem = (ifRemember!=null&&"on".equals(ifRemember))?true:false;

    // valid login
    ReturnT<UserInfo> result = userService.findUser(username, password);
    if (result.getCode() != ReturnT.SUCCESS_CODE) {
        redirectAttributes.addAttribute("errorMsg", result.getMsg());

        redirectAttributes.addAttribute(Conf.REDIRECT_URL, request.getParameter(Conf.REDIRECT_URL));
        return "redirect:/login";
    }

    // 1、make xxl-sso user
    XxlSsoUser xxlUser = new XxlSsoUser();
    xxlUser.setUserid(String.valueOf(result.getData().getUserid()));
    xxlUser.setUsername(result.getData().getUsername());
    xxlUser.setVersion(UUID.randomUUID().toString().replaceAll("-", ""));
    xxlUser.setExpireMinute(SsoLoginStore.getRedisExpireMinute());
    xxlUser.setExpireFreshTime(System.currentTimeMillis());


    // 2、make session id
    String sessionId = SsoSessionIdHelper.makeSessionId(xxlUser);

    // 3、login, store storeKey + cookie sessionId
    SsoWebLoginHelper.login(response, sessionId, xxlUser, ifRem);

    // 4、return, redirect sessionId
    String redirectUrl = request.getParameter(Conf.REDIRECT_URL);
    if (redirectUrl!=null && redirectUrl.trim().length()>0) {
        String redirectUrlFinal = redirectUrl + "?" + Conf.SSO_SESSIONID + "=" + sessionId;
        return "redirect:" + redirectUrlFinal;
    } else {
        return "redirect:/";
    }

}
 
Example 19
Source File: CustomFieldBulkDeleteController.java    From wallride with Apache License 2.0 4 votes vote down vote up
@RequestMapping
public String delete(
		@Valid @ModelAttribute("form") CustomFieldBulkDeleteForm form,
		BindingResult errors,
		String query,
		AuthorizedUser authorizedUser,
		RedirectAttributes redirectAttributes) {
	redirectAttributes.addAttribute("query", query);

	if (!form.isConfirmed()) {
		errors.rejectValue("confirmed", "Confirmed");
	}
	if (errors.hasErrors()) {
		logger.debug("Errors: {}", errors);
		return "redirect:/_admin/{language}/customfields/index";
	}
	
	Collection<CustomField> customFields = null;
	try {
		customFields = customFieldService.bulkDeleteCustomField(form.buildCustomFieldBulkDeleteRequest(), errors);
	}
	catch (ValidationException e) {
		if (errors.hasErrors()) {
			logger.debug("Errors: {}", errors);
			return "redirect:/_admin/{language}/customfields/index";
		}
		throw e;
	}
	
	List<String> errorMessages = null;
	if (errors.hasErrors()) {
		errorMessages = new ArrayList<>();
		for (ObjectError error : errors.getAllErrors()) {
			errorMessages.add(messageSourceAccessor.getMessage(error));
		}
	}
	
	redirectAttributes.addFlashAttribute("deletedCustomFields", customFields);
	redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
	return "redirect:/_admin/{language}/customfields/index";
}
 
Example 20
Source File: ArticleBulkDeleteController.java    From wallride with Apache License 2.0 4 votes vote down vote up
@RequestMapping
public String delete(
		@Valid @ModelAttribute("form") ArticleBulkDeleteForm form,
		BindingResult errors,
		String query,
		AuthorizedUser authorizedUser,
		RedirectAttributes redirectAttributes) {
	redirectAttributes.addAttribute("query", query);

	if (!form.isConfirmed()) {
		errors.rejectValue("confirmed", "Confirmed");
	}
	if (errors.hasErrors()) {
		logger.debug("Errors: {}", errors);
		return "redirect:/_admin/{language}/articles/index";
	}
	
	Collection<Article> articles = null;
	try {
		articles = articleService.bulkDeleteArticle(form.buildArticleBulkDeleteRequest(), errors);
	}
	catch (ValidationException e) {
		if (errors.hasErrors()) {
			logger.debug("Errors: {}", errors);
			return "redirect:/_admin/{language}/articles/index";
		}
		throw e;
	}
	
	List<String> errorMessages = null;
	if (errors.hasErrors()) {
		errorMessages = new ArrayList<>();
		for (ObjectError error : errors.getAllErrors()) {
			errorMessages.add(messageSourceAccessor.getMessage(error));
		}
	}
	
	redirectAttributes.addFlashAttribute("deletedArticles", articles);
	redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
	return "redirect:/_admin/{language}/articles/index";
}