【问题标题】:Change url path in Spring MVC by @RequestMapping通过 @RequestMapping 更改 Spring MVC 中的 url 路径
【发布时间】:2015-06-14 08:36:18
【问题描述】:

当前路径显示

http://localhost:8081/UserLogin/login

但我想要这个

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

我的控制器类是

@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);  
      } 
  }

想在不匹配时改变 else 条件。 提前致谢。 :)

【问题讨论】:

    标签: spring model-view-controller annotations


    【解决方案1】:

    我将返回重定向以在新 URL 下呈现视图:

    request.addAttribute("message",message) // better use a Model
    return "redirect:/[SERVLET_MAPPING]/index";
    

    【讨论】:

    • [SERVLET_MAPPING] 一个被 Spring 取代的功能/占位符吗?
    • 不,你必须把实际的 servlet 映射放在那里
    • 那我建议返回new ModelAndView(new RedirectView("/index", true)),因为最后一个参数true会插入servlet映射
    【解决方案2】:

    需要一些时间来了解您想要什么:- 我猜您想更改登录后从服务器返回的 URL。

    但这不起作用,因为 URL 是从浏览器请求的,服务器无法更改它们。相反,服务器可以响应“HTTP 303 重定向”(而不是视图)。这会导致浏览器加载通过重定向给出的 URL。

      @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)!! 
      }
    

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

    【讨论】:

    猜你喜欢
    • 2011-03-16
    • 2011-12-12
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2015-07-20
    • 1970-01-01
    • 2011-04-10
    相关资源
    最近更新 更多