Spring MVC - Esempio di mappatura di più resolver

Nel caso in cui si desideri utilizzare un resolver a vista multipla in un'applicazione Spring MVC, è possibile impostare l'ordine di priorità utilizzando la proprietà order. L'esempio seguente mostra come utilizzare ilResourceBundleViewResolver e il InternalResourceViewResolver in 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>

Qui, la proprietà order definisce la classifica di un risolutore di viste. In questo, 0 è il primo risolutore e 1 è il successivo risolutore e così via.

views.properties

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

Ad esempio, utilizzando la configurazione precedente, se URI -

  • / hello è richiesto, DispatcherServlet inoltrerà la richiesta a hello.jsp definito dal bean hello in views.properties.

Per cominciare, disponiamo di un IDE Eclipse funzionante e prendiamo in considerazione i seguenti passaggi per sviluppare un'applicazione Web basata su Dynamic Form utilizzando Spring Web Framework.

Passo Descrizione
1 Crea un progetto con un nome TestWeb sotto un pacchetto com.tutorialspoint come spiegato nel capitolo Spring MVC - Hello World.
2 Crea una classe Java HelloController nel pacchetto com.tutorialspoint.
3 Crea un file di visualizzazione hello.jsp nella sottocartella jsp.
4 Crea un file delle proprietà views.properties nella cartella SRC.
5 Scarica la libreria JSTL jstl.jar . Mettilo nel tuo CLASSPATH.
6 Il passaggio finale è creare il contenuto dei file sorgente e di configurazione ed esportare l'applicazione come spiegato di seguito.

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

hello.jsp

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

Una volta terminata la creazione dei file sorgente e di configurazione, esporta la tua applicazione. Fai clic con il tasto destro sulla tua applicazione, usaExport → WAR File opzione e salva il tuo HelloWeb.war file nella cartella webapps di Tomcat.

Ora, avvia il server Tomcat e assicurati di essere in grado di accedere ad altre pagine web dalla cartella webapps utilizzando un browser standard. Prova ad accedere all'URL -http://localhost:8080/HelloWeb/hello, se tutto va bene con l'applicazione Web Spring, vedremo la seguente schermata.