SpringMVC_重定向与转发

web.xml中设置的拦截*.htm,以后项目有可能会改为其他的如*.shtml,这时类中的@RequestMapping里的value都要改,因此,以后写@RequestMapping的时候,不加后缀。改jsp中的form表单里<form action="<%=basePath %>test1.htm" method=post>的后缀就行了。

1.传统Servlet实现重定向与转发

先在Tomcat文件夹下lib中找到包:servlet-api.jar包,复制到WEB-INF下的lib中。就可以用servlet了。

servlet转发:跳到那个页面,但网址没有

package cn.java.controller.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Scope("prototype") //设置为多例,Scope只能修饰类,如果设置为单例,只能等张三用完,李四才能用,多例的话张三创建一个,李四创建一个,互不干扰
public class HomeController {
    
    @RequestMapping(value="test1")
    public void  test1(String username,String password,HttpServletRequest request,HttpServletResponse response) throws Exception {
        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            request.getRequestDispatcher("/pages/front/success.jsp").forward(request, response);//路径不加工程名。跳到登录成功的页面,属于转发,跳到那个页面后地址还是test1.htm
        }
        else { //登录失败
            request.getRequestDispatcher("/index.jsp").forward(request, response);//跳回原地不动
            System.out.println("登录失败");
        }
        
    }
}

servlet重定向:跳到那个页面,网址也变成那个页面了

package cn.java.controller.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Scope("prototype") //设置为多例,Scope只能修饰类,如果设置为单例,只能等张三用完,李四才能用,多例的话张三创建一个,李四创建一个,互不干扰
public class HomeController {
    
    @RequestMapping(value="test1")
    public void  test1(String username,String password,HttpServletRequest request,HttpServletResponse response) throws Exception {
        
        String basePath = request.getContextPath();//动态获取工程名

        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            //request.getRequestDispatcher("/pages/front/success.jsp").forward(request, response);//跳到登录成功的页面,属于转发,跳到那个页面后地址还是test1.htm
            response.sendRedirect(basePath+"/pages/front/success.jsp");//路径加工程名.属于重定向,跳到成功登录的页面,页面地址也变为了success.jsp了。
        }
        else { //登录失败
            //request.getRequestDispatcher("/index.jsp").forward(request, response);//跳回原地不动
            response.sendRedirect(basePath+"/index.jsp");//路径加工程名.属于重定向,跳到成功登录的页面,页面地址也变为了success.jsp了。
            System.out.println("登录失败");
        }
        
    }
}

2.springMVC实现重定向/转发

    @RequestMapping(value="test2")
    public String test1(String username,String password) {
        
        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            return "redirect:/pages/front/success.jsp";//重定向,地址也会变化,springMVC直接动态获取工程名
        }
        else { //登录失败
            return "forward:/pages/front/fail.jsp";//转发,地址不变化。springMVC直接动态获取工程名
        }
        
    }

return里要有关键字:redirect和forward。如果没有关键字就默认是转发。

原文地址:https://www.cnblogs.com/lonske/p/9099462.html