2017-02-21 7 views
0

良い日です。Spring LocaleResolverは、startPage(http:// localhost:8080 /)defaultLocaleには入れません

私はdefaultLocaleを設定するためのLocaleResolverを設定するが、それは、スタートページ(

<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-config設定:

<?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の場合、結果は同じです。

ありがとうございました。

+0

これは 'DispatcherServlet'を通過しないため、サーバーによって直接処理されるためです。 –

+0

助けてもらえますか?私は、web.xmlの上で書き、ことが可能であることが見出さします。 ​​LOCALE RU の – Nolik

+0

はウェルカムページを使用すると、すべてのページが提供されてはいけません'DispatcherServlet'によって。 –

答えて

0

mr。 M. Deinum。問題 -
ウェルカムページを使用せず、DispatcherServletがすべてのページを提供するようにしないでください。 もご利用いただけます:

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

ありがとうございます。

0

このようにインターセプタを更新してください。

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

を追加するだけで、 >)。私はあなたのアドバイスを使用しようとします - 結果は同じです。 – Nolik

関連する問題