Spring MVC - Ví dụ về ánh xạ nhiều trình phân giải

Trong trường hợp bạn muốn sử dụng Multiple View Resolver trong ứng dụng Spring MVC thì thứ tự ưu tiên có thể được đặt bằng cách sử dụng thuộc tính order. Ví dụ sau đây cho thấy cách sử dụngResourceBundleViewResolverInternalResourceViewResolver trong Spring Web MVC Framework.

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>

Ở đây, thuộc tính order xác định thứ hạng của trình phân giải chế độ xem. Trong trường hợp này, 0 là trình phân giải đầu tiên và 1 là trình phân giải tiếp theo, v.v.

views.properties

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

Ví dụ: bằng cách sử dụng cấu hình trên, nếu URI -

  • / hello được yêu cầu, DispatcherServlet sẽ chuyển tiếp yêu cầu tới hello.jsp được định nghĩa bởi bean hello trong views.properties.

Để bắt đầu, hãy để chúng tôi có một IDE Eclipse đang hoạt động và xem xét các bước sau để phát triển một Ứng dụng Web dựa trên Biểu mẫu Động bằng cách sử dụng Spring Web Framework.

Bươc Sự miêu tả
1 Tạo một dự án với tên TestWeb trong một gói com.tutorialspoint như đã giải thích trong chương Spring MVC - Hello World.
2 Tạo một lớp Java HelloController trong gói com.tutorialspointpack.
3 Tạo một tệp xem hello.jsp trong thư mục con jsp.
4 Tạo một tệp thuộc tính views.properties trong thư mục SRC.
5 Tải xuống thư viện JSTL jstl.jar . Đặt nó vào CLASSPATH của bạn.
6 Bước cuối cùng là tạo nội dung của tệp nguồn và cấu hình và xuất ứng dụng như được giải thích bên dưới.

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>

views.properties

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

xin chào.jsp

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

Khi bạn đã hoàn tất việc tạo tệp nguồn và cấu hình, hãy xuất ứng dụng của bạn. Nhấp chuột phải vào ứng dụng của bạn, sử dụngExport → WAR File tùy chọn và lưu của bạn HelloWeb.war tệp trong thư mục ứng dụng web của Tomcat.

Bây giờ, khởi động máy chủ Tomcat và đảm bảo rằng bạn có thể truy cập các trang web khác từ thư mục ứng dụng web bằng trình duyệt chuẩn. Cố gắng truy cập URL -http://localhost:8080/HelloWeb/hello, nếu mọi thứ đều ổn với Ứng dụng Web Mùa xuân, chúng ta sẽ thấy màn hình sau.