Spring MVC-视图解析器(View Resolverr)-资源包视图解析器(Resource Bundle View Resolver)示例(转载实践)
2017-09-10 09:44
736 查看
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_resourcebundleviewresolver.htm
说明:示例基于Spring MVC [b]4.1.6。[/b]
ResourceBundleViewResolver用于使用在属性文件中定义的视图bean来解析视图名称。以下示例显示如何使用Spring Web MVC框架使用ResourceBundleViewResolver。
TestWeb-servlet.xml
basename是指包含视图的资源束的名称。资源束的默认名称是views.properties,可以使用basename属性覆盖。
views.properties
例如,使用上面的配置,如果是URI
/hello被请求,DispatcherServlet将请求转发到由在您的view.properties中的bean hello定义的hello.jsp。
这里“hello”是要匹配的视图名称。类是指视图类型,url是视图的位置。
首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:
HelloController.java
TestWeb-servlet.xml
views.properties
hello.jsp
完成创建源文件和配置文件后,导出应用程序。右键单击应用程序,然后使用Export > WAR File选项,并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。
现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试访问URL http://localhost:8080/HelloWeb/hello,如果您的Spring Web应用程序一切正常,您应该看到以下结果:
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test24
说明:示例基于Spring MVC [b]4.1.6。[/b]
ResourceBundleViewResolver用于使用在属性文件中定义的视图bean来解析视图名称。以下示例显示如何使用Spring Web MVC框架使用ResourceBundleViewResolver。
TestWeb-servlet.xml
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views" /> </bean>
basename是指包含视图的资源束的名称。资源束的默认名称是views.properties,可以使用basename属性覆盖。
views.properties
hello.(class)=org.springframework.web.servlet.view.JstlView hello.url=/WEB-INF/jsp/hello.jsp
例如,使用上面的配置,如果是URI
/hello被请求,DispatcherServlet将请求转发到由在您的view.properties中的bean hello定义的hello.jsp。
这里“hello”是要匹配的视图名称。类是指视图类型,url是视图的位置。
首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为TestWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。 |
2 | 在com.tutorialspoint包下创建一个Java类HelloController。 |
3 | 在jsp子文件夹下创建一个视图文件hello.jsp。 |
4 | 在src文件夹下创建一个属性文件views.properties。 |
5 | 下载JSTL库jstl.jar。把它放在你的CLASSPATH中。 |
6 | 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。 |
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" /> </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>
完成创建源文件和配置文件后,导出应用程序。右键单击应用程序,然后使用Export > WAR File选项,并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。
现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试访问URL http://localhost:8080/HelloWeb/hello,如果您的Spring Web应用程序一切正常,您应该看到以下结果:
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test24
相关文章推荐
- Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)
- Spring MVC-视图解析器(View Resolverr)-XML视图解析器(Xml View Resolver)示例(转载实践)
- Spring MVC-视图解析器(View Resolverr)-多重解析器(Multiple Resolver)示例(转载实践)
- Spring MVC-控制器(Controller)-可参数化视图控制器(Parameterizable View Controller )示例(转载实践)
- Spring MVC-控制器(Controller)-属性方法名称解析器(Properties Method Name Resolver )示例(转载实践)
- Spring MVC视图解析器:Spring MVC XmlViewResolver示例
- Spring MVC-控制器(Controller)-参数方法名称解析器(Parameter Method Name Resolver )示例(转载实践)
- 详解Spring MVC 4之ViewResolver视图解析器
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- 详解Spring MVC 4之ViewResolver视图解析器
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- 详解Spring MVC 4之ViewResolver视图解析器
- (开发笔记)转载:SpringMVC介绍之视图解析器ViewResolver
- Spring MVC 视图解析器(ViewResolver )
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring MVC 之 视图解析器ResourceBundleViewResolver InternalResourceViewResolver
- Spring MVC 4之ViewResolver视图解析器
- Spring MVC 视图解析器(ViewResolver )
- (csdn内转载)spring学习笔记:配置视图解析器ViewResolver
- Spring MVC 4之ViewResolver视图解析器