Spring MVC(六)

Spring MVC内部资源视图解析器

  1. InternalResourceViewResolver用于将提供的URI解析为实际URI
  2. Spring Web MVC框架中使用SpringResultViewResolver,InternalResourceViewResolver允许映射网页与请求。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
    
    

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
    
    
      model.addAttribute("message", "Spring MVC Framework 映射网页与请求示例!");

      return "hello";
   }

}

URL映射配置文件

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp/"/>
   <property name="suffix" value=".jsp"/>
</bean>
  1. 对于/hello请求,DispatcherServlet会将请求转发到前缀+ view-name + suffix = /WEB-INF/jsp/hello.jsp

Spring MVC内部资源视图解析器样例

  1. 创建一个名称为 InternalResourceViewResolver的动态WEB项目
  2. com.yiibai.springmvc 包下创建一个Java类HelloController
  3. 在jsp子文件夹下创建一个视图文件:hello.jsp

HelloController.java

package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
    
    

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
    
    
      model.addAttribute("message", "Spring MVC Framework 映射网页与请求示例!");

      return "hello";
   }

}

InternalResourceViewResolver-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.yiibai.springmvc" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${
    
    message}</h2>
</body>
</html>

Spring MVC Xml视图解析器

  1. XmlViewResolver用于在xml文件中定义的视图bean来解析视图名称

XmlViewResolver-servlet.xml

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
   <property name="location">
      <value>/WEB-INF/views.xml</value>
   </property>
</bean>

views.xml

<bean id="hello"
   class="org.springframework.web.servlet.view.JstlView">
   <property name="url" value="/WEB-INF/jsp/hello.jsp" />
</bean>
  1. 对于/hello请求,DispatcherServlet会将请求转发到由view.xml中定义的hello对应的 hello.jsp

Spring MVC Xml视图解析器样例

  1. 创建一个名称为 XmlViewResolver 的动态WEB项目。
  2. 在 com.yiibai.springmvc 包下创建一个Java类HelloController。
  3. 在jsp子文件夹下创建一个视图文件:hello.jsp。
  4. 下载JSTL库jstl.jar。 把它放在 CLASSPATH 中。

HelloController.java

package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
    
    

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
    
    
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

XmlViewResolver-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.yiibai.springmvc" />

   <bean class="org.springframework.web.servlet.view.XmlViewResolver">
      <property name="location">
         <value>/WEB-INF/views.xml</value>
      </property>
   </bean>
</beans>

views.xml

<bean id="hello"
   class="org.springframework.web.servlet.view.JstlView">
   <property name="url" value="/WEB-INF/jsp/hello.jsp" />
</bean>

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${
    
    message}</h2>
</body>
</html>

Spring MVC资源绑定视图解析器

  1. ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称

ResourceBundleViewResolver-servlet.xml

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
   <property name="basename" value="views" />
</bean>
  1. basename是指携带视图的资源束的名称,资源束的默认名称是views.properties,
  2. 可以使用basename属性重写(也就是将views修改为其它名称,对应的views.properties文件名称也要修改)。

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp
  1. 对于/hello请求,DispatcherServlet会将请求转发到由views.properties中定义的hello对应的hello.jsp
  2. 这里“hello”是要匹配的视图名称。class指定视图类型,url是视图的位置

Spring MVC资源绑定视图解析器样例

  1. 创建一个名称为 ResourceBundleViewResolver 的动态WEB项目
  2. 在 com.yiibai.springmvc 包下创建一个Java类HelloController。
  3. 在jsp子文件夹下创建一个视图文件:hello.jsp
  4. 在src文件夹下创建属性文件views.properties
  5. 下载JSTL库jstl.jar。 把它放在 CLASSPATH中

HelloController.java

package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
    
    

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
    
    
      model.addAttribute("message", "Hello, Spring MVC资源绑定视图解析器!");

      return "hello";
   }

}

ResourceBundleViewResolver-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.yiibai" />

   <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
      <property name="basename" value="views" />
   </bean>
</beans>

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${
    
    message}</h2>
</body>
</html>

Spring MVC多解析器映射

  1. 在spring mvc应用程序中使用多个视图解析器,那么可以使用order属性设置优先级顺序

MultipleResolver-servlet.xml

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
   <property name="basename" value="views" />
   <property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp/" />
   <property name="suffix" value=".jsp" />
   <property name="order" value="1" />
</bean>
  1. order属性定义了视图解析器的排序。0作为第一解析器,1作为下一解析器

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp
  1. 对于/hello请求,DispatcherServlet会将请求转发到由views.properties中定义的hello对应的 hello.jsp
  2. “hello”是要匹配的视图名称。class指定视图类型,url是视图的位置

Spring MVC多解析器映射样例

  1. 创建一个名称为 MultipleResolver 的动态WEB项目。
  2. 在 com.yiibai.springmvc 包下创建一个Java类HelloController
  3. 在jsp子文件夹下创建一个视图文件:hello.jsp
  4. 在src文件夹下创建属性文件views.properties
  5. 下载JSTL库jstl.jar。 把它放在 CLASSPATH中

HelloController.java

package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
    
    

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
    
    
      model.addAttribute("message", "Hello,Spring MVC资源绑定视图解析器!");

      return "hello";
   }

}

MultipleResolver-servlet.xmcl

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.yiibai" />

   <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
      <property name="basename" value="views" />
      <property name="order" value="0" />
   </bean>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
      <property name="order" value="1" />
   </bean>
</beans>

views.properties 配置

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${
    
    message}</h2>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_43408367/article/details/121065499