Siddhant Sahni
Siddhant Sahni

Reputation: 157

Redirect after POST method in spring MVC

My problem is that I want to redirect to my index page without having to see the attributes appended in the URL.

I found the solution to be addFlashAttributes, it doesn't append attributes in the URL but I can't see any message saved by this method.

Code:

Controller

@RequestMapping(value="/login",method = RequestMethod.POST)  
public ModelAndView loginResult(HttpServletRequest req,
                                HttpServletResponse res,
                                RedirectAttributes redir) {
    if (uname.equals(inf.getUsername()) & &pwd.equals(inf.getPassword()) && dept.equals(inf.getDept()))
    {
        req.getSession().setAttribute("uname",inf.getName());
        return new ModelAndView("employeeLoginResult", "message", message1); 
    }
    else if (uname.equals(inf2.getUsername()) && pwd.equals(inf2.getPassword()) && dept.equals(inf2.getDept()))
    {
        req.getSession().setAttribute("uname", inf2.getName());
        return new ModelAndView("adminLoginResult", "message", message2); 
    }
    else
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/index.jsp");
        redir.addFlashAttribute("message","Sorry Username Password Error");
        return modelAndView;
    }
}

Right now I have hardcoded values for validation and would integrate the validation with DAO layer of database in the future. The addFlashAttribute does the work for me of not appending the message to the URL, but I also want to display the message on the index page.

Index page

<head><tile>index</title>
</head>
<body>
    <b><span class="heading">LOGIN USER</span></b>
    <div class="container">
        <form action="login.html" method="Post">
            <div class="form_style">
            <input type="text" name="username" placeholder="Enter Username"/>
            <input type="password" name="pwd" placeholder="Enter password"/>
            <select name="dept">
                <option>IT</option>
                <option>Admin</option>
                <option>HR</option>
                <option>Marketing</option>
            </select>
            <input type="Submit" value="submit">
            <p>${message}</p>
            </div>
        </form>
    </div>
</body>

What am I doing wrong? Can anyone explain?

Why is ${message} not displaying the attribute?

Any help is appreciated. Thanks for the same.

Upvotes: 0

Views: 5451

Answers (3)

Anjali Pavithra
Anjali Pavithra

Reputation: 72

You can change your Controller as below and use same index.jsp file.

Controller

@RequestMapping(value="/login",method = RequestMethod.POST)  
 public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res, Model model){

    String message = "";
    String response = "";

     if(uname.equals(inf.getUsername())&&pwd.equals(inf.getPassword())&&dept.equals(inf.getDept())) {
                    req.getSession().setAttribute("uname",inf.getName());
        message = message1;
        response = "employeeLoginResult";
            }
         else if(uname.equals(inf2.getUsername())&&pwd.equals(inf2.getPassword())&&dept.equals(inf2.getDept())) {
                 req.getSession().setAttribute("uname",inf2.getName());
        message = message2;
        response = "adminLoginResult"; 
            }
            else{
        message = "Sorry Username Password Error";
        response = "index";
            }
    model.addAttribute("message", message);
    return response;
}

Upvotes: 1

Siddhant Sahni
Siddhant Sahni

Reputation: 157

I think I found a way about this problem by using forward instead of redirect.

The way forward works is that it will forward my attributes from my controller and won't append them onto the URL.

This was the best possible solution for me without changing much of my code.

Code:

I just changed one statement in my controller where I was redirecting to my index page

Controller:

@RequestMapping(value="/login",method = RequestMethod.POST)  
    public ModelAndView loginResult(HttpServletRequest req,HttpServletResponse res,RedirectAttributes redir,Model model)
 {
    InfoEmployee inf = new InfoEmployee();
    InfoManager inf2 = new InfoManager();   
    String pwd=req.getParameter("pwd");  
    String dept = req.getParameter("dept");
    String name1 = inf.getName();
    String message1 = "Welcome "+name1;  
    String name2 = inf2.getName();
    String message2 = "Welcome "+name2;    

if(uname.equals(inf.getUsername())&&pwd.equals(inf.getPassword())&&dept.equals(inf.getDept()))
    {
        req.getSession().setAttribute("uname",inf.getName());
        return new ModelAndView("employeeLoginResult", "message", message1); 

    }
    else if(uname.equals(inf2.getUsername())&&pwd.equals(inf2.getPassword())&&dept.equals(inf2.getDept()))
    {
        req.getSession().setAttribute("uname",inf2.getName());
        return new ModelAndView("adminLoginResult", "message", message2); 
    }
    else
    {


   return new ModelAndView("forward:index.jsp","message","Sorry Username Password Error");
    }
}

The ${message} is now displaying my attribute.

Image:

enter image description here

Upvotes: 0

gamgoon
gamgoon

Reputation: 9

Use the controller for the index page.

attributes using addFlashAttribute will be retrieved using another controller.

After the redirect, flash attributes are automatically added to the model of the controller that serves the target URL. RedirectAttributes

@RequestMapping("/index")
public String index(HttpServletRequest req, HttpServletResponse res, Model model) {
    // model has the attribute 'message'
    return "/indexView";
}

Upvotes: 0

Related Questions