package com.tz.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ViewController { @RequestMapping("/view") public String view(){ return "../../view";//相对路径 } @RequestMapping("/view02") public String view02(){ return "forward:/view.jsp";//转发,有前缀的返回值独立解析 } @RequestMapping("/view03") public String view03(){//发送给view02进行处理 return "forward:/view"; } @RequestMapping("/view04") public String view04(){ //response.sendRedirect("/项目名/view.jsp"); return "redirect:/view.jsp";//代表当前项目下开始,springmvc会自动为路径拼接上项目名 } @RequestMapping("/view05") public String view05(){//中转站 //response.sendRedirect("/项目名/view.jsp"); return "redirect:/view04";//代表当前项目下开始,springmvc会自动为路径拼接上项目名 } //使用前缀的转发或者重定向操作,配置的视图解析器就不会帮助拼接字符串 }
index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="view">view</a> <a href="view02">view02</a> <a href="view03">view03</a> <a href="view04">重定向</a> </body> </html>