Spring MVC视图(三)

纸上得来终觉浅

1.Spring MVC如何解析视图?

它会把方法的返回值(包括String,ModelAndView,View)转换为ModelAndView对象,然后由ViewResolver视图解析器解析为视图对象;包括JSP/JSTL/PDF等


2 .视图

视图的作用是渲染模型数据,将模型里的数据以某种形式呈现给客户,常用的视图实现类:


3. 1)Spring MVC前面的文章使用的视图技术是JSP,视图解析器使用的是InterResourceViewResolver;

2)如果使用了JSTL,Spring MVC会自动把视图由InternalResourceView转为JstlView,,如果使用了JSTL的fmt标签,则需要在Spring MVC的配置文件中配置国际化资源文件。(没有实践过)

3)可以通过order属性指定解析器的优先顺序,order越小优先级越高。以InternalResourceViewResolver和BeanNameViewResolver为例:

HelloWorld.java:  (视图使用jsp,视图解析器使用的InternalResourceViewResolver)

package roadArchitectWeb.Test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
	@RequestMapping("/view")
	public String index(){
		System.out.println("HelloWorld.index()");
		return "myview";
	}
}
Myview.java:   (使用自定义的视图,解析器使用BeanNameViewResolver)

package roadArchitectWeb.Test;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
/*自定义一个视图*/
@Component
public class Myview implements View{
	@Override
	public String getContentType() {
		return "text/html";
	}
	@Override
	public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
	 response.getWriter().print("This is BeanNameViewResolver");
	}
}
Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>roadArchitectWeb</display-name>

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>helloworld</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
<!-- 		默认加载/WEb-INF/<servletName-servlet>.xml的Spring配置文件,可以通过contextConfigLocation手动指定 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:Spring-MVC.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>helloworld</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
Spring-MVC.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="roadArchitectWeb.Test"></context:component-scan>

<!-- 配置视图解析器 -->
<!-- InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/JSP/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- BeanNameViewResolver -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<!-- 优先级比上面的解析器要高 -->
<property name="order" value="100"></property>
</bean>

</beans>
myview.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.println("This is InternalResourceViewResolver");
%>
</body>
</html>
运行程序,输入/view,得到的结果是  This is BeanNameViewResolver,说明BeanNameViewResolver的配置生效,优先级也是生效的。InternalResourceViewResolver的默认优先级是整型的最大值。
4)Excel视图


5)如果视图解析器没有解析到相关的视图,会抛出ServletException异常

4.如何访问直接访问WEB-INF下的目录:

一般情况下,要访问的话需要设置servlet,直接访问的话可以在MVC的配置文件中增加:

<mvc:view-controller path="/myview" view-name="myview"/>
注:在增加上面语句后,原来的servlet就会失效,于是还需要加上:

<mvc:annotation-driven></mvc:annotation-driven>
详细的解释,后面介绍

5.访问资源文件的话(指的是WebContent目录下(不包括WEB-INF目录)),使用下面的注解即可:

<mvc:default-servlet-handler/>
详细解释后面介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值