My objective is to pass model attributes from controller to JSP page during a redirect and avoid the attribute being displayed in URL. The source code below is validating login from datastore using java data objects.
Controller:
@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"); count=0; user=new User(); //Generating Query Query q = pm.newQuery(User.class); q.setFilter("userName == userNameParam"); q.declareParameters("String userNameParam"); try{ List<User> results = (List<User>) q.execute(uname); for (User u: results) { String userName=u.getUserName(); if(userName.equals(uname)){ System.out.println(u.getPassword()); if(u.getPassword().equals(pswd1)){ count=count+1; modelAndView.setViewName("redirect:welcome"); modelAndView.addObject("USERNAME",uname); return modelAndView; } //rest of the logic }
JSP:
<h1>Welcome ${USERNAME} </h1>
My current URL is /welcome?USERNAME=robin
My goal is to display it as /welcome
Also, my page is supposed to display "Welcome robin" whereas it displays only Welcome.
Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.
There are two ways you can do this. Two using RedirectView object. The return type of the method should be ResponseEntity<Void>. STEP2: Build response entity object with 302 http status code and the external URL.
Furthermore, there's also an addAttributes() method. Its purpose is to add values in the Model that'll be identified globally. That is, every request to every controller method will return a default value as a response.
Using the redirect prefix in your controller will generate a HTTP response with the 302 status code and the location header pointing to the redirection URL. The browser will then redirect to that URL (model exposed in the first request will be lost, and the browser URL will be the second one).
RedirectAttributes only work with RedirectView, please follow the same
@RequestMapping(value="/Login",method = RequestMethod.POST) public RedirectView loginValidate(HttpServletRequest req, RedirectAttributes redir){ ... redirectView= new RedirectView("/foo",true); redir.addFlashAttribute("USERNAME",uname); return redirectView; }
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With