java Spring Framework 2.0如何配置ResourceBundleViewResolver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/104587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:03:09  来源:igfitidea点击:

How to configure ResourceBundleViewResolver in Spring Framework 2.0

javaspringframeworks

提问by Elliot Vargas

Everywhere I look always the same explanation pop ups.
Configure the view resolver.

我所看到的任何地方总是弹出相同的解释。
配置视图解析器。

<bean id="viewMappings"
      class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="views" />
</bean>

And then put a file in the classpath named view.properties with some key-value pairs (don't mind the names).

然后在名为 view.properties 的类路径中放置一个文件,其中包含一些键值对(不要介意名称)。

logout.class=org.springframework.web.servlet.view.JstlView
logout.url=WEB-INF/jsp/logout.jsp

What does logout.classand logout.urlmean?
How does ResourceBundleViewResolveruses the key-value pairs in the file?
My goal is that when someone enters the URI myserver/myapp/logout.htmthe file logout.jspgets served.

是什么logout.classlogout.url意味着什么?
如何ResourceBundleViewResolver使用文件中的键值对?
我的目标是,当有人输入 URI 时myserver/myapp/logout.htm,文件logout.jsp会被提供。

回答by Craig Day

ResourceBundleViewResolver uses the key/vals in views.properties to create view beans (actually created in an internal application context). The name of the view bean in your example will be "logout" and it will be a bean of type JstlView. JstlView has an attribute called URL which will be set to "WEB-INF/jsp/logout.jsp". You can set any attribute on the view class in a similar way.

ResourceBundleViewResolver 使用 views.properties 中的 key/vals 来创建视图 bean(实际上是在内部应用程序上下文中创建的)。您的示例中的视图 bean 的名称将是“注销”,它将是一个 JstlView 类型的 bean。JstlView 有一个名为 URL 的属性,它将被设置为“WEB-INF/jsp/logout.jsp”。您可以以类似的方式在视图类上设置任何属性。

What you appear to be missing is your controller/handler layer. If you want /myapp/logout.htm to serve logout.jsp, you must map a Controller into /myapp/logout.htm and that Controller needs to return the view name "logout". The ResourceBundleViewResolver will then be consulted for a bean of that name, and return your instance of JstlView.

您似乎缺少的是控制器/处理程序层。如果您希望 /myapp/logout.htm 为 logout.jsp 提供服务,您必须将一个 Controller 映射到 /myapp/logout.htm 并且该 Controller 需要返回视图名称“logout”。然后将查询 ResourceBundleViewResolver 以获取该名称的 bean,并返回您的 JstlView 实例。

回答by Brian Matthews

To answer your question logoutis the view name obtained from the ModelAndView object returned by the controller. If your are having problems you many need the following additional configuration.

回答您的问题logout是从控制器返回的 ModelAndView 对象中获取的视图名称。如果您遇到问题,您可能需要以下额外配置。

You need to add a servlet mapping for *.htmin your web.xml:

您需要*.htm在您的 中添加一个 servlet 映射web.xml

    <web-app>
        <servlet>
            <servlet-name>htm</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <oad-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>htm</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    </web-app>

And if you want to map directly to the *.jspwithout creating a custom controller then you need to add the following bean to your Spring context:

如果您想直接映射到*.jsp而不创建自定义控制器,那么您需要将以下 bean 添加到您的 Spring 上下文中:

    <bean id="urlFilenameController"
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />