Passing model attribute during redirect in spring MVC and avoiding the same in URL

I am a newbie to Spring I have searched web and related posts in stackoverflow too. I didn't find the one that i actually need. My objective is to pass model attributes from controller to jsp page during a redirect and avoid the attribute being displayed in URL. The below is validating login from datastore using jdo.

My Controller:

public class LoginController {
    int count;
    PersistenceManager pm = PMF.get().getPersistenceManager();

    //Instance of data class
        User user;
    ModelAndView modelAndView=new ModelAndView();

    @RequestMapping(value="/Login",method = RequestMethod.POST)
    public ModelAndView loginValidate(HttpServletRequest req){

        //Getting login values
        String uname=req.getParameter("nameLogin");
        String pswd1=req.getParameter("pswdLogin");

        user=new User();

        //Generating Query
        Query q = pm.newQuery(User.class);
        q.setFilter("userName == userNameParam");
        q.declareParameters("String userNameParam");

            List<User> results = (List<User>) q.execute(uname);  
            for (User u: results) {

                String userName=u.getUserName();



                        return modelAndView;

         //rest of the logic 


 <h1>Welcome ${USERNAME} </h1>

My URL is /welcome?USERNAME=robin My objective is to show it as /welcome Also, my page is supposed to display "Welcome robin" whereas it displays only Welcome. Please let me know how can I resolve it. Thanks in advance!


You should use the flash attribute facility of Spring MVC. When you want data to be accessible after a redirect, you do not add it to the model (Model, ModelMap or ModelAndView) but to a RedirectAttributes that you get as parameter in your controller method.

@RequestMapping(value="/Login",method = RequestMethod.POST)
public ModelAndView loginValidate(HttpServletRequest req, RedirectAttributes redir){

    return modelAndView;

Those flash attributes are passed via the session (and are destroyed immediately after being used - see Spring Reference Manual for details). This has two interests :

  • they are not visible in URL
  • you are not restricted to String, but may pass arbitrary objects.

You need to be careful here because I think what are you trying to do is not supported for a good reason. The "redirect" directive will issue a GET request to your controller. The GET request should only retrieve existing state using request parameters, this is the method contract. That GET request should not rely on a previous interaction or on any object stored some where in the session as a result of it. GET request is designed to retrieve existing (persisted) state. Your original (POST) request should have persisted everything you need for you GET request to retrieve a state.

RedirectAttributes are not designed to support you in this case, and even if you managed to correctly use it it will only work once and then they will be destroyed. If you then refresh the browser you will get an application error because it cannot find your attributes anymore.

All the objects that are send using redirect can be passed in URL itself from controller as -

String message = "Hi Hello etc etc"
return new ModelAndView("redirect:" + "welcome", "message", message);

This message variable will be available in URL as a GET request -

http://localhost:8080/Demo/welcome?message=Hi Hello etc etc

You can now access the variable on your JSP through Scriptlet -

<%= request.getParameter("message") %>

Need Your Help

search in java ArrayList

java search arraylist

I'm trying to figure out the best way to search a customer in an ArrayList by its Id number. The code below is not working; the compiler tells me that I am missing a return statement.

Parcelable and inheritance in Android

java android design-patterns inheritance parcelable

I got an implementation of Parcelable working for a single class that involves no inheritance. I have problems figuring out the best way to implement the interface when it come to inheritance. Let'...