SpringMVC框架简介和请求处理流程-3

253 阅读1分钟

「这是我参与2022首次更文挑战的第28天,活动详情查看:2022首次更文挑战」。

继续上篇文章,接着说说springmvc请求的处理过程

3.参考地址

1) 在你的页面中的,访问地址不加 "/"

访问的是: http://localhost:8080/ch06_path/index.jsp

路径: http://localhost:8080/ch06_path/

资源: index.jsp

在index.jsp发起 user/some.do请求,访问地址变为 http://localhost:8080/ch06_path/user/some.do

当你的地址 没有斜杠开头,例如 user/some.do , 当你点击链接时, 访问地址是当前页面的地址加上链接的地址。

http://localhost:8080/ch06_path/ + user/some.do


index.jsp 访问 user/some.do , 返回后现在的地址: http://localhost:8080/ch06_path/user/some.do

http://localhost:8080/ch06_path/user/some.do

路径:http://localhost:8080/ch06_path/user/

资源:some.do

在index.jsp在 user/some.do ,就变为 http://localhost:8080/ch06_path/user/user/some.do

解决方案:

1.加入${pageContext.request.contextPath}

2.加入一个base标签, 是html语言中的标签。 表示当前页面中访问地址的基地址。

你的页面中所有 没有“/”开头的地址,都是以base标签中的地址为参考地址

使用base中的地址 + user/some.do 组成访问地址

2)在你的页面中的,访问地址加 "/"

访问的是: http://localhost:8080/ch06_path/index.jsp

路径: http://localhost:8080/ch06_path/

资源: index.jsp

点击 /user/some.do, 访问地址变为 http://localhost:8080/user/some.do

参考地址是 你的服务器地址, 也就是 http://localhost:8080

如果你的资源不能访问: 加入${pageContext.request.contextPath}

<a href="${pageContext.request.contextPath}/user/some.do">发起user/some.do的get请求</a>

index.jsp--addStudent.jsp---student/addStudent.do( service的方法,调用dao的方法)--result.jsp

================================================

ch08-forard-redirect:转发和重定向

forward:表示转发

redirect:表示重定向

forward和redirect都是关键字, 有一个共同的特点不和视图解析器一同工作

扩展:

forward和redirect他们都可以访问 视图文件,比如某个jsp ,html

forward:/hello.jsp forward:/main.html

forward和redirect他们都可以访问其它的controller

forward:/some.do , redirect:/other.do

处理器方法可以返回ModelAndView, String , void 都可以使用forward,redirect

============================================================