一、视图解析器:
1.springmvc核心配置文件,添加视图解析器:
1 |
2.视图解析器的使用:
1 2 3 4 5 6 7 8 | @Controller public class JumpAction { @RequestMapping ( "/one" ) public String one(){ System.out.println( "请求转发页面(默认)" ); return "main" ; //方法返回"main","main"会被视图解析器添加前后缀,变成/admin/main.jsp,接着访问该URI对应的页面。 } |
3、视图解析器类InternalResourceViewResolver源码解析:
1 2 3 4 5 | public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered { public static final String REDIRECT_URL_PREFIX = "redirect:" ; //重定向 public static final String FORWARD_URL_PREFIX = "forward:" ; //转发 private String prefix = "" ; //前缀 private String suffix = "" ; //后缀 |
- 在springmvc核心配置文件中配置视图解析器,为视图解析器添加前后缀,实际上是给视图解析器类InternalResourceViewResolver的成员方法赋值,视图解析器类会自动为Action类的方法中return的字符串进行拼接,拼接两个成员方法作为前后缀生成新的URI。
- 我们注意到视图解析器类InternalResourceViewResolver还有两个静态成员变量,如果Action类的方法中return的字符串包含这两个值时,视图解析器类就不再进行前缀后缀的拼接。
二、SpringMVC四种跳转方式:
- 请求转发页面。
- 请求转发action。
- 重定向页面。
- 重定向action。
1.跳转方式案例:
前端:
1 | <a href= "%24%7BpageContext.request.contextPath%7D/one.action" rel= "external nofollow" >请求转发页面(默认)</a><br><a href= "%24%7BpageContext.request.contextPath%7D/two.action" rel= "external nofollow" >请求转发action</a><br><a href= "%24%7BpageContext.request.contextPath%7D/three.action" rel= "external nofollow" >重定向页面</a><br><a href= "%24%7BpageContext.request.contextPath%7D/four.action" rel= "external nofollow" >重定向action</a><br> |
后端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @Controller public class JumpAction { @RequestMapping ( "/one" ) public String one(){ System.out.println( "请求转发页面(默认)" ); //return "main"; //这种访问方式,默认会调用视图解析器自动拼接前缀和后缀进行请求转发页面跳转 return "forward:/fore/user.jsp" ; //只要使用了forward:就可以屏蔽前缀和后缀的拼接,自己手工构建返回的路径 } @RequestMapping ( "/two" ) public String two(){ System.out.println( "请求转发action" ); return "forward:/other.action" ; } @RequestMapping ( "/three" ) public String three(){ System.out.println( "重定向页面" ); return "redirect:/admin/main.jsp" ; //只要使用了redirect:就可以屏蔽前缀和后缀的拼接,自己手工构建返回的路径 } @RequestMapping ( "/four" ) public String four(){ System.out.println( "重定向action" ); //观察地址栏的变化 http://localhost:8080/other.action return "redirect:/other.action" ; } } |
到此这篇关于SpringMVC的四种跳转方式、视图解析器的文章就介绍到这了,更多相关SpringMVC跳转方式内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!