在转发和重定向中不需要视图解释器,视图解析的作用,其实就是把控制层返回回来的字符串,拼接到前缀和后缀之间,组成一个jsp页面的地址。
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
如果你要使用视图解析器的方式来进行转发,Controller层的代码如下
//使用视图解析器转发
@RequestMapping("/d1/t4")
public String DemonTest4 (HttpServletRequest request,Model model) {
String text = request.getParameter("text");
model.addAttribute("msg",text);
return "hello";
}
配置文件(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--注册DispatcherServlet 本质就是一个servlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc-servlet.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--配置web.xml文件的启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置文件(springmvc-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描特定包下的注解-->
<context:component-scan base-package="com.zkw.restful"/>
<!--过滤静态资源-->
<mvc:default-servlet-handler/>
<!--mvc注解驱动-->
<mvc:annotation-driven/>
</beans>
在来一个Controller层
注意:不能重定向到WEB-INF/下边。
原因:(1)重定向是客户端的,而转发是服务端内部的。(2)重定向是让客户端去访问重定向的地址,而WEB-INF下的文件是不能通过外部访问的!
package com.zkw.restful;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class TestDemon {
//重定向
@RequestMapping("/d1/t1")
public String DemonTest (HttpServletRequest request,Model model) {
String text = request.getParameter("text");
model.addAttribute("msg",text);
return "redirect:/test.jsp";
}
//转发
@PostMapping("/d1/t2")
public String DemonTest2 ( HttpServletRequest request, Model model) {
String text = request.getParameter("text");
model.addAttribute("msg",text);
return "forward:/WEB-INF/jsp/hello.jsp";
}
//不使用视图解析器转发
@PostMapping("/d1/t3")
public String DemonTest3 (HttpServletRequest request,Model model) {
String text = request.getParameter("text");
model.addAttribute("msg",text);
return "/WEB-INF/jsp/hello.jsp";
}
}
最后来看一下,jsp页面(test.jsp、hello.jsp、index.jsp)因为hello.jsp和test.jsp页面一样,只写一个了。
index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/d1/t1" method="get">
<input type="text" name="text">
<input type="submit">
</form>
<form action="/d1/t2" method="post">
<input type="text" name="text">
<input type="submit">
</form>
<form action="/d1/t3" method="post">
<input type="text" name="text">
<input type="submit">
</form>
</body>
</html>
test.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
index.jsp页面
结果为
重定向:
转发: