Languages
[Edit]
EN

Java - spring attribute redirection

13 points
Created by:
Greg
5140

In Spring Freamwork it is possible to make attribute redirection in following way.

1. RedirectAttributes class examples

1.1. redirect: prefix

RedirectionExampleController.java file:

package com.dirask.examples;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;

@Controller
public class RedirectionExampleController {

    @RequestMapping(value = "/redirect-to-my-final-page",
        method = RequestMethod.GET)
    public String redirectToMyFinalPage(RedirectAttributes attributes) {

        attributes.addFlashAttribute("my-redirected-message",
            "This is my message!");

        return "redirect:/my-final-page";
    }

    @RequestMapping(value = "/my-final-page",
            method = RequestMethod.GET)
    @ResponseBody
    public String myFinalPage(Model model) {

        Map<String, Object> attributes = model.asMap();

        return (String)attributes.get("my-redirected-message");
    }
}

 Running:

  1. Run server, e.g. on localhost:8080
  2. Open in browser following link: http://localhost:8080/redirect-to-my-final-page

Result:

Note: web page should be redirected to http://localhost:8080/my-final-page.

1.2. ModelAndView class

In this section alternative redirection approach with ModelAndView class is presented.

RedirectionExampleController.java file:

package com.dirask.examples;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;

@Controller
public class RedirectionExampleController {

    @RequestMapping(value = "/redirect-to-my-final-page",
        method = RequestMethod.GET)
    public ModelAndView redirectToMyFinalPage(RedirectAttributes attributes) {

        attributes.addFlashAttribute("my-redirected-message",
            "This is my message!");

        return new ModelAndView(new RedirectView("/my-final-page", true));
    }

    @RequestMapping(value = "/my-final-page",
            method = RequestMethod.GET)
    @ResponseBody
    public String myFinalPage(Model model) {

        Map<String, Object> attributes = model.asMap();

        return (String)attributes.get("my-redirected-message");
    }
}

1.3. RedirectView class

In this section alternative redirection approach with RedirectView class is presented.

RedirectionExampleController.java file:

package com.dirask.examples;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;

@Controller
public class RedirectionExampleController {

    @RequestMapping(value = "/redirect-to-my-final-page",
        method = RequestMethod.GET)
    public RedirectView redirectToMyFinalPage(RedirectAttributes attributes) {

        attributes.addFlashAttribute("my-redirected-message",
            "This is my message!");

        return new RedirectView("/my-final-page", true);
    }

    @RequestMapping(value = "/my-final-page",
            method = RequestMethod.GET)
    @ResponseBody
    public String myFinalPage(Model model) {

        Map<String, Object> attributes = model.asMap();

        return (String)attributes.get("my-redirected-message");
    }
}

2. References

  1. Redirecting - Spring Docs 
  2. RedirectAttributes Class - Spring Docs
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join