SpringMVC 返回 html 视图页面,SpringMVC与Servlet,Servlet重定向与转发

1. SpringMVC与Servlet的关系

SpringMVC框架是建立在Servlet之上的,提供各种功能,各种封装,各种方便的同时,它一点儿也没有限制Servlet,我们完全可以在SpringMVC的controller中,完全按照Servlet的方式来写代码,同时还享受SpringMVC提供的方便。所以我们完全没有理由再使用Servlet了。

2. SpringMVC 返回 html 视图页面

SpringMVC的controller一般我们可以配置返回:jsp, json, Velocity, FreeMarker, XML, PDF, Excel等等视图。那么如何返回到 html 视图呢?当然一般我们没有这种必要,但是有一些奇葩场景还是存在的。

上面我们知道SpringMVC没有限制对Servlet有任何的限制,我们完全可以在controller中写servlet形式的代码,而我们知道,我们在Servlet中可以通过 转发和重定向到 html 类型的页面,所以我们也可以在SpringMVC的controller中利用request的转发和response的重定向到html页面,也可以利用PrintWriter直接输出html字符流。这些Servlet中的用法,我们完全可以在SpringMVC中使用。

复制代码
    @RequestMapping(value="/htmlView")
    public void htmlView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        // ...
//      request.getRequestDispatcher("index.html").forward(request, response);
        response.sendRedirect("http://www.baidu.com");
    }
复制代码
    @RequestMapping(value="/getPage")
    public void writeSubmitHtml(Reader reader, Writer writer, HttpSession session) throws IOException {
        User user = (User) session.getAttribute(ConstantConfig.LONGIN_USER);
        StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        sbHtml.append("<title>支付宝即时到账交易接口</title></head><body>"+ user.getNo() +"</body></html>");
        writer.write(sbHtml.toString());  
    }

上面分别使用 request的转发和response的重定向,以及PrintWriter直接输出html字符流,到达springMVC返回 html 类型视图的目的。

3. Servlet重定向与转发

下面的文字,转自:http://blog.sina.com.cn/s/blog_7ffb8dd501013tbg.html

一、请求转发与 响应重定向的种类

有两种方式获得Servlet 转发对象(RequestDispatcher):

一种是通过HttpServletRequest的getRequestDispatcher()方法获得,

一种是通过ServletContext的getRequestDispatcher()方法获得。

Servlet 重定向的方法只有一种:HttpServletResponse的sendRedirect()方法。

这三个方法的参数都是一个URL形式的字符串,但在使用相对路径或绝对路径上有所区别。

二、请求转发与 响应重定向中路径参数区别

假设通过http://localhost/myApp/cool/bar.do 请求到达该方法所属的Servlet。

1、响应重定向 ◆ HttpServletResponse.sendRedirect(String)

参数可以指定为相对路径、绝对路径或其它Web应用。

i:相对路径response.sendRedirect("foo/stuff.do") ,容器相对于原来请求URL的目录加参数来生成完整的URL------**http://localhost/myApp/cool/foo/stuff.do**。

ii:绝对路径response.sendRedirect("/foo/stuff.do") ,容器相对于Web应用本身加参数建立完整的URL,这是因为重定向response.sendRedirect("")是服务器向客户端发送一个请求头信息,由客户端 请求一次服务器,请求是在服务器外进行的

所以完整的url是------**http://localhost/foo/stuff.do**。

iii:其它Web应用:response.sendRedirect("http://www.xxx.com ")容器直接定向到该URL。

2、请求转发 ◆HttpServletRequest.getRequestDispatcher(String)

参数可以指定为相对路径或绝对路径。

i:相对路径情况下生成的完整URL与重定向方法相同。

ii:绝对路径与Servlet重定向不同容器将相对于Web应用的根目录加参数生成完整的UR L(即"/"根路径就是相对于虚拟路径)这是因为转发是在服务器内部进行的,写绝对路径/开头指的是当前的Web应用程序。即:

request.getRequestDispatcher("/foo/stuff.do" )生成的URL是http://localhost/myApp/foo/stuff.do

3、 ◆ ServletContext.getRequestDispatcher(String)

参数只能指定为绝对路径生成的完整URL与HttpServletRequest.getRequestDispatcher(String)相同

##################################

同理:

JSP 提交表单给 Servlet 路径问题

JSP页面提交表单给Servlet时,路径的写法要格外注意。

例如在web.xml中注册如下的servlet:

<servlet>

<servlet-name>addStudent</servlet-name>

<servlet-class>org.mytest.addStudent</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>addStudent</servlet-name>

<url-pattern>/servlet/addStudent</url-pattern>

</servlet-mapping>

假如说,你工程名字为HibernateApp3,JSP页面提交表单给servlet时有两种写法:

1.相对路径: <form action=servlet/addStudent method=post>...</form>

  1. 绝对路径: <form action="/HibernateApp3/servlet/addStudent" method=post>...</form>

或者 <form action="<%=request.getContextPath() %>/servlet/addStudent" method=post>...</form>

注意:/代表根目录,如果路径是使用/开头,Tomcat就是webApp那个目录,如果你不是/开头代表你从当前工程的目录开始,例如:webApp/HibernateApp3/

这一点非常重要,很多提交表单时发生的错误都是因为提交路径出错造成的。

附、<a href>的路径如果是"/"开头,则表示相对于主机,如果不是则表示相对于当前请求

综上所述:

这里最最关键的要能清楚发出请求目的资源的请求是在服务器内部还是服务器外部:内部时,"/"就是项目的虚拟目录;外部时,"/"就是代表主机的根目录

相关推荐
子兮曰21 小时前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万1 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋1 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁1 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95271 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大1 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师1 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学1 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端