Spring MVC - Çoklu Çözümleyici Eşleştirme Örneği

Bir Spring MVC uygulamasında Çoklu Görüntü Çözücü kullanmak istemeniz durumunda, order özelliği kullanılarak öncelik sırası ayarlanabilir. Aşağıdaki örnek, nasıl kullanılacağını gösterir.ResourceBundleViewResolver ve InternalResourceViewResolver Spring Web MVC Çerçevesinde.

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>

Burada sipariş özelliği, bir görünüm çözümleyicinin sıralamasını tanımlar. Bunda, 0 ilk çözümleyici ve 1 sonraki çözümleyici vb.

views.properties

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

Örneğin, yukarıdaki yapılandırmayı kullanarak URI -

  • / merhaba istenirse, DispatcherServlet isteği, views.properties dosyasında bean hello ile tanımlanan hello.jsp'ye iletir.

Başlangıç ​​olarak, çalışan bir Eclipse IDE'ye sahip olalım ve Spring Web Framework kullanarak Dinamik Form tabanlı bir Web Uygulaması geliştirmek için aşağıdaki adımları göz önünde bulunduralım.

Adım Açıklama
1 Spring MVC - Hello World bölümünde açıklandığı gibi com.tutorialspoint paketinin altında TestWeb adıyla bir proje oluşturun.
2 Com.tutorialspointpackage altında bir Java sınıfı HelloController oluşturun.
3 Jsp alt klasörünün altında bir görünüm dosyası hello.jsp oluşturun.
4 SRC klasörü altında bir özellikler dosyası views.properties oluşturun.
5 JSTL kitaplığı jstl.jar'ı indirin . CLASSPATH'ınıza koyun.
6 Son adım, kaynak ve yapılandırma dosyalarının içeriğini oluşturmak ve uygulamayı aşağıda açıklandığı gibi dışa aktarmaktır.

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

merhaba.jsp

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

Kaynak ve yapılandırma dosyalarını oluşturmayı tamamladığınızda, uygulamanızı dışa aktarın. Uygulamanıza sağ tıklayın, kullanınExport → WAR File seç ve kaydet HelloWeb.war Tomcat'in webapps klasöründeki dosya.

Şimdi, Tomcat sunucusunu başlatın ve diğer web sayfalarına standart bir tarayıcı kullanarak webapps klasöründen erişebildiğinizden emin olun. URL'ye erişmeyi deneyin -http://localhost:8080/HelloWeb/helloSpring Web Uygulaması ile her şey yolundaysa aşağıdaki ekranı göreceğiz.