栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring MVC(六)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring MVC(六)

文章目录
    • Spring MVC内部资源视图解析器
    • Spring MVC内部资源视图解析器样例
    • Spring MVC Xml视图解析器
    • Spring MVC Xml视图解析器样例
    • Spring MVC资源绑定视图解析器
    • Spring MVC资源绑定视图解析器样例
    • Spring MVC多解析器映射
    • 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映射配置文件


   
   

  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



   

   
      
      
   


hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>


Hello World


   

${message}

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

XmlViewResolver-servlet.xml


   
      /WEB-INF/views.xml
   

views.xml


   

  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



   

   
      
         /WEB-INF/views.xml
      
   

views.xml


   

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>


Hello World


   

${message}

Spring MVC资源绑定视图解析器
  1. ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称

ResourceBundleViewResolver-servlet.xml


   

  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



   

   
      
   

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" %>


Hello World


   

${message}

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

MultipleResolver-servlet.xml


   
   


   
   
   

  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



   

   
      
      
   
   
      
      
      
   

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" %>


Hello World


   

${message}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号