SpringMVC请求重定向与请求转发

SpringMVC中测试:

请求转发:一次请求,请求状态会保存,url不会保存。

@RequestMapping(value = "judge", method = RequestMethod.POST)
    public String judge(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        String username = request.getParameter("username");
        ResultSet rs = userService.usernameIsEmpty(request);
        //用户名不空
        if(rs!=null)
        {
            User user = userService.queryByUsername(username);
            String password = request.getParameter("password");
            if(!password.equals(user.getPassword())) return "fail";
            else {//密码正确,则请求转发
//                request.setAttribute("forward",user);可以不要
                return "forward:list";
            }
        }

请求重定向:两次请求,第一次请求的状态不会被保存,url会改变。

@RequestMapping(“/redirect1”)  
    public String index2(RedirectAttributes attr) throws UnsupportedEncodingException{  
        attr.addAttribute(”ds”, URLEncoder.encode(“重定向”, “utf-8”));  
        return “redirect:redirect2.html”;  
    }  
    @RequestMapping(“/redirect2”)  
    public String index22(String ds) throws UnsupportedEncodingException{  
        System.out.println(URLDecoder.decode(ds, ”utf-8”));  
        return “index”;  
    }  

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/81813455