2

Currently path is showing

http://localhost:8081/UserLogin/login

But i want this as

http://localhost:8081/UserLogin/index
or
http://localhost:8081/UserLogin/

My controller class is

@RequestMapping(value = "/login" ,method = RequestMethod.POST)
  public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
      //return "hi this is a test";
      String userName = request.getParameter("data[Admin][user_name]");
      String userPass=request.getParameter("data[Admin][password]");
      int userId=userDAO.getUser(userName, userPass);
      if(userId!=0){  
        String message = "welcome!!!"; 
        return new ModelAndView("result", "message", message);  
      }  
      else{  
        String message = "fail";
        return new ModelAndView("index", "message",message);  
      } 
  }

Want to change in else condition when not match. Thanks in advance. :)

2 Answers 2

1

I would return a redirect to render the view under the new URL:

request.addAttribute("message",message) // better use a Model
return "redirect:/[SERVLET_MAPPING]/index";
Sign up to request clarification or add additional context in comments.

3 Comments

is [SERVLET_MAPPING] a feature/placeholder replaced by Spring?
No, you have to put there the actual servlet mapping
Then I would recommend to return new ModelAndView(new RedirectView("/index", true)) because the last parameter true will insert the servlet mapping
1

It take some time to understand what you want: - I guess you want to alter the URL that is returned from the Server after login.

But this does not work this way, because the URL is requested from the browser and the server can not change them. Instead the server can respond an "HTTP 303 Redirect" (instead of the view). This cause the Browser to load the URL given with the Redirect.

  @RequestMapping(value = "/login" ,method = RequestMethod.POST)
  public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
      //return "hi this is a test";
      String userName = request.getParameter("data[Admin][user_name]");
      String userPass=request.getParameter("data[Admin][password]");
      int userId=userDAO.getUser(userName, userPass);
      if(userId!=0){  
        return new ModelAndView(new RedirectView("/result", true));  // "/result" this is/become an URL!
      }  
      else {  
        return new ModelAndView(new RedirectView("/index", true));  // "/index" this is/become an URL!
      } 
  }

  @RequestMapping(value = "/index" ,method = RequestMethod.GET)
  public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
      String message = "fail";
      return new ModelAndView("index", "message",message);  //"index" the the name of an jsp (or other template)!!
  }

  @RequestMapping(value = "/result" ,method = RequestMethod.GET)
  public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
      String message = "welcome!!!"; 
      return new ModelAndView("result", "message", message);  //"result" the the name of an jsp (or other template)!! 
  }

@See http://en.wikipedia.org/wiki/URL_redirection

2 Comments

Hi Ralph, I get the redirect thing. But the thing is, I am also adding 'error' attribute in model to view as error, but it is not persisted on redirect. Which means no error on redirected page. How can I keep the error as well? My code: mv = new ModelAndView(new RedirectView("/", true)); mv.getModelMap().addAttribute("error", "Blank Error."); return mv;
@AakashPatel: have a look at Spring's "Flash Attributes" (docs.spring.io/spring/docs/current/spring-framework-reference/…) - The Spring Reference has not very much content about it, so have a look at this three tuorials: dzone.com/articles/spring-mvc-flash-attributes , Chapter 6: baeldung.com/spring-redirect-and-forward , baeldung.com/spring-web-flash-attributes and this StackOverflow answer stackoverflow.com/a/25598824/280244

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.