The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_resourcebundleviewresolver.htm
Description: The sample is based on spring MVC 4.1.6.
The resourcebundleviewresolver is used to resolve the view name using the view bean defined in the properties file. The following example shows how to use Resourcebundleviewresolver with the Spring WEB MVC framework.
Testweb-servlet.xml
<class= "Org.springframework.web.servlet.view.ResourceBundleViewResolver" > < name= "basename" value= "views"/></ Bean >
BaseName refers to the name of the resource bundle that contains the view. The default name for the resource bundle is views.properties, which can be overridden using the BaseName property.
Views.properties
Hello. (class) =Org.springframework.web.servlet.view.JstlViewhello.url=/web-inf/jsp/hello.jsp
For example, using the above configuration, if it is a URI
/hello is requested, dispatcherservlet forwards the request to the hello.jsp defined by the bean hello in your view.properties.
Here "Hello" is the name of the view to match. A class is a view type, and the URL is the location of the view.
First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:
Steps |
Description |
1 |
Create a project named TestWeb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section. |
2 |
Create a Java class Hellocontroller under the Com.tutorialspoint package. |
3 |
Create a view file under the JSP subfolder hello.jsp. |
4 |
Create a property file Views.properties under the SRC folder. |
5 |
Download Jstl Library Jstl.jar. Put it in your classpath. |
6 |
The final step is to create the contents of all the source and configuration files and export the application as described below. |
Hellocontroller.java
PackageCom.tutorialspoint;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.ui.ModelMap, @Controller @requestmapping ("/hello") Public classhellocontroller{@RequestMapping (method=requestmethod.get) PublicString Printhello (Modelmap model) {Model.addattribute ("Message", "Hello Spring MVC framework!"); return"Hello"; }}
Testweb-servlet.xml
<Beansxmlns= "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-scanBase-package= "Com.tutorialspoint" /> <Beanclass= "Org.springframework.web.servlet.view.ResourceBundleViewResolver"> < Propertyname= "BaseName"value= "views" /> </Bean></Beans>
Views.properties
Hello. (Class) =org.springframework.web.servlet.view.jstlviewhello.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>
After you finish creating the source files and profiles, export the application. Right-click the application and use the Export > WAR file option and save your helloweb.war file in the Tomcat WebApps folder.
Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try to access the URL http://localhost:8080/HelloWeb/hello, if your spring Web application is all right, you should see the following results:
Maven Example:
Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test24
Spring mvc-View parser (view Resolverr)-Resource Bundle View resolver (Resource Bundle view Resolver) example (reprint practice)