2017-02-21 55 views
0

美好的一天,所有。Spring LocaleResolver不會放到startPage(http:// localhost:8080 /)defaultLocale

我配置的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 - 結果相同。

感謝您的關注。

+0

因爲它不通過DispatcherServlet,而是直接由服務器提供服務。 –

+0

你可以advaise,我怎麼能解決它?我發現,可能的話,在web.xml中寫:<的context-param> ​​LOCALE Nolik

+0

不要使用歡迎頁面,讓所有頁面提供服務通過'DispatcherServlet'。 –

回答

0

感謝先生。 M. Deinum。問題 -
請勿使用歡迎頁面,並讓DispatcherServlet爲所有頁面提供服務。 additionaly可以使用:

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

謝謝大家。

0

像這樣更新你的Interceptor。

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

映射在我的實現覆蓋範圍內的所有請求()。我嘗試使用你的advaise - 結果是一樣的。 – Nolik

相關問題