Spring MVC - 多个解析器映射示例


如果您想在 Spring MVC 应用程序中使用多视图解析器,则可以使用 order 属性设置优先级顺序。以下示例演示如何在 Spring Web MVC 框架中使用ResourceBundleViewResolverInternalResourceViewResolver 。

TestWeb-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>

这里,order 属性定义了视图解析器的排名。其中,0 是第一个解析器,1 是下一个解析器,依此类推。

视图.属性

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

例如,使用上面的配置,如果 URI -

  • /hello 被请求时,DispatcherServlet 会将请求转发到views.properties 中bean hello 定义的h​​ello.jsp。

首先,让我们准备一个可用的 Eclipse IDE,并考虑以下步骤来使用 Spring Web 框架开发基于动态表单的 Web 应用程序。

描述
1 按照 Spring MVC - Hello World 章节中的说明,在 com.tutorialspoint 包下创建一个名为 TestWeb 的项目。
2 在 com.tutorialspointpackage 下创建一个 Java 类 HelloController。
3 在jsp子文件夹下创建一个视图文件hello.jsp。
4 在SRC文件夹下创建属性文件views.properties。
5 下载 JSTL 库jstl.jar。将其放入您的 CLASSPATH 中。
6 最后一步是创建源文件和配置文件的内容并导出应用程序,如下所述。

HelloController.java

package com.tutorialspoint;

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";
   }

}

TestWeb-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.tutorialspoint" />

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

视图.属性

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

你好.jsp

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

创建源文件和配置文件后,导出您的应用程序。右键单击您的应用程序,使用Export → WAR File选项并将HelloWeb.war文件保存在 Tomcat 的 webapps 文件夹中。

现在,启动 Tomcat 服务器并确保您能够使用标准浏览器从 webapps 文件夹访问其他网页。尝试访问 URL - http://localhost:8080/HelloWeb/hello,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。

Spring内部资源视图解析器