SpringMVC视图
视图的作用是渲染数据,将模型Model中的数据展示给客户,SpringMVC中视图的种类有很多,默认有转发视图(InternalResourceView)和重定向视图(RedirectView)。
当工程引入jstl的依赖,转发视图会自动跳转jstlView,若使用的视图为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的ThymeleafView。
1、ThymeleafView
当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称将拼接视图前缀和后缀所得到的最终路径会通过转发的方式实现跳转。
test_view.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org"><title>Title</title>
</head>
<body>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
</body>
</html>
ViewController
@Controller
public class ViewController {@RequestMapping("/testThymeleafView")public String testThymeleafView(){return "success";}
}
success.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org"><title>Title</title>
</head>
<body>
success
</body>
</html>
2、转发视图
SpringMVC中默认的转发视图是InternalResourceView,SpringMVC中创建视图的情况:
当控制器方法中设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀为"forward:"去掉,剩下部分作为最终路径通过转发方式实现跳转。如:“forward:/”,“forward:/employee”
ViewController
@Controller
public class ViewController {@RequestMapping("/testThymeleafView")public String testThymeleafView(){return "success";}@RequestMapping("/testForward")public String testForward(){return "forward:/testThymeleafView";}
}
test_view.html
<a th:href="@{/testForward}">测试InternalResourceView</a>
3、重定向视图
SpringMVC中默认的重定型视图是RedirectView,当控制器方法中设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向方式实现跳转。如:“redirect:/”,“redirect:employee”
转发和重定向不能访问web-inf下的文件。
ViewController
@RequestMapping("/testRedirect")
public String testRedirect(){return "redirect:/testThymeleafView";
}
test_view.html
<a th:href="@{/testRedirect}">测试RedirectView</a>
4、视图控制器
当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view_controller
标签进行表示。
如:在上面的程序中,创建一个首页跳转控制器就是达到页面跳转的,可以使用xml配置的形式完成相关功能。
<mvc:view-controller path="/" view-name="firstPage"></mvc:view-controller>
注意:当使用上面的配置文件的时候springMVC的所有Controller都会失效,这时候需要使用<mvc:annotation-driven>
标签开启注解驱动。
完整springMVC配置文件
<?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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--开启组件扫描--><context:component-scan base-package="com.louis.controller"></context:component-scan><bean id="ThymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="characterEncoding" value="UTF-8"/><property name="templateEngine"><bean class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver"><bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><property name="prefix" value="/WEB-INF/templates/"/><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/><property name="characterEncoding" value="UTF-8"/></bean></property></bean></property></bean><!--可表示控制器@Controllerpublic class SetFirstPage {@RequestMapping("/")public String testFirstPage(){return "firstPage";}}表示视图不用走 controller , 而是通过url 直接访问到视图的html ,会导致其它所有的访问路径都不能用--><mvc:view-controller path="/" view-name="firstPage"></mvc:view-controller><!--会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean--><mvc:annotation-driven></mvc:annotation-driven>
</beans>
jsp页面实现SpringMVC视图
环境搭建
web.xml
<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"><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springMVC.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
springMVC.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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--开启组件扫描--><context:component-scan base-package="com.louis.controller"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/templates/"/><property name="suffix" value=".jsp"/></bean>
</beans>
maven坐标
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.1</version>
</dependency>
<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>3.0.12.RELEASE</version>
</dependency>
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version>
</dependency>
<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>首页</title>
</head>
<body>
<h3>首页</h3>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
成功跳转
</body>
</html>
JspController
@Controller
public class JspController {@RequestMapping("/success")public String success(){return "success";}
}