Moumita Das Purba
Moumita Das Purba

Reputation: 35

Change url path in Spring MVC by @RequestMapping

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. :)

Upvotes: 2

Views: 8200

Answers (2)

Ralph
Ralph

Reputation: 120771

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

Upvotes: 1

codependent
codependent

Reputation: 24442

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";

Upvotes: 1

Related Questions