【问题标题】:Spring LocaleResolver don't put to the startPage(http://localhost:8080/) defaultLocaleSpring LocaleResolver 不要放到 startPage(http://localhost:8080/) defaultLocale
【发布时间】:2017-02-21 13:34:28
【问题描述】:

大家好。

我配置了 LocaleResolver 来设置 defaultLocale,但它不适用于起始页(

  <welcome-file-list>
        <welcome-file>/WEB-INF/view/index.jsp</welcome-file>
    </welcome-file-list>

)。

web.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>GetITFRee</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/WEB-INF/view/index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/view/error/errorpage.jsp</location>
    </error-page>
    </web-app>

应用上下文:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


    <context:property-placeholder location="classpath:util.properties" />
    <!--Activates various annotations to be detected in bean classes: Spring's @Required and @Autowired and so on-->
    <context:annotation-config/>

    <!-- Datasource.  -  MySQL -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!--Do not forget activate @Transactional JPA annotation with <annotation-driven/>-->
    <!-- JPA Persistence Context and EntityManager configuration -->
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <!--packagesToScan - search Entity and mapping them -->
        <property name="packagesToScan" value="by.GetItFree" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>
    </bean>

    <!-- Automatic Transaction Participation-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <jpa:repositories base-package="by.GetItFree.orm.repository" entity-manager-factory-ref="entityManagerFactory"
                      transaction-manager-ref="transactionManager"/>


    <!-- REST template configuration -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

</beans>

mvc 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- context:component-scan This tag will scan @Component, @Repository, @Service, @Controller
         and also resolves @Autowired and @Qualifier -->
    <context:component-scan base-package="by.GetItFree"/>

    <!--
        mvc:annotation-driven configures Spring MVC annotations
        Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
        HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values
        from @RequestMapping or @ExceptionHandler methods.
     -->
    <mvc:annotation-driven/>

    <!-- activate @Transactional JPA annotation -->
    <tx:annotation-driven/>

    <!-- ViewResolver bean config for mapping strings to jsp views -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="order" value="1"/>
        <property name="prefix" value="/WEB-INF/view"/>
        <property name="suffix" value=".jsp"/>
    </bean>


    <mvc:view-controller path="/about.html" view-name="/about/about"/>
    <mvc:view-controller path="/index.html" view-name="/index"/>


    <!-- Static Resources Configuration (get access to static sources such as CSS and JavaScript files) -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <mvc:interceptors>

        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="languageVar"/>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="ru"/>
        <!-- cookieMaxAge in seconds. if you set it to -1, the cookie will be deleted when browser is closed) -->
        <property name="cookieMaxAge" value="100000"/>
    </bean>

    <!-- MessageSource ReloadableResourceBundleMessageSource configuration -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames" value="classpath:/locales/messages,classpath:util"/>
        <property name="cacheSeconds" value="1"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

    <!--
     mvc:annotation-driven configures Spring MVC annotations
     Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath.
     HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values
     from @RequestMapping or @ExceptionHandler methods.
    -->
    <mvc:annotation-driven>
        <!--use int RestController to produce pretty json response-->
        <mvc:message-converters>
            <bean id="jacksonHttpMessageConverter"
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prettyPrint" value="true"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

如果我将尝试直接访问http://localhost:8080/index.htlm - 它设置为默认区域设置。 对于 SessionLocaleResolver - 结果相同。

感谢您的关注。

【问题讨论】:

  • 因为它不通过DispatcherServlet,而是直接由服务器提供服务。
  • 您能建议一下,我该如何解决?我发现,可能,写在 web.xml: LOCALEru
  • 不要使用欢迎页面,让所有页面都由DispatcherServlet 提供服务。
  • 谢谢!这很简单 -> 只需添加

标签: java spring spring-mvc locale


【解决方案1】:

像这样更新你的拦截器。

  <mvc:interceptors>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="languageVar"/>
            </bean>
    </mvc:interceptors>

【讨论】:

  • 映射在我的实现中覆盖了所有请求()。我尝试使用您的建议 - 结果相同。
【解决方案2】:

感谢先生。 M.德南。问题 -
不要使用欢迎页面,而是让 DispatcherServlet 提供所有页面。 另外可以使用:

 <mvc:view-controller path="/" view-name="first-view page"> 

谢谢大家。

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2021-06-28
    • 2015-09-18
    • 2017-03-19
    • 2021-01-26
    相关资源
    最近更新 更多